实现QT元类型和QT线程通信

移动开发
本文介绍的是实现QT元类型和QT线程通信,文中详细介绍了如何实现,我们先来看内容。

实现QT元类型QT线程通信是本文将要介绍的内容,不多说废话,先来看内容。今天调试QT线程通信的程序时,突然发现如下消息:

实现QT元类型和QT线程通信

其中PEOPLE只是我定义的枚举类型即enum PEOPLE。然后在Qt的信号-槽函数的参数中使用了这个枚举型,在发送信号时就出现了上述警告。上面警告的大概意思是信号队列中无法使用PEOPLE类型,要使用qRegisterMetaType()注册该类型后方可使用。

通常使用的connect,实际上最后一个参数使用的是Qt::AutoConnection类型:(友们,点击之后,就会放大,不好意思,影响你视觉了)

实现QT元类型和QT线程通信

Qt支持6种连接方式,其中3中最主要:

Qt::DirectConnection(直连方式)

当信号发出后,相应的槽函数将立即被调用。emit语句后的代码将在所有槽函数执行完毕后被执行。(信号与槽函数关系类似于函数调用,同步执行)

Qt::QueuedConnection(排队方式)

当信号发出后,排队到信号队列中,需等到接收对象所属线程的事件循环取得控制权时才取得该信号,调用相应的槽函数。emit语句后的代码将在发出信号后立即被执行,无需等待槽函数执行完毕。(此时信号被塞到信号队列里了,信号与槽函数关系类似于消息通信,异步执行)

Qt::AutoConnection(自动方式)

Qt的默认连接方式,如果信号的发出和接收这个信号的对象同属一个线程,那个工作方式与直连方式相同;否则工作方式与排队方式相同。

我的项目中的确跨线程使用了PEOPLE为参数类型的信号,因此使用的应当是排队方式的信号-槽机制,出现“队列中无法使用PEOPLE类型”的警告信息就可以理解了。放狗搜了一圈,有篇文章提供了个这样的解决方案:

  1. connect(cm, SIGNAL(sendLog(QUuid, QByteArray, bool)),   
  2.             this,SLOT(sendRes(QUuid,QByteArray,bool)));  

改为:

  1. connect(cm, SIGNAL(sendLog(QUuid, QByteArray, bool)),   
  2.             this,SLOT(sendRes(QUuid,QByteArray,bool)));  

 

这样做的确能使警告信息消失,因为Qt官方文档写了:

  1. With queued connections, the parameters must be of types that are known to Qt's meta-object system,   
  2. because Qt needs to copy the arguments to store them in an event behind the scenes. 

即使用排队方式的信号-槽机制,Qt的元对象系统(meta-object system)必须知道信号传递的参数类型。这里手动改为直连方式,Qt的元对象系统就不必知道参数类型了,于是警告信息消失。但这样做是不安全的,见Qt官方文档:

  1. Be aware that using direct connections when the sender and receiver live in different threads is unsafe if   
  2. an event loop is running in the receiver's thread, for the same reason that calling any function on an obje  
  3. ct living in another thread is unsafe. 

因此,咱还是老老实实地用qRegisterMetaType()注册类型吧

我写的线程通讯方法是采用信号槽机制,通常情况下,信号和槽机制可以同步操作,这就意味着在发射信号的时候,使用直接函数即可以立刻调用连接到一个信号上的多个槽。然而,当连接位于不同线程中的对象时,这一机制就会变得不同步起来,可以通过刚才介绍的,修改QObject::connect()的第5个可选参数而改变。

connect的第五个参数Qt::QueuedConnection表示槽函数由接受信号的线程所执行,如果不加表示槽函数由发出信号的次线程执行。当传递信号的参数类型不是QT的元类型时要先注册,关于QT的元类型可以参看QT文档。

QMetaType这个类里面列举了所有的元类型。

以枚举PEOPLE为例,注册时首先Q_DECLARE_METATYPE(PEOPLE);

然后,int id=qRegisterMetaType<PEOPLE>("PEOPLE");

加上这两句就注册成功了。

#p#

贴个示例的代码,次线程不断更改一个PEOPLE{boy,girl}的信息传给GUI主线程,主线程在GUI界面上显示。

  1. mythread.h  
  2.  
  3. view plaincopy to clipboardprint?  
  4. #ifndef MYTHREAD_H     
  5. #define MYTHREAD_H     
  6. #include <QThread>     
  7. enum PEOPLE{boy,girl};     
  8. class MyThread : public QThread     
  9. {     
  10. Q_OBJECT     
  11.     
  12. public:     
  13. MyThread();     
  14. ~MyThread();     
  15. protected:     
  16. void run();     
  17. signals:     
  18. void changeText(PEOPLE pe);     
  19. };     
  20. #endif // MYTHREAD_H    
  21. #ifndef MYTHREAD_H  
  22. #define MYTHREAD_H  
  23. #include <QThread> 
  24. enum PEOPLE{boy,girl};  
  25. class MyThread : public QThread  
  26. {  
  27. Q_OBJECT  
  28. public:  
  29. MyThread();  
  30. ~MyThread();  
  31. protected:  
  32. void run();  
  33. signals:  
  34. void changeText(PEOPLE pe);  
  35. };  
  36. #endif // MYTHREAD_H   
  37.  
  38. mainwindow.h  
  39. view plaincopy to clipboardprint?  
  40. #ifndef MAINWINDOW_H     
  41. #define MAINWINDOW_H     
  42. #include "mythread.h"     
  43. #include <QMainWindow>     
  44. namespace Ui {     
  45.     class MainWindow;     
  46. }     
  47.     
  48. class MainWindow : public QMainWindow {     
  49.     Q_OBJECT     
  50. public:     
  51.     MainWindow(QWidget *parent = 0);     
  52.     ~MainWindow();     
  53. private slots:     
  54. void labelSetText(PEOPLE qstr);     
  55. protected:     
  56.     void changeEvent(QEvent *e);     
  57. private:     
  58.     Ui::MainWindow *ui;     
  59. };     
  60. #endif // MAINWINDOW_H    
  61. #ifndef MAINWINDOW_H  
  62. #define MAINWINDOW_H  
  63. #include "mythread.h"  
  64. #include <QMainWindow> 
  65. namespace Ui {  
  66.     class MainWindow;  
  67. }  
  68. class MainWindow : public QMainWindow {  
  69.     Q_OBJECT  
  70. public:  
  71.     MainWindow(QWidget *parent = 0);  
  72.     ~MainWindow();  
  73. private slots:  
  74. void labelSetText(PEOPLE qstr);  
  75. protected:  
  76.     void changeEvent(QEvent *e);  
  77. private:  
  78.     Ui::MainWindow *ui;  
  79. };  
  80. #endif // MAINWINDOW_H   
  81.  
  82. mythread.cpp  
  83. view plaincopy to clipboardprint?  
  84. #include "mythread.h"     
  85. MyThread::MyThread()     
  86. : QThread()     
  87. {     
  88. }     
  89. MyThread::~MyThread()     
  90. {     
  91. }     
  92. void MyThread::run(){     
  93.  static int i=1;     
  94.  while(true)     
  95.  {     
  96.   if(i==1)emit changeText(boy);     
  97.   else emit changeText(girl);     
  98.   ii=i*(-1);     
  99.  QThread::sleep(1);     
  100.  }     
  101. }    
  102. #include "mythread.h"  
  103. MyThread::MyThread()  
  104. : QThread()  
  105. {  
  106. }  
  107. MyThread::~MyThread()  
  108. {  
  109. }  
  110. void MyThread::run(){  
  111.  static int i=1;  
  112.  while(true)  
  113.  {  
  114.   if(i==1)emit changeText(boy);  
  115.   else emit changeText(girl);  
  116.   ii=i*(-1);  
  117.  QThread::sleep(1);  
  118.  }  
  119. }  
  120.  
  121. mainwindow.cpp  
  122. view plaincopy to clipboardprint?  
  123. #include "mainwindow.h"     
  124. #include "ui_mainwindow.h"     
  125. #include "mythread.h"     
  126. MainWindow::MainWindow(QWidget *parent) :     
  127.     QMainWindow(parent),     
  128.     ui(new Ui::MainWindow)     
  129. {     
  130.     ui->setupUi(this);     
  131.     MyThread *mythread = new MyThread;     
  132.     int id=qRegisterMetaType<PEOPLE>("PEOPLE");     
  133.     connect(mythread,SIGNAL(changeText(PEOPLE)),this,SLOT(labelSetText(PEOPLE)),Qt::QueuedConnection);     
  134.     mythread->start();     
  135.     
  136. }     
  137. MainWindow::~MainWindow()     
  138. {     
  139.     delete ui;     
  140. }     
  141. void MainWindow::changeEvent(QEvent *e)     
  142. {     
  143.     QMainWindow::changeEvent(e);     
  144.     switch (e->type()) {     
  145.     case QEvent::LanguageChange:     
  146.         ui->retranslateUi(this);     
  147.         break;     
  148.     default:     
  149.         break;     
  150.     }     
  151. }     
  152. void MainWindow::labelSetText(PEOPLE qstr){     
  153. switch(qstr)     
  154. {     
  155. case boy:     
  156.     ui->label->setText("BOY");break;     
  157. case girl:     
  158.     ui->label->setText("GIRL");break;     
  159. }     
  160. }  

小结:实现QT元类型QT线程通信的内容到这就介绍完了,希望本文能帮你解决问题。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-06-22 14:04:33

Qt 元类型 注册

2011-06-13 17:46:07

Qt 串口通信

2011-06-22 10:12:08

Qt 线程

2011-06-23 13:38:27

QT 元对象 信号

2011-06-30 09:46:01

QT 显示视频 linux

2011-06-27 11:08:37

Qt 串口 通信

2011-06-14 09:46:11

Qt QThread 线程

2011-06-20 13:43:08

Qt Socket 线程

2011-06-22 15:09:34

Qt 线程 sleep

2011-06-22 15:50:45

QT 线程

2011-06-22 17:09:50

QT 进程 通信

2011-06-22 16:18:23

QT 多线程 QSocket

2011-06-13 10:44:44

Qt Flash

2011-06-22 16:50:09

Qt 进程 通信机制

2011-06-22 17:27:19

QT 进程通信

2011-06-30 11:23:29

Qt 线程

2011-06-30 18:15:36

Qt 线程 同步

2011-06-29 16:34:11

Qt 子线程 线程

2011-06-30 17:21:56

Qt 线程 共享

2011-06-22 15:42:18

QT 信号
点赞
收藏

51CTO技术栈公众号