標籤:tar 分享圖片 一個 public otto 方式 .com ons tools
QObject::connect: Cannot queue arguments of type ‘QMap<QString,QString>‘,(Make sure ‘QMap<QString,QString>‘ is registered using qRegisterMetaType().).
上述錯誤,只有在跨線程訊號傳遞時才會出現. 因為QMap是QT可識別的基本類型,不需要再註冊元對象系統中,在同一個線程中運行沒有問題.
源碼:
Cpp代碼
- // 線程類 thread.h
- class Thread:public QThread
- {
- Q_OBJECT
-
- public:
- Thread(){}
- ~Thread(){}
-
- protected:
- virtual void run();
-
- signals:
- void sendMsg(const QMap<QString,QString> &msgs);
- }
Cpp代碼
- // 訊號接收類 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中
Cpp代碼
- // thread.h
- typedef QMap<QString,QString> StringMap; // typedef操作符為QMap起一別名
-
- void sendMsg(const StringMap &);
Cpp代碼
- // 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
Object::connect: Cannot queue arguments of type 'QMap<QString,QString>'(要使用qRegisterMetaType<StringMap>進行註冊)