Results 1 to 10 of 10

Thread: Update GUI from another thread

  1. #1
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Update GUI from another thread

    Hi Everyone!

    I'm struggling with the following problem for quite some time. My application runs heavy mathematical equations based on input files. The actual work is done within a separate thread. This way the GUI remains responsive. Everything works great, but I just can't figure out how to update the GUI from the separate thread. While processing the equations I want the GUI to show which equation is currently processed.

    I just recently started using QT. This because I need to develop a application that runs on both Windows and Mac. The concept of slots is hard to me. I have developed many applications using Xcode and Cocoa. In this environment I can create complicated multithreading applications (that continuously update the GUI) without any problem. My C++ knowledge is average.

    It drives me crazy. I spend to much time figuring this out. And still no result.

    To the point:
    Who can create an sample project for me?

    The sample must contain:
    - GUI with one text-box / button
    - Clicking the button starts a new thread
    - The new thread executes a loop 5 times.
    - The loop simulates a heavy process (for example by using 1-second sleep)
    - Every time the loop starts the GUI is updated by appending a line to the textbox.

    I'm willing to pay for your time. I definitely need some help here. Please keep the sample as simple as possible. This way i can understand how it works.

    (A little desperate)
    Kind regards,
    Anne

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Update GUI from another thread

    How does your thread look like? Add in *.h file
    Qt Code:
    1. Q_SIGNALS:
    2. void currentEquitation(QString);
    To copy to clipboard, switch view to plain text mode 
    In the cpp, where you calculate a new equation simple do
    Qt Code:
    1. Q_EMIT currentEquitation("Equitation foo");
    To copy to clipboard, switch view to plain text mode 
    And then in your GUI class where you start your thread (assuming it is called thread and you have a QLabel called label):
    Qt Code:
    1. QObject::connect(thread, SIGNAL(currentEquitation(QString)), label, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 
    That's all.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Update GUI from another thread

    Hi,

    Hope this simple example will help you.
    Attached Files Attached Files
    Òscar Llarch i Galán

  4. The following user says thank you to ^NyAw^ for this useful post:

    Anne (14th July 2010)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Update GUI from another thread

    You don't have to subclass QThread, you can simply use existing classes for compilation and use them "directly":
    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class calc : public QObject
    5. {
    6. Q_OBJECT
    7.  
    8. public Q_SLOTS:
    9. void doCalc()
    10. {
    11. for (int i = 1; i < 25; ++i)
    12. {
    13. Q_EMIT message(QString::number(i));
    14. QTime t = QTime::currentTime();
    15. t = t.addSecs(2);
    16. while (t > QTime::currentTime()) {}
    17. }
    18. }
    19.  
    20. Q_SIGNALS:
    21. void message(QString);
    22. };
    23.  
    24.  
    25. int main(int argv, char** args)
    26. {
    27. QApplication app(argv, args);
    28.  
    29. // GUI
    30. QPushButton* b = new QPushButton("start", &w);
    31. QLabel* l = new QLabel(&w);
    32. QLineEdit* le = new QLineEdit(&w);
    33. QVBoxLayout* la = new QVBoxLayout();
    34. la->addWidget(l);
    35. la->addWidget(b);
    36. la->addWidget(le);
    37. w.setLayout(la);
    38. w.show();
    39.  
    40. // Thread
    41. calc c;
    42. c.moveToThread(&t);
    43. t.start();
    44.  
    45. // Connections
    46. QObject::connect(&c, SIGNAL(message(QString)), l, SLOT(setText(QString)));
    47. QObject::connect(b, SIGNAL(clicked()), &c, SLOT(doCalc()));
    48.  
    49. return app.exec();
    50. }
    51.  
    52. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lykurg for this useful post:

    Anne (14th July 2010)

  7. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Update GUI from another thread

    Quote Originally Posted by Anne View Post
    I'm willing to pay for your time.
    If you want to offer a job, even if it is a small one, please use your "Jobs" forum.

    Nevertheless, if you found the answers usefull, nobody will stop you if you spend some money to QtCentre.org or KDE e.V. or or or...

  8. #6
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Update GUI from another thread

    Dear Lykurg and ^NyAw^,

    Wow, is it just that simple?! QT really amazes me.

    You really helped me out here, everything is perfectly clear right now. I was thinking too complicated. In Xcode / Cocoa it's actually much harder to do exactly this. If you ever have any Xcode / Cocoa related question, please contact me!

    Generally I never ask for help. I want to figure out things like these myself. In the long run this is the best way to learn a new language in my opinion. But in this case I was completely stuck.

    I did not really intend to offer a job, was just asking for an example. Please give me some info on how to donate to qtcentre.org (can't find the PayPal address). I love communities like these and always support them.

    All your help is greatly appreciated,
    Kind regards,
    Anne

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Update GUI from another thread

    Quote Originally Posted by Anne View Post
    Please give me some info on how to donate to qtcentre.org (can't find the PayPal address). I love communities like these and always support them.
    Qt Centre paypal account is foundation at qtcentre.org.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Update GUI from another thread

    @wysota
    Thanks for the address!

    @^NyAw^ and Others
    I tried to open your sample project, but the folder does not contain any .pro files.
    The only files I see are .h, .cpp, .ui and .qrc.
    How can I open such an project directly with QT? The .qrc is not recognized?
    (Of cours I now just directly used the .cpp and .h files, but was just curious)

  11. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Update GUI from another thread

    You can run "qmake -project" inside that folder. Then Qt is generating a pro file for you.

  12. #10
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Update GUI from another thread

    Hi,

    Quote Originally Posted by Anne View Post
    @^NyAw^ and Others
    I tried to open your sample project, but the folder does not contain any .pro files.
    The only files I see are .h, .cpp, .ui and .qrc.
    How can I open such an project directly with QT? The .qrc is not recognized?
    (Of cours I now just directly used the .cpp and .h files, but was just curious)
    I just don't added the pro file because I'm using Visual Studio on Windows and I'm not using the pro file, but there is an option to create the pro file that then I can attach.
    Just use "qmake -project" as Lykurg said to create the "pro" file and then call "make" to compile the project.

    Quote Originally Posted by Anne View Post
    Generally I never ask for help
    It's a good idea to learn by yourself, but sometimes the examples don't show what you are expecting or you are not able really how something works. Then, ask the forum! (but first of all try to find similar posts on it).
    Òscar Llarch i Galán

Similar Threads

  1. Replies: 9
    Last Post: 28th November 2009, 21:31
  2. update a wigdet in a thread is no allow?
    By lanmanck in forum Qt Programming
    Replies: 8
    Last Post: 25th October 2009, 04:05
  3. Replies: 16
    Last Post: 7th October 2009, 09:17
  4. update widget from separate thread
    By method in forum Qt Programming
    Replies: 5
    Last Post: 10th July 2009, 15:33
  5. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 23:09

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.