Communication between a QT GUI (master) thread and a child thread--using a signal slot across threads

Source: Internet
Author: User
Tags emit

On the main thread, you can control the child thread start, stop, Zero

If a child thread starts, it sends a number to the main thread every second, allowing the main thread to update the number on the interface.


Program:



On the code:

[CPP]View Plaincopyprint?
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include <windows.h>
    4. Class Thread: Public qthread
    5. {
    6. Q_object
    7. Private
    8. int number;
    9. Protected
    10. void Run ();
    11. Public
    12. Thread (Qobject *parent=0);
    13. ~thread ();
    14. Signals:
    15. void updatesignal (int num);
    16. Public Slots:
    17. void Resetslot ();
    18. };
    19. Class Widget: Public qwidget
    20. {
    21. Q_object
    22. Private
    23. Qlabel *label;
    24. Qpushbutton *startbutton;
    25. Qpushbutton *stopbutton;
    26. Qpushbutton *resetbutton;
    27. Thread *mythread;
    28. int number;
    29. Public
    30. Widget (Qwidget *parent = 0);
    31. ~widget ();
    32. Signals:
    33. void Resetsignal ();
    34. Public Slots:
    35. void Clearslot ();
    36. void Startslot ();
    37. void Stopslot ();
    38. void Updateslot (int num);
    39. };
    40. Thread::thread (Qobject *parent)
    41. {
    42. Number = 0;
    43. }
    44. Thread::~thread ()
    45. {
    46. }
    47. void Thread::run ()
    48. {
    49. While (1)
    50. {
    51. //Turn on a dead loop and let number increment by 1 per second and notify the main interface update by sending updatesignal signal
    52. Emit updatesignal (number);
    53. number++;
    54. Sleep (1);
    55. }
    56. }
    57. void Thread::resetslot ()
    58. {
    59. Number = 0;
    60. Emit updatesignal (number);
    61. }
    62. Widget::widget (Qwidget *parent)
    63. {
    64. //SET interface layout
    65. Startbutton = New Qpushbutton ("start");
    66. Stopbutton = New Qpushbutton ("Stop");
    67. Resetbutton = New Qpushbutton ("reset");
    68. label = New Qlabel ("Empty");
    69. MyThread = new Thread;
    70. Qvboxlayout *layout = new Qvboxlayout;
    71. Layout->addwidget (label);
    72. Layout->addwidget (Startbutton);
    73. Layout->addwidget (Stopbutton);
    74. Layout->addwidget (Resetbutton);
    75. SetLayout (layout);
    76. //Connect the respective signal bad
    77. Connect (Stopbutton, SIGNAL (clicked ()),
    78. This , SLOT (Stopslot ()));
    79. Connect (Startbutton, SIGNAL (clicked ()),
    80. This , SLOT (Startslot ()));
    81. Connect (Resetbutton, SIGNAL (clicked ()),
    82. This , SLOT (Clearslot ()));
    83. Connect (MyThread, SIGNAL (updatesignal (int)),
    84. This , SLOT (updateslot (int)));
    85. Connect (this, SIGNAL (Resetsignal ()),
    86. MyThread, SLOT (Resetslot ()));
    87. Setwindowtitle ("Thread Test");
    88. Resize (200, 200);
    89. Mythread->start ();
    90. }
    91. Widget::~widget ()
    92. {
    93. }
    94. void Widget::startslot ()
    95. {
    96. Mythread->start ();
    97. }
    98. void Widget::stopslot ()
    99. {
    100. Mythread->terminate ();
    101. }
    102. void Widget::updateslot (int num)
    103. {
    104. Label->settext (Qstring::number (num));
    105. }
    106. void Widget::clearslot ()
    107. {
    108. Emit resetsignal ();
    109. }
    110. #include "Main.moc"
    111. int main (int argc, char **argv)
    112. {
    113. Qapplication app (argc, argv);
    114. Widget *widget = new Widget;
    115. Widget->show ();
    116. return app.exec ();
    117. }

http://blog.csdn.net/small_qch/article/details/6681907

Communication between a QT GUI (master) thread and a child thread--using a signal slot across threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.