偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

實(shí)現(xiàn)QT元類型和QT線程通信

移動(dòng)開(kāi)發(fā)
本文介紹的是實(shí)現(xiàn)QT元類型和QT線程通信,文中詳細(xì)介紹了如何實(shí)現(xiàn),我們先來(lái)看內(nèi)容。

實(shí)現(xiàn)QT元類型QT線程通信是本文將要介紹的內(nèi)容,不多說(shuō)廢話,先來(lái)看內(nèi)容。今天調(diào)試QT線程通信的程序時(shí),突然發(fā)現(xiàn)如下消息:

實(shí)現(xiàn)QT元類型和QT線程通信

其中PEOPLE只是我定義的枚舉類型即enum PEOPLE。然后在Qt的信號(hào)-槽函數(shù)的參數(shù)中使用了這個(gè)枚舉型,在發(fā)送信號(hào)時(shí)就出現(xiàn)了上述警告。上面警告的大概意思是信號(hào)隊(duì)列中無(wú)法使用PEOPLE類型,要使用qRegisterMetaType()注冊(cè)該類型后方可使用。

通常使用的connect,實(shí)際上最后一個(gè)參數(shù)使用的是Qt::AutoConnection類型:(友們,點(diǎn)擊之后,就會(huì)放大,不好意思,影響你視覺(jué)了)

實(shí)現(xiàn)QT元類型和QT線程通信

Qt支持6種連接方式,其中3中最主要:

Qt::DirectConnection(直連方式)

當(dāng)信號(hào)發(fā)出后,相應(yīng)的槽函數(shù)將立即被調(diào)用。emit語(yǔ)句后的代碼將在所有槽函數(shù)執(zhí)行完畢后被執(zhí)行。(信號(hào)與槽函數(shù)關(guān)系類似于函數(shù)調(diào)用,同步執(zhí)行)

Qt::QueuedConnection(排隊(duì)方式)

當(dāng)信號(hào)發(fā)出后,排隊(duì)到信號(hào)隊(duì)列中,需等到接收對(duì)象所屬線程的事件循環(huán)取得控制權(quán)時(shí)才取得該信號(hào),調(diào)用相應(yīng)的槽函數(shù)。emit語(yǔ)句后的代碼將在發(fā)出信號(hào)后立即被執(zhí)行,無(wú)需等待槽函數(shù)執(zhí)行完畢。(此時(shí)信號(hào)被塞到信號(hào)隊(duì)列里了,信號(hào)與槽函數(shù)關(guān)系類似于消息通信,異步執(zhí)行)

Qt::AutoConnection(自動(dòng)方式)

Qt的默認(rèn)連接方式,如果信號(hào)的發(fā)出和接收這個(gè)信號(hào)的對(duì)象同屬一個(gè)線程,那個(gè)工作方式與直連方式相同;否則工作方式與排隊(duì)方式相同。

我的項(xiàng)目中的確跨線程使用了PEOPLE為參數(shù)類型的信號(hào),因此使用的應(yīng)當(dāng)是排隊(duì)方式的信號(hào)-槽機(jī)制,出現(xiàn)“隊(duì)列中無(wú)法使用PEOPLE類型”的警告信息就可以理解了。放狗搜了一圈,有篇文章提供了個(gè)這樣的解決方案:

  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)));  

 

這樣做的確能使警告信息消失,因?yàn)镼t官方文檔寫(xiě)了:

  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. 

即使用排隊(duì)方式的信號(hào)-槽機(jī)制,Qt的元對(duì)象系統(tǒng)(meta-object system)必須知道信號(hào)傳遞的參數(shù)類型。這里手動(dòng)改為直連方式,Qt的元對(duì)象系統(tǒng)就不必知道參數(shù)類型了,于是警告信息消失。但這樣做是不安全的,見(jiàn)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. 

因此,咱還是老老實(shí)實(shí)地用qRegisterMetaType()注冊(cè)類型吧

我寫(xiě)的線程通訊方法是采用信號(hào)槽機(jī)制,通常情況下,信號(hào)和槽機(jī)制可以同步操作,這就意味著在發(fā)射信號(hào)的時(shí)候,使用直接函數(shù)即可以立刻調(diào)用連接到一個(gè)信號(hào)上的多個(gè)槽。然而,當(dāng)連接位于不同線程中的對(duì)象時(shí),這一機(jī)制就會(huì)變得不同步起來(lái),可以通過(guò)剛才介紹的,修改QObject::connect()的第5個(gè)可選參數(shù)而改變。

connect的第五個(gè)參數(shù)Qt::QueuedConnection表示槽函數(shù)由接受信號(hào)的線程所執(zhí)行,如果不加表示槽函數(shù)由發(fā)出信號(hào)的次線程執(zhí)行。當(dāng)傳遞信號(hào)的參數(shù)類型不是QT的元類型時(shí)要先注冊(cè),關(guān)于QT的元類型可以參看QT文檔。

QMetaType這個(gè)類里面列舉了所有的元類型。

以枚舉PEOPLE為例,注冊(cè)時(shí)首先Q_DECLARE_METATYPE(PEOPLE);

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

加上這兩句就注冊(cè)成功了。

#p#

貼個(gè)示例的代碼,次線程不斷更改一個(gè)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. }  

小結(jié):實(shí)現(xiàn)QT元類型QT線程通信的內(nèi)容到這就介紹完了,希望本文能幫你解決問(wèn)題。

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-06-22 14:04:33

Qt 元類型 注冊(cè)

2011-06-13 17:46:07

Qt 串口通信

2011-06-23 13:38:27

QT 元對(duì)象 信號(hào)

2011-06-22 10:12:08

Qt 線程

2011-06-27 11:08:37

Qt 串口 通信

2011-06-14 09:46:11

Qt QThread 線程

2011-06-30 09:46:01

QT 顯示視頻 linux

2011-06-20 13:43:08

Qt Socket 線程

2011-06-22 17:09:50

QT 進(jìn)程 通信

2011-06-22 15:50:45

QT 線程

2011-06-22 15:09:34

Qt 線程 sleep

2011-06-13 10:44:44

Qt Flash

2011-06-22 16:18:23

QT 多線程 QSocket

2011-06-22 16:50:09

Qt 進(jìn)程 通信機(jī)制

2011-06-22 17:27:19

QT 進(jìn)程通信

2011-06-22 15:24:50

Qt 線程

2011-06-13 10:03:19

Qt 多線程 編程

2011-07-01 13:03:32

QT 線程 串口

2011-06-30 11:23:29

Qt 線程

2011-06-29 16:34:11

Qt 子線程 線程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)