Qt学习:线程间共享数据(使用信号槽传递数据,必须提前使用qRegisterMetaType来注册参数的类型)

时间:2024-09-04 12:05:26

Qt线程间共享数据主要有两种方式:

  • 使用共享内存。即使用一个两个线程都能够共享的变量(如全局变量),这样两个线程都能够访问和修改该变量,从而达到共享数据的目的;
  • 使用singal/slot机制,把数据从一个线程传递到另外一个线程。

第一种办法在各个编程语言都使用普遍,而第二种方式倒是QT的特有方式,下面主要学习一下这种方式:

在线程之间传递signal与在一个线程内传递signal是不一样的。在一个线程内传递signal时,emit语句会直接调用所有连接的slot并等待到所有slot被处理完;在线程之间传递signal时,slot会被放到队列中(queue),而emit这个signal后会马上返回;默认情况,线程之间使用queue机制,而线程内使用direct机制,但在connect中可以改变这些默认的机制。

  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. //TextDevice.cpp
  21. #include <QMutexLocker>
  22. #include <QDebug>
  23. #include <QString>
  24. #include "TextDevice.h"
  25. TextDevice::TextDevice() {
  26. m_count = 0;
  27. }
  28. void TextDevice::run() {
  29. exec();
  30. }
  31. void TextDevice::stop() {
  32. quit();
  33. }
  34. void TextDevice::write(const QString& text) {
  35. QMutexLocker locker(&m_mutex);
  36. qDebug() << QString("Call %1: %2").arg(m_count++).arg(text);
  37. }
  38. //TextThread.h
  39. #ifndef TEXTTHREAD_H
  40. #define TEXTTHREAD_H
  41. #include <QThread>
  42. #include <QString>
  43. class TextThread : public QThread {
  44. Q_OBJECT
  45. public:
  46. TextThread(const QString& text);
  47. void run();
  48. void stop();
  49. signals:
  50. void writeText(const QString&);
  51. private:
  52. QString m_text;
  53. bool m_stop;
  54. };
  55. #endif // TEXTTHREAD_H
  56. //TextThread.cpp
  57. #include "TextThread.h"
  58. TextThread::TextThread(const QString& text) : QThread() {
  59. m_text = text;
  60. m_stop = false;
  61. }
  62. void TextThread::stop() {
  63. m_stop = true;
  64. }
  65. void TextThread::run() {
  66. while(!m_stop) {
  67. emit writeText(m_text);
  68. sleep(1);
  69. }
  70. }
  71. //main.cpp
  72. #include <QApplication>
  73. #include <QMessageBox>
  74. #include "TextDevice.h"
  75. #include "TextThread.h"
  76. int main(int argc, char** argv) {
  77. QApplication app(argc, argv);
  78. //启动线程
  79. TextDevice device;
  80. TextThread foo("foo"), bar("bar");
  81. //把两个线程使用signal/slot连接起来
  82. QObject::connect(&foo, SIGNAL(writeText(const QString&)), &device, SLOT(write(const QString&)));
  83. QObject::connect(&bar, SIGNAL(writeText(const QString&)), &device, SLOT(write(const QString&)));
  84. //启动线程
  85. foo.start();
  86. bar.start();
  87. device.start();
  88. QMessageBox::information(0, "Threading", "Close me to stop.");
  89. //停止线程
  90. foo.stop();
  91. bar.stop();
  92. device.stop();
  93. //等待线程结束
  94. device.wait();
  95. foo.wait();
  96. bar.wait();
  97. return 0;
  98. }

上面例子代码可以看出两个线程之间传送了类型为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&");
  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. //TextAndNumber.cpp
  21. #include "TextAndNumber.h"
  22. TextAndNumber::TextAndNumber() {
  23. }
  24. TextAndNumber::TextAndNumber(int count, QString text) {
  25. m_count = count;
  26. m_text = text;
  27. }
  28. int TextAndNumber::count() {
  29. return m_count;
  30. }
  31. QString TextAndNumber::text() {
  32. return m_text;
  33. }
  34. //TextDevice.h
  35. #ifndef TEXTDEVICE_H
  36. #define TEXTDEVICE_H
  37. #include <QThread>
  38. #include <QDebug>
  39. #include <QString>
  40. #include "TextAndNumber.h"
  41. class TextDevice : public QThread {
  42. Q_OBJECT
  43. public:
  44. TextDevice();
  45. void run();
  46. void stop();
  47. public slots:
  48. void write(TextAndNumber& tran);
  49. private:
  50. int m_count;
  51. };
  52. #endif // TEXTDEVICE_H
  53. //TextDevice.cpp
  54. #include "TextDevice.h"
  55. TextDevice::TextDevice() : QThread() {
  56. m_count = 0;
  57. }
  58. void TextDevice::run() {
  59. exec();
  60. }
  61. void TextDevice::stop() {
  62. quit();
  63. }
  64. void TextDevice::write(TextAndNumber& tran) {
  65. qDebug() << QString("Call %1 (%3): %2").arg(m_count++).arg(tran.text()).arg(tran.count());
  66. }
  67. //TextThread.h
  68. #ifndef TEXTTHREAD_H
  69. #define TEXTTHREAD_H
  70. #include <QThread>
  71. #include <QString>
  72. #include "TextAndNumber.h"
  73. class TextThread : public QThread {
  74. Q_OBJECT
  75. public:
  76. TextThread(const QString& text);
  77. void run();
  78. void stop();
  79. signals:
  80. void writeText(TextAndNumber& tran);
  81. private:
  82. QString m_text;
  83. int m_count;
  84. bool m_stop;
  85. };
  86. #endif // TEXTTHREAD_H
  87. //TextThread.cpp
  88. #include "TextThread.h"
  89. TextThread::TextThread(const QString& text) : QThread() {
  90. m_text = text;
  91. m_stop = false;
  92. m_count = 0;
  93. }
  94. void TextThread::run() {
  95. while(!m_stop) {
  96. TextAndNumber tn(m_count++, m_text);
  97. emit writeText(tn);
  98. sleep(1);
  99. }
  100. }
  101. void TextThread::stop() {
  102. m_stop = true;
  103. }
  104. //main.cpp
  105. #include <QApplication>
  106. #include <QMessageBox>
  107. #include "TextThread.h"
  108. #include "TextDevice.h"
  109. #include "TextAndNumber.h"
  110. int main(int argc, char *argv[])
  111. {
  112. QApplication app(argc, argv);
  113. qRegisterMetaType<TextAndNumber>("TextAndNumber");
  114. qRegisterMetaType<TextAndNumber>("TextAndNumber&");
  115. TextDevice device;
  116. TextThread foo("foo"), bar("bar");
  117. QObject::connect(&foo, SIGNAL(writeText(TextAndNumber&)), &device, SLOT(write(TextAndNumber&)));
  118. QObject::connect(&bar, SIGNAL(writeText(TextAndNumber&)), &device, SLOT(write(TextAndNumber&)));
  119. device.start();
  120. foo.start();
  121. bar.start();
  122. QMessageBox::information(0, "Threading", "Click me to close");
  123. foo.stop();
  124. bar.stop();
  125. device.stop();
  126. foo.wait();
  127. bar.wait();
  128. device.wait();
  129. qDebug() << "Application end.";
  130. return 0;
  131. }

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