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!
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.
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.
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.
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.
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++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With