Qt程序錯誤“QObject::connect: Cannot queue arguments of type ‘QTextCursor’”的解決方法
原創文章,轉載請注明: 轉載自
勤奮的小青蛙
本文鏈接地址: Qt程序錯誤“QObject::connect: Cannot queue arguments of type ‘QTextCursor’”的解決方法
本文鏈接地址: Qt程序錯誤“QObject::connect: Cannot queue arguments of type ‘QTextCursor’”的解決方法
1. 運行情景
當我在一個窗口中添加了 QTextEdit 控件,並在一個工作者線程中直接調用了 QTextEdit 的append函數,這個時候就會出現下面的錯誤:
1
2
3
|
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
|
2. 解決方法
經過進一步檢查發現原因是Qt中帶參數的信號如果在線程中被發送,那么必須放入隊列里面,由於QTextEdit是Qt庫自帶的,改起來不方便,所以我采用了一個簡單的方法來解決這個問題,原理是在窗口類中定義信號和槽,並實現另一個接口函數,這個接口函數由線程調用,在接口函數中emit一個信號,示例代碼如下:
1)聲明信號和函數
1
2
3
4
|
signals:
void
AppendText(
const
QString &text);
private
slots:
void
SlotAppendText(
const
QString &text);
|
2)聲明接口函數
1
2
|
public
:
void
Append(
const
QString &text);
|
3)在類構造函數中連接信號與槽
1
|
connect(
this
,SIGNAL(AppendText(QString)),
this
,SLOT(SlotAppendText(QString)));
|
4)實現接口函數
1
2
3
4
|
void
ClassName::Append(
const
QString &text)
{
emit AppendText(
"ok: string1"
);
}
|
5)實現槽函數
1
2
3
4
|
void
CIspWnd::SlotAppendText(
const
QString &text)
{
mText.append(text);
}
|
小結:不帶參數的信號在Qt工作者線程中被發送即不會出現這類錯誤。