Results 1 to 3 of 3

Thread: Qt Signal&Slots

  1. #1
    Join Date
    Dec 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Qt Signal&Slots

    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Signal&Slots

    The QQmlApplicationEngine and QQmlComponent you create in lines 18 and 20 of fun.cpp go out of scope as soon as the start() function exits, so all of the QQmlComponent's children and their connections will be destroyed when it is. In your working code, that QQmlApplicationEngine and QQmlComponent do not go out of scope until the program exits, so all references to them and their children are valid for the entire program.

    You can change your fun class so that it works by making "engine", "componentMainWindow", and "objectMainWindow" member variables of the fun class, and initializing them in the constructor for fun:

    Qt Code:
    1. #ifndef FUN_H
    2. #define FUN_H
    3.  
    4. #include <QObject>
    5. #include <QQmlApplicationEngine>
    6. #include <QQmlEngine>
    7. #include <QQmlComponent>
    8. #include <QDebug>
    9.  
    10.  
    11. class fun : public QObject
    12. {
    13. Q_OBJECT
    14. public:
    15. fun();
    16. void start();
    17.  
    18. public slots:
    19. inline void cppSlot()
    20. {
    21. qDebug() << "Button was clicked";
    22. }
    23.  
    24. private:
    25. QQmlApplicationEngine engine;
    26. QQmlComponent componentMainWindow;
    27. QObject * objectMainWindow;
    28. };
    29.  
    30. #endif // FUN_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QDebug>
    2. #include <QObject>
    3. #include <QQmlProperty>
    4. #include "fun.h"
    5.  
    6.  
    7. fun::fun()
    8. : componentMainWindow( &engine, QUrl(QStringLiteral("qrc:/main.qml") )
    9. {
    10. objectMainWindow = componentMainWindow.create();
    11. }
    12.  
    13. void fun::start()
    14. {
    15. qDebug() << componentMainWindow.errors();
    16.  
    17.  
    18. QObject *objectKwad = objectMainWindow->findChild<QObject*>("kwadlo");
    19.  
    20. objectKwad = objectKwad->findChild<QObject*>("kwad");
    21.  
    22. if (objectKwad) {
    23. qDebug() << "Object was found";
    24.  
    25. } else {
    26. qDebug() << "Object with given name was not found";
    27. }
    28.  
    29. objectKwad->setProperty("width", 120);
    30. connect(objectKwad, SIGNAL(buttonClick()), this, SLOT(cppSlot()));
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Signal&Slots

    Quote Originally Posted by Gumis View Post
    However, when I put the code responsible for finding object from QML and handling the connection on outside class
    Which is a bad idea anyway.

    But d_stranz has the answer if you really want to go down that path.

    Cheers,
    _

Similar Threads

  1. One signal more slots
    By paolom in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2012, 11:48
  2. Signal/Slots, Where can i use it?
    By Tio in forum Newbie
    Replies: 2
    Last Post: 25th May 2010, 01:36
  3. Signal and Slots
    By waynew in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 03:50
  4. signal and slots
    By vermarajeev in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2007, 08:31
  5. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.