Object::connect: Cannot queue arguments of type 'QMap '(要使用qRegisterMetaType 進行注冊)


   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


免責聲明!

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



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