QObject::connect fails ... Don't know why (solved)
-
Hi guys,
I'm new at developing with Qt, but I promised a friend of mine to programm a Raspberry Pi Software to controll a hookah desk. Of course we want a grafical UI but we need C++ as programminung language due to some interfaces. So I thought I wanna try QML in combination with C++.
Me - a total Qt noob - spend the last few hours figuring out why I always get this error messenge:
QObject::connect: No such slot QObject::qmlSignal(QString) in ..\Shishatisch\main.cpp:18
I searched for a sollution for this problem and it turned out that some other guys have that problem, too. The solution always was to name all functions the same way. But mine are since the beginning. So that didn't help me. 1.000.000 Google searches later I gave up and decided to ask some experts.
Due to the fact that this isn't exactly a secret project I will now post all of my source code (beside the qml code cause it would be too long)
connect.h:
#ifndef CONNECT_H #define CONNECT_H #include <qstring.h> #include <QObject> class Connect : public QObject { public: explicit Connect(QObject *parent = 0); signals: public slots: void qmlSignal(QString msg); }; #endif // CONNECT_H
connect.cpp
#include "connect.h" #include <QDebug> Connect::Connect(QObject *parent) : QObject(parent) { } void Connect::qmlSignal(QString msg) { qDebug() << msg; }
main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <connect.h> #include <QQmlContext> #include <QQuickItem> #include <QtCore> #include <QDebug> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); Connect* data=new Connect(); QObject* item=engine.rootObjects().value(0); QObject::connect(item,SIGNAL(qmlSignal(QString)),data,SLOT(qmlSignal(QString))); return app.exec(); }
-
@onlydp You have some little issues here regarding the difference between a slot and a signal I think.
I signal is not really a function - you declare it but do not define its implementation. You will "emit" the signal at some point and pass parameters with it. The slot is where a signal can be received and you declare it in the header and also declare an implementation. So in the header file its:
signals: void mySignalToSendData(QString msg); publis slots: void mySlotToReceiveData(QString msg);
and in the cpp file:
void myClass::mySlotToReceiveData(QString msg) { .. do stuff.. }
Then you connect a signal to a slot (maybe in main):
myClass myclass; connect(&myclass, SIGNAL(mySignalToSendData(QString msg)), &myclass, SLOT(mySlotToReceiveData(QString msg))
In your code you do not want to make your slots and signals have the same name - it will be very confusing I think. Signals are like "send message" functions and slots are like "message receivers"
-
@onlydp said:
class Connect : public QObject
{
public:
explicit Connect(QObject *parent = 0);signals:
public slots:
I would suggest not to name slot FOO "signalFoo" so people are not confused.
But you are missing Q_OBJECT macro:class Connect : public QObject
{
Q_OBJECT
public:
......