QObject::connect: Cannot queue arguments of type 'QMap<QString,QString>',(Make sure 'QMap<QString,QString>' is registered using qRegisterMetaType().).
上述錯誤,只有在跨線程信號傳遞時才會出現. 因為QMap是QT可識別的基本類型,不需要再注冊元對象系統中,在同一個線程中運行沒有問題.
源碼:
- // 線程類 thread.h
- class Thread:public QThread
- {
- Q_OBJECT
- public:
- Thread(){}
- ~Thread(){}
- protected:
- virtual void run();
- signals:
- void sendMsg(const QMap<QString,QString> &msgs);
- }
- // 信號接收類 test.h
- Test(Thread *th):m_th(th)
- {
- // 不同線程用隊列方式連接
- connect(m_th,SIGNAL(sendMsg(const QMap<QString,QString> &)),this,SLOT(handle(const QMap<QString,QString> &)),Qt::QueuedConnection);
- }
解決方案:通過qRegisterMetaType()方法注冊至Metype中
- // thread.h
- typedef QMap<QString,QString> StringMap; // typedef操作符為QMap起一別名
- void sendMsg(const StringMap &);
- // test.h
- Test(Thread *th):m_th(th)
- {
- // 注冊QMap至元對象系統
- qRegisterMetaType<StringMap>("StringMap");
- connect(m_th,SIGNAL(sendMsg(const StringMap &)),this,SLOT(handle(const StringMap &)),Qt::QueuedConnection);
- }
http://tcspecial.iteye.com/blog/1897006

