Hello everyone,

I have encountered on problem of signal&slots. When I used QObject::connect function in main, where QGuiApplication is declared, everything works properly. However, when I put the code responsible for finding object from QML and handling the connection on outside class, the funcionality doesn't work. Please for help

Not working code:

Qt Code:
  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QQmlEngine>
  4. #include <QQmlComponent>
  5. #include <QDebug>
  6. #include <QObject>
  7. #include <QQmlProperty>
  8. #include "fun.h"
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. QGuiApplication app(argc, argv);
  14.  
  15.  
  16. fun f;
  17. f.start();
  18.  
  19.  
  20. return app.exec();
  21. }
To copy to clipboard, switch view to plain text mode 

Source of the class
Qt Code:
  1. #include <QQmlApplicationEngine>
  2. #include <QQmlEngine>
  3. #include <QQmlComponent>
  4. #include <QDebug>
  5. #include <QObject>
  6. #include <QQmlProperty>
  7. #include "fun.h"
  8.  
  9.  
  10. fun::fun()
  11. {
  12.  
  13. }
  14.  
  15. void fun::start()
  16. {
  17.  
  18. QQmlApplicationEngine engine;
  19.  
  20. QQmlComponent componentMainWindow(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
  21.  
  22. qDebug() << componentMainWindow.errors();
  23.  
  24. QObject *objectMainWindow = componentMainWindow.create();
  25.  
  26. QObject *objectKwad = objectMainWindow->findChild<QObject*>("kwadlo");
  27.  
  28. objectKwad = objectKwad->findChild<QObject*>("kwad");
  29.  
  30. if (objectKwad) {
  31. qDebug() << "Object was found";
  32.  
  33. } else {
  34. qDebug() << "Object with given name was not found";
  35. }
  36.  
  37. objectKwad->setProperty("width", 120);
  38. connect(objectKwad, SIGNAL(buttonClick()), this, SLOT(cppSlot()));
  39.  
  40. }
To copy to clipboard, switch view to plain text mode 

Header of the class
Qt Code:
  1. #ifndef FUN_H
  2. #define FUN_H
  3.  
  4. #include <QObject>
  5. #include <QDebug>
  6.  
  7.  
  8. class fun : public QObject
  9. {
  10. Q_OBJECT
  11. public:
  12. fun();
  13. void start();
  14.  
  15. public slots:
  16. inline void cppSlot()
  17. {
  18. qDebug() << "Button was clicked";
  19. }
  20. };
  21.  
  22. #endif // FUN_H
To copy to clipboard, switch view to plain text mode 


Working code, when I use the QObject::connect in main.
Qt Code:
  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QQmlEngine>
  4. #include <QQmlComponent>
  5. #include <QDebug>
  6. #include <QObject>
  7. #include <QQmlProperty>
  8. #include "fun.h"
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. QGuiApplication app(argc, argv);
  14. /*fun f;
  15.   f.start();*/
  16.  
  17.  
  18. QQmlApplicationEngine engine;
  19.  
  20. QQmlComponent componentMainWindow(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
  21.  
  22. qDebug() << componentMainWindow.errors();
  23.  
  24. QObject *objectMainWindow = componentMainWindow.create();
  25.  
  26. QObject *objectKwad = objectMainWindow->findChild<QObject*>("kwadlo");
  27.  
  28.  
  29. objectKwad = objectKwad->findChild<QObject*>("kwad");
  30. if (objectKwad) {
  31.  
  32. } else {
  33. qDebug() << "Object with given name was not found";
  34. }
  35. objectKwad->setProperty("width", 120);
  36. QObject::connect(objectKwad, SIGNAL(buttonClick()), &f, SLOT(cppSlot()));
  37.  
  38. return app.exec();
  39. }
To copy to clipboard, switch view to plain text mode