实现QT元类型和QT线程通信
创始人
2024-08-02 06:01:38
0

实现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");

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

#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      
  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  
  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      
  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  
  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");     
  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线程通信的内容到这就介绍完了,希望本文能帮你解决问题。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...