Qt學習:線程間共享數據(使用信號槽傳遞數據,必須提前使用qRegisterMetaType來注冊參數的類型)


Qt線程間共享數據主要有兩種方式:

  • 使用共享內存。即使用一個兩個線程都能夠共享的變量(如全局變量),這樣兩個線程都能夠訪問和修改該變量,從而達到共享數據的目的;
  • 使用singal/slot機制,把數據從一個線程傳遞到另外一個線程。

第一種辦法在各個編程語言都使用普遍,而第二種方式倒是QT的特有方式,下面主要學習一下這種方式:

        在線程之間傳遞signal與在一個線程內傳遞signal是不一樣的。在一個線程內傳遞signal時,emit語句會直接調用所有連接的slot並等待到所有slot被處理完;在線程之間傳遞signal時,slot會被放到隊列中(queue),而emit這個signal后會馬上返回;默認情況,線程之間使用queue機制,而線程內使用direct機制,但在connect中可以改變這些默認的機制。

 

[c-sharp]  view plain copy
 
  1. //TextDevice.h  
  2. #ifndef TEXTDEVICE_H  
  3. #define TEXTDEVICE_H  
  4. #include <QThread>  
  5. #include <QString>  
  6. #include <QMutex>  
  7. class TextDevice : public QThread {  
  8.     Q_OBJECT  
  9. public:  
  10.     TextDevice();  
  11.     void run();  
  12.     void stop();  
  13. public slots:  
  14.     void write(const QString& text);  
  15. private:  
  16.     int m_count;  
  17.     QMutex m_mutex;  
  18. };  
  19. #endif // TEXTDEVICE_H  
  20.   
  21.   
  22. //TextDevice.cpp  
  23. #include <QMutexLocker>  
  24. #include <QDebug>  
  25. #include <QString>  
  26. #include "TextDevice.h"  
  27. TextDevice::TextDevice() {  
  28.     m_count = 0;  
  29. }  
  30. void TextDevice::run() {  
  31.     exec();  
  32. }  
  33. void TextDevice::stop() {  
  34.     quit();  
  35. }  
  36. void TextDevice::write(const QString& text) {  
  37.     QMutexLocker locker(&m_mutex);  
  38.     qDebug() << QString("Call %1: %2").arg(m_count++).arg(text);  
  39. }  
  40.   
  41. //TextThread.h  
  42. #ifndef TEXTTHREAD_H  
  43. #define TEXTTHREAD_H  
  44. #include <QThread>  
  45. #include <QString>  
  46. class TextThread : public QThread {  
  47.     Q_OBJECT  
  48. public:  
  49.     TextThread(const QString& text);  
  50.     void run();  
  51.     void stop();  
  52. signals:  
  53.     void writeText(const QString&);  
  54. private:  
  55.     QString m_text;  
  56.     bool m_stop;  
  57. };  
  58. #endif // TEXTTHREAD_H  
  59.   
  60. //TextThread.cpp  
  61. #include "TextThread.h"  
  62. TextThread::TextThread(const QString& text) : QThread() {  
  63.     m_text = text;  
  64.     m_stop = false;  
  65. }  
  66. void TextThread::stop() {  
  67.     m_stop = true;  
  68. }  
  69. void TextThread::run() {  
  70.     while(!m_stop) {  
  71.         emit writeText(m_text);  
  72.         sleep(1);  
  73.     }  
  74. }  
  75.   
  76. //main.cpp  
  77. #include <QApplication>  
  78. #include <QMessageBox>  
  79. #include "TextDevice.h"  
  80. #include "TextThread.h"  
  81.   
  82. int main(int argc, char** argv) {  
  83.     QApplication app(argc, argv);  
  84.     //啟動線程  
  85.     TextDevice device;  
  86.     TextThread foo("foo"), bar("bar");  
  87.     //把兩個線程使用signal/slot連接起來  
  88.     QObject::connect(&foo, SIGNAL(writeText(const QString&)), &device, SLOT(write(const QString&)));  
  89.     QObject::connect(&bar, SIGNAL(writeText(const QString&)), &device, SLOT(write(const QString&)));  
  90.     //啟動線程  
  91.     foo.start();  
  92.     bar.start();  
  93.     device.start();  
  94.     QMessageBox::information(0, "Threading", "Close me to stop.");  
  95.     //停止線程  
  96.     foo.stop();  
  97.     bar.stop();  
  98.     device.stop();  
  99.     //等待線程結束  
  100.     device.wait();  
  101.     foo.wait();  
  102.     bar.wait();  
  103.     return 0;  
  104. }  

 

 

上面例子代碼可以看出兩個線程之間傳送了類型為QString的信息。像QString等這些QT本身定義的類型,直接傳送即可。但如果是自己定義的類型如果想使用signal/slot來傳遞的話,則沒有這么簡單。直接使用的話,會產生下面這種錯誤:
          QObject::connect: Cannot queue arguments of type 'TextAndNumber' (Make sure 'TextAndNumber' is registed using qRegisterMetaType().)
         原因:當一個signal被放到隊列中(queued)時,它的參數(arguments)也會被一起一起放到隊列中(queued起來),這就意味着參數在被傳送到slot之前需要被拷貝、存儲在隊列中(queue)中;為了能夠在隊列中存儲這些參數(argument),Qt需要去construct、destruct、copy這些對象,而為了讓Qt知道怎樣去作這些事情,參數的類型需要使用qRegisterMetaType來注冊(如錯誤提示中的說明)

        步驟:(以自定義TextAndNumber類型為例)

  • 自定一種類型,在這個類型的頂部包含:#include <QMetaType>
  • 在類型定義完成后,加入聲明:Q_DECLARE_METATYPE(TextAndNumber);
  • 在main()函數中注冊這種類型:qRegisterMetaType<TextAndNumber>("TextAndNumber");
  • 如果還希望使用這種類型的引用,可同樣要注冊:qRegisterMetaType<TextAndNumber>("TextAndNumber&");

 

[cpp]  view plain copy
 
  1. //TextAndNumber.h  
  2. #ifndef TEXTANDNUMBER_H  
  3. #define TEXTANDNUMBER_H  
  4. #include <QMetaType>  
  5. //必須包含QMetaType,否則會出現下面錯誤:  
  6. //error: expected constructor, destructor, or type conversion before ‘;’ token  
  7. #include <QString>  
  8. class TextAndNumber {  
  9. public:  
  10.     TextAndNumber();  
  11.     TextAndNumber(int, QString);  
  12.     int count();  
  13.     QString text();  
  14. private:  
  15.     int m_count;  
  16.     QString m_text;  
  17. };  
  18. Q_DECLARE_METATYPE(TextAndNumber);  
  19. #endif // TEXTANDNUMBER_H  
  20.   
  21. //TextAndNumber.cpp  
  22. #include "TextAndNumber.h"  
  23. TextAndNumber::TextAndNumber() {  
  24. }  
  25. TextAndNumber::TextAndNumber(int count, QString text) {  
  26.     m_count = count;  
  27.     m_text = text;  
  28. }  
  29. int TextAndNumber::count() {  
  30.     return m_count;  
  31. }  
  32. QString TextAndNumber::text() {  
  33.     return m_text;  
  34. }  
  35.   
  36. //TextDevice.h  
  37. #ifndef TEXTDEVICE_H  
  38. #define TEXTDEVICE_H  
  39. #include <QThread>  
  40. #include <QDebug>  
  41. #include <QString>  
  42. #include "TextAndNumber.h"  
  43. class TextDevice : public QThread {  
  44.     Q_OBJECT  
  45. public:  
  46.     TextDevice();  
  47.     void run();  
  48.     void stop();  
  49. public slots:  
  50.     void write(TextAndNumber& tran);  
  51. private:  
  52.     int m_count;  
  53. };  
  54.   
  55. #endif // TEXTDEVICE_H  
  56.   
  57. //TextDevice.cpp  
  58. #include "TextDevice.h"  
  59. TextDevice::TextDevice() : QThread() {  
  60.     m_count = 0;  
  61. }  
  62. void TextDevice::run() {  
  63.     exec();  
  64. }  
  65. void TextDevice::stop() {  
  66.     quit();  
  67. }  
  68. void TextDevice::write(TextAndNumber& tran) {  
  69.     qDebug() << QString("Call %1 (%3): %2").arg(m_count++).arg(tran.text()).arg(tran.count());  
  70. }  
  71.   
  72. //TextThread.h  
  73. #ifndef TEXTTHREAD_H  
  74. #define TEXTTHREAD_H  
  75. #include <QThread>  
  76. #include <QString>  
  77. #include "TextAndNumber.h"  
  78. class TextThread : public QThread {  
  79.     Q_OBJECT  
  80. public:  
  81.     TextThread(const QString& text);  
  82.     void run();  
  83.     void stop();  
  84. signals:  
  85.     void writeText(TextAndNumber& tran);  
  86. private:  
  87.     QString m_text;  
  88.     int m_count;  
  89.     bool m_stop;  
  90. };  
  91.   
  92. #endif // TEXTTHREAD_H  
  93.   
  94. //TextThread.cpp  
  95. #include "TextThread.h"  
  96. TextThread::TextThread(const QString& text) : QThread() {  
  97.     m_text = text;  
  98.     m_stop = false;  
  99.     m_count = 0;  
  100. }  
  101. void TextThread::run() {  
  102.     while(!m_stop) {  
  103.         TextAndNumber tn(m_count++, m_text);  
  104.         emit writeText(tn);  
  105.         sleep(1);  
  106.     }  
  107. }  
  108. void TextThread::stop() {  
  109.     m_stop = true;  
  110. }  
  111.   
  112. //main.cpp  
  113. #include <QApplication>  
  114. #include <QMessageBox>  
  115. #include "TextThread.h"  
  116. #include "TextDevice.h"  
  117. #include "TextAndNumber.h"  
  118. int main(int argc, char *argv[])  
  119. {  
  120.     QApplication app(argc, argv);  
  121.     qRegisterMetaType<TextAndNumber>("TextAndNumber");  
  122.     qRegisterMetaType<TextAndNumber>("TextAndNumber&");  
  123.     TextDevice device;  
  124.     TextThread foo("foo"), bar("bar");  
  125.     QObject::connect(&foo, SIGNAL(writeText(TextAndNumber&)), &device, SLOT(write(TextAndNumber&)));  
  126.     QObject::connect(&bar, SIGNAL(writeText(TextAndNumber&)), &device, SLOT(write(TextAndNumber&)));  
  127.     device.start();  
  128.     foo.start();  
  129.     bar.start();  
  130.     QMessageBox::information(0, "Threading", "Click me to close");  
  131.   
  132.     foo.stop();  
  133.     bar.stop();  
  134.     device.stop();  
  135.     foo.wait();  
  136.     bar.wait();  
  137.     device.wait();  
  138.     qDebug() << "Application end.";  
  139.     return 0;  
  140. }  

 

 http://blog.csdn.net/swingline/article/details/5635288


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM