Results 1 to 4 of 4

Thread: Qt signal handling question

  1. #1
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Qt signal handling question

    Hi all,

    I should implement a very simple form in which I have two buttons: Start and Stop.
    Connected to the Start button I have a loop like:

    PROCESS_IS_RUNNING=true;

    while (i<I_Max && PROCESS_IS_RUNNING)
    {
    ....
    }

    where PROCESS_IS_RUNNING is a global bool variable.

    When the Stop button is clicked the variable PROCESS_IS_RUNNING is set to false value. I was expecting in this way to stop the loop when the Stop button is pressed but this is not the case. I cannot press the Stop button until the process connected to the Start button is completed (when i=I_MAX). I guess this is a signals handling problem under qt but I cannot figure out how to implement the solution. Any suggestion?

    Thank you in advance for you attention

    Bye

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt signal handling question

    with this infinite loop you blocks main thread.
    while (i<I_Max && PROCESS_IS_RUNNING)
    {
    ....
    }
    there are two solutions:
    1. put qApp->processEvent() in that loop.
    2. put this loop in separate thread.
    I also suggest you to read this.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Angry Re: Qt signal handling question

    Thanks a lot for your help. Anyway I'm quite new in using Qt and there are still some points not very clear to me.

    I have decided to use the processEvents() option. In principle everything is clear to me on how to use it but I have some difficulties to implement it in my code.

    I define the QApplication in my main.cpp file:

    int main(int argc, char *argv[])
    {
    QApplication app(argc,argv);

    qdialog m;
    app.setMainWidget(&m);
    m.show();
    return app.exec();

    }

    I have the loop which I want to stop in a slot of qdialog class. Of course app is not defined in the qdialog class and so I cannot use something like app.processEvents() in my loop. On the other hand if I define app in qdialog class I cannot pass to it the command line options.
    I'm a little bit confused! Where and in which way should I define app?

    Bye

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt signal handling question

    if you carefully read documentation about QApplication you will see that there is qApp macro which performs access to an application instance.
    so, you can use this macro whenever you need.
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. MyWidget mw;
    6. mw.show();
    7.  
    8. return app.exec();
    9. }
    10.  
    11. void MyWidget::loop()
    12. {
    13. while (m_flag) {
    14. ...
    15. qApp->processEvents();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. The following user says thank you to spirit for this useful post:

    gd (12th November 2009)

Similar Threads

  1. Signal Slot Question
    By graciano in forum Newbie
    Replies: 8
    Last Post: 19th August 2009, 10:35
  2. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  3. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  4. QLabel text change signal question
    By MarkoSan in forum Newbie
    Replies: 10
    Last Post: 5th April 2008, 10:19
  5. Another signal and slot question
    By fnmblot in forum Newbie
    Replies: 5
    Last Post: 4th March 2008, 19:50

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.