Object::connect: Cannot queue arguments of type 'QMap<QString,QString>'(要使用qRegisterMetaType<StringMap>进行注册)

   QObject::connect: Cannot queue arguments of type 'QMap<QString,QString>',(Make sure 'QMap<QString,QString>' is registered using qRegisterMetaType().).  

   上述错误,只有在跨线程信号传递时才会出现.  因为QMap是QT可识别的基本类型,不需要再注册元对象系统中,在同一个线程中运行没有问题.

   源码: 

Cpp代码  收藏代码
  1. // 线程类 thread.h   
  2. class Thread:public QThread  
  3. {  
  4.     Q_OBJECT  
  5.   
  6. public:  
  7.     Thread(){}  
  8.     ~Thread(){}  
  9.   
  10. protected:  
  11.     virtual void run();  
  12.   
  13. signals:  
  14.     void sendMsg(const QMap<QString,QString> &msgs);  
  15. }  

  

Cpp代码  收藏代码
  1. // 信号接收类 test.h  
  2. Test(Thread *th):m_th(th)  
  3. {  
  4.   // 不同线程用队列方式连接  
  5.   connect(m_th,SIGNAL(sendMsg(const QMap<QString,QString> &)),this,SLOT(handle(const QMap<QString,QString> &)),Qt::QueuedConnection);  
  6. }  

    解决方案:通过qRegisterMetaType()方法注册至Metype中

Cpp代码  收藏代码
  1. // thread.h  
  2. typedef QMap<QString,QString> StringMap; // typedef操作符为QMap起一别名  
  3.   
  4. void sendMsg(const StringMap &);  
Cpp代码  收藏代码
  1. // test.h   
  2. Test(Thread *th):m_th(th)  
  3. {  
  4.     // 注册QMap至元对象系统  
  5.     qRegisterMetaType<StringMap>("StringMap");  
  6.     connect(m_th,SIGNAL(sendMsg(const StringMap &)),this,SLOT(handle(const StringMap &)),Qt::QueuedConnection);  
  7. }  

     

 
 
http://tcspecial.iteye.com/blog/1897006
原文地址:https://www.cnblogs.com/findumars/p/9589328.html