Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML communicating with C++

Tags:

c++

qt

qml

I am having problems with QML communicating with C++. I had followed all the steps expected to make the sample code to run properly. After some hours working on this small example, it comes down to one error message, that I simply can't get rid off:

./input/main.cpp:18: error: 
        no matching function for call to 
        'QObject::connect(QObject*&, const char*, Input*, const char*)'
                   &input, SLOT(cppSlot(QString)));
                                                 ^

I read some previous posts on related problem, double checking everything, and apparently everything looks correct. Here are the details:

./Sources/main.cpp

#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlEngine>
#include <QQuickWindow>
#include <QtDeclarative>
#include <QObject>
#include <QDebug>
#include "Input.h"

int main(int argc, char *argv[]) {
 QApplication app(argc, argv);

 QDeclarativeView view(QUrl::fromLocalFile("input.qml"));
 QObject *item = view.rootObject();

 Input input;
 QObject::connect(item, SIGNAL(qmlSignal(QString)),
                  &input, SLOT(cppSlot(QString)));
 view.show();
 return app.exec();
}

./Headers/Input.h

#ifndef INPUT_H
#define INPUT_H
#include <QObject>
#include <QDebug>
class Input : public QObject
{  public:
     Input(){} // default constructor
     Q_OBJECT
   public slots:
     void cppSlot(const QString &msg) {
                  qDebug() << "Called the C++ slot with message:" << msg;
          }

};
#endif // INPUT_H

./Input.pro

QT += qml quick sensors xml
QT += declarative
SOURCES += \
    main.cpp \
    Input.cpp
RESOURCES += \
    Input.qrc
OTHER_FILES += \
    Input.qml
HEADERS += \
    Input.h

./Resources/Input.qrc

/
Input.qml

The connect, I am using is from the qtproject example:

QObject::connect(item, SIGNAL(qmlSignal(QString)),&myClass, SLOT(cppSlot(QString))); 

Maybe someone could have some clue what is missing here? Thanks!

like image 688
Gerry Woodberg Avatar asked Dec 30 '13 00:12

Gerry Woodberg


People also ask

How do you communicate between C++ and QML?

Connecting to QML Signals All QML signals are automatically available to C++, and can be connected to using QObject::connect() like any ordinary Qt C++ signal. In return, any C++ signal can be received by a QML object using signal handlers.

How do I connect with QML?

A Connections object creates a connection to a QML signal. However, it is not possible to connect to a signal in this way in some cases, such as when: Multiple connections to the same signal are required. Creating connections outside the scope of the signal sender.

How QML works with C++?

QML is designed to be easily extensible through C++ code. The classes in the Qt QML module enable QML objects to be loaded and manipulated from C++, and the nature of QML engine's integration with Qt's meta object system enables C++ functionality to be invoked directly from QML.

Is QML compiled to C++?

With the QML type compiler, you will be able to compile your object structure to C++. Each QML component becomes a C++ class this way.


1 Answers

Will the real class Input please stand up?

You seem to have one defined in Input.h, and another one in Input.cpp. Only one of them is a Q_OBJECT and QObject subclass. main.cpp is seeing the other one from Input.h, so it's entirely unsurprising it can't connect it.

See e.g this tutorial for how to correctly split c++ class declarations and definitions between header and source files, if you're unfamiliar with this area of C++.

like image 197
timday Avatar answered Sep 24 '22 14:09

timday