Question about signal and slot
-
firstclass.h
#ifndef FIRSTCLASS_H #define FIRSTCLASS_H #include <QObject> class FirstClass : public QObject { Q_OBJECT public: explicit FirstClass(QObject *parent = nullptr); signals: void signalfirst(); public slots: void displayfirst(); };
firstclass.cpp
#include "firstclass.h" #include "secondclass.h" #include <QDebug> FirstClass::FirstClass(QObject *parent) : QObject(parent) { } void FirstClass::displayfirst() { qDebug()<<"Display First"; emit signalfirst(); SecondClass scnd; scnd.displaysecond(); }
Secondclass.h
#ifndef SECONDCLASS_H #define SECONDCLASS_H #include <QObject> class SecondClass : public QObject { Q_OBJECT public: explicit SecondClass(QObject *parent = nullptr); void displaysecond(); signals: void signalSecond(); public slots: }; #endif // SECONDCLASS_H
Secondclass.cpp
#include "secondclass.h" #include <QDebug> SecondClass::SecondClass(QObject *parent) : QObject(parent) { } void SecondClass::displaysecond() { qDebug()<<"Display Second"; emit signalSecond(); }
main.cpp
#include <QQmlContext> #include <firstclass.h> #include <secondclass.h> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterType<SecondClass>("com.mycompany.qmlcomponents", 1, 0, "Secondclasssignal"); FirstClass first; QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("bhushan",&first); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
main.qml
import QtQuick 2.9 import QtQuick.Window 2.2 import com.mycompany.qmlcomponents 1.0 Rectangle { width: parent.width height: parent.height color: "red" MouseArea { anchors.fill: parent onClicked: { bhushan.displayfirst() } } Connections { target: bhushan onSignalfirst: { console.log("first came") } } Secondclasssignal { onSignalSecond: { console.log("Second came") } } }
I am able to get signal of first class, But i am not able to get signal of second class,
Is there any solution or explanation why it is so ? -
Hi,
If you are talking about the second class object you create in
displayfirst
, it's not connected anywhere so there's no reason for anything to receive the signal. -
In addition to what @SGaist said, where do you want the signal of Object of SecondClass ? You are just creating second object in display(..) method. This is object gets destroyed once the function call is over. Second object is not exposed to QML. Second object emits signal. However you have not connected or handling this signal anywhere. No destructors exist in any of the classes. This is making you think something else becz you are not noticing the destruction of objects.
-
@dheerendra @SGaist Sir, How can i expose second object that i have created in qml ?
-
- setContextProperty OR
- Return the object from method of first class. You need to use qmlRegisterMetaType<>() for this to work.
-
@Bhushan_Sure to add to @dheerendra,
to return an object to qml, it has to be created on the heap and if parentless, the QML-Engine will - untill explicitly told otherwise - take ownership of the object and the gc may delete it at any time without you knowing it.
So keep that in mind.
-
@J-Hilk super caution !!!.
-
Did the suggestion worked for you ? Hope the issue is resolved.
-
@dheerendra @J-Hilk I was doing mistake in this, in
FirstClass.cpp i am making SecondClass scnd(Second class object) and using secondclass object i am emitting signal, but i have registered firstclass object in main.cpp and not SecondClassobject.- In short, i was emiting signal using secondclass_object but i registered only firstclass_object in main.cpp and not Secondclass_object. That's why it is creating problem.