In QT multi-threaded programming, you can use signals and slots for thread communication

Source: Internet
Author: User

In QT multi-threaded programming, you can use signals and slots for thread Communication. The following is a simple example.

The program implements a thread to customize a signal and slot, timing 1 seconds to send a signal, slot response after printing a message.

[cpp] View plain copy 650) this.width=650; "src=" https://code.csdn.net/assets/CODE_ Ico.png "width=" "height=" "alt=" on code to view the chip "style=" border:none; "/>650" this.width=650; "src=" HTTPS ://code.csdn.net/assets/ico_fork.svg "width=" "height=" "alt=" derived from my Code slice "style=" border:none; "/>

  1. #include <QtCore/QCoreApplication>   

  2. #include <QThread>   

  3. #include <stdio.h>   

  4. class MyThread: public Qthread

  5. {

  6. Q_object

  7. public :  

  8. MyThread ();

  9. void  Stop ();

  10. Private :  

  11. BOOL  isrunning;

  12. void  Run ();

  13. public slots:

  14. void  showmsg ();

  15. Signals:

  16. void  msg ();

  17. };

  18. Mythread::mythread ()

  19. {

  20.      isrunning = true ;  

  21. Connect (This, SIGNAL (msg ()),This, SLOT (showmsg ()), Qt::D irectconnection);

  22. }

  23. void mythread::showmsg ()

  24. {

  25. printf ("hello!\n");

  26. }

  27. void Mythread::run ()

  28. {

  29. while (isrunning)

  30. {

  31. Sleep (1);

  32. Emit msg ();

  33. }

  34. printf ("exit!\n");

  35. }

  36. void mythread::stop ()

  37. {

  38. IsRunning = false;

  39. }

  40. int Main (int argc, char *argv[])

  41. {

  42. Qcoreapplication a (argc, argv);

  43. MyThread mthread;

  44. Mthread.start ();

  45. while (1)

  46. {

  47. if (getchar () = =' B ')

  48. {

  49. Mthread.stop ();

  50. Mthread.wait ();

  51. break  ;

  52. }

  53. }

  54. return  a.exec ();

  55. }

  56. #include "main.moc"   


When compiling in QT creator, you need to compile with "qmake" to generate the MOC File. Then compile with the build Project.

PS:QT Meta-object System


In QT multi-threaded programming, you can use signals and slots for thread communication

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.