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);
}