Solved Signal not emitted or not received
-
Hi,
I am not 100% sure, but shouldn't you set all the context properties before creating your component since you are connecting stuff in there from the context ?
-
Hi @SGaist ,
thanks for taking a look. Forgive my ignorance, but could you expand on that a little? Do you mean in the MainWindow.cpp?
These?
engine.rootContext()->setContextProperty("pMessages", &pMessageProcessor); engine.rootContext()->setContextProperty("modFinder", &moduleFinder); engine.rootContext()->setContextProperty("mainWindow", this);
-
Yes, you are constructing your component before these are assigned.
Move it after these three lines.
-
Again - sorry for not quite getting it! Do you mean when the QML window gets created here?
void MainWindow::on_actionP_Bus_triggered() { if( component->status() != component->Ready ) { if( component->status() == component->Error ) qDebug()<< "Error: " + component->errorString(); return; } component->create(); }
this does happen after mainWindow has been created.
-
I was thinking about the construction part:
QQmlComponent *component = new QQmlComponent(&engine, (QUrl(QStringLiteral("qrc:/qml/mainAppWindow.qml"))));
-
Hi @SGaist
I moved the constructor into the function and it didn't make a difference.
-
Then I would first take these two into the main function and initialise everything there just to be sure it's working correctly.
-
I tried it. The QML component initialized immediately, but there was no change in the behavior.
I was reading this article:
debugging-signals-and-slots-in-qt
and tried this line:
connect(this, SIGNAL(&analogInputsValue()), qApp, SLOT(aboutQt));
I assume this is supposed to pop up the Qt About box when the signal triggers, but I get a console message:
"qObject::connect: no such signal pMessageProcessor::&analogInputsValue()
-
-
Hi @JKSH,
I removed the '&' and got the same result - "no such signal".
-
@mscottm said in Signal not emitted or not received:
I removed the '&' and got the same result - "no such signal".
Looking at your
mainWindow.h
, you don't have aanalogInputsValue()
signal. Instead, you have aanalogInputsValue(QString, qint16, qint16, qint16, qint16)
signal.So, your connection code should be
connect(this, SIGNAL(analogInputsValue(QString, qint16, qint16, qint16, qint16)), qApp, SLOT(aboutQt()));
NOTE: Remember the
()
in the slot too! WriteSLOT(aboutQt())
, notSLOT(aboutQt)
-
@jksh said in Signal not emitted or not received:
connect(this, SIGNAL(analogInputsValue(QString, qint16, qint16, qint16, qint16)), qApp, SLOT(aboutQt()));
OHHH! That did it, thanks!
So the signal is obviously firing - the 'About' message box popped up. Now I know where to focus my troubleshooting. I know that at least the IDE is recognizing the class contextProperty, as it 'colorizes' the target class name in the connect statement.
What else can I look at or try?
-
Well, to ensure that the basics are working, try starting from the Embedding C++ Objects into QML with Context Properties example and then move things around until they are where you would like to have them in your application.
-
@SGaist - on that page it says:
"If the QML item needs to receive signals from the context property, it can connect to them using the Connections type. For example, if ApplicationData has a signal named dataChanged(), this signal can be connected to using an onDataChanged handler within a Connections object:
Text { text: applicationData.getCurrentDateTime() Connections { target: applicationData onDataChanged: console.log("The application data changed!") } }
That is what I'm doing, and the only difference from my QML and their example is I have my Connections statement higher in the hierarchy, but I've tried it in multiple places.
-
What I wanted you to check was that your QML + context properties are working correctly when following the same setup as the example provided in the documentation. Once that has been confirmed, we can then move these stuff in your main window class.
-
The code you posted is very large; it will take us a lot of time to read through and identify your problem.
Please post a minimal example that demonstrates your problem.
-
Hi @SGaist,
I think you put me on to something. When I created the project to test the code in the example you pointed to, I noticed in the 'boilerplate' code that was created, a QObject::connect statement that I don't have:
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url);
Is this connecting the qml side to the cpp side? I didn't think about this as I was adapting an example. Do I need to add something like this?
edit - I also just noticed that there is a QQmlApplicationEngine created in the main.cpp, and I created another one in MainWindow.cpp - that has to be an issue...doesn't it?
@JKSH - I do apologize, I was trying to be thorough, but I understand the need for brevity when someone is trying to help and get to the heart of an issue.
-
It can be useful to stop the application if there's a failure to create the object from the file you passed as parameter.
It is however not directly related to the issue you are having.
-
Hi @SGaist ,
I created a small project and adapted the example you pointed to, and created a signal in c++ that would update a text in QML every few seconds using context properties and a Connections statement in QML:
QTimer* timer = new QTimer(this); timer->setInterval(3000); connect(timer, SIGNAL(timeout()), this, SLOT(sendOutSignal())); timer->start(); } void pMessageProcessor::sendOutSignal() { x+=1; number.setNum(x); emit mySignal(number); }
Text { id: myText text: {""} Connections { target: applicationData onMySignal: myText.text = myVariable } }
It works as expected.
Next I moved the code over to my application and connected one of my QML label texts to the signal and it works - the label updates with the timer and signal, however I still can't get my original signal to work.
-
@mscottm said in Signal not emitted or not received:
Next I moved the code over to my application and connected one of my QML label texts to the signal and it works - the label updates with the timer and signal, however I still can't get my original signal to work.
Congratulations, that's good progress.
Compare the two applications and see what's different between the way your C++ and QML objects are set up. This will lead you to the solution. It might help if you start removing parts from your original application that are not related to the connection.