Results 1 to 10 of 10

Thread: Update GUI from another thread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

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

    Anne (14th July 2010)

Similar Threads

  1. Replies: 9
    Last Post: 28th November 2009, 20:31
  2. update a wigdet in a thread is no allow?
    By lanmanck in forum Qt Programming
    Replies: 8
    Last Post: 25th October 2009, 03:05
  3. Replies: 16
    Last Post: 7th October 2009, 08:17
  4. update widget from separate thread
    By method in forum Qt Programming
    Replies: 5
    Last Post: 10th July 2009, 14:33
  5. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 22: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
  •  
Qt is a trademark of The Qt Company.