2013-02-18 49 views
2

我收到以下錯誤而編譯我的CPP文件:對象::連接:沒有這樣的插槽AllWidgets :: m_pSpinBoxOut->的setText

Object::connect: No such slot AllWidgets::m_pSpinBoxOut->setText(const QString &) in Widgets.cpp:148 

這裏是行148:

connect(m_pSpinBox,SIGNAL(valueChanged(double)),this,SLOT(m_pSpinBoxOut->setText(const QString &))); 

的第一個m_pSpinBox只是一個SpinBox,沒有問題,但它說m_pSpinBoxOut(這是一個QLabel)沒有setText插槽...實際上在QT網站上顯示它有它...

我也試圖侃侃而談ge這行148如下:

connect(m_pSpinBox,SIGNAL(valueChanged(double)),m_pSpinBoxOut,SLOT(setText("demo"))); 

connect(m_pSpinBox,SIGNAL(valueChanged(double)),m_pSpinBoxOut,SLOT(QLabel::setText("demo"))) 

connect(m_pSpinBox,SIGNAL(valueChanged(double)),m_pSpinBoxOut,SLOT(QString::setText("demo"))); 

沒有什麼,但警告消息更改。分別是:

Object::connect: No such slot QLabel::setText("demo") 
Object::connect: No such slot QLabel::QLabel::setText("demo") 
Object::connect: No such slot QLabel::QString::setText("demo") 

我做錯了什麼?

回答

4
connect(m_pSpinBox,SIGNAL(valueChanged(double)), 
     m_pSpinBoxOut,SLOT(setText(const QString&))); 

SLOT必須的名稱和接收方法的指定參數時,該this擁有m_pSpinBoxOut事實是無關緊要。此外,arg聲明不能包含表達式(即QLabel::setText("demo"))。

我還應該指出,這個連接無法工作,因爲double不能隱式轉換爲QString。所以,你必須創建一個轉換插槽:

connect(m_pSpinBox,SIGNAL(valueChanged(double)), 
     this,SLOT(converterSlot(double))); 
... 
AllWidgets::converterSlot(double number) 
{ 
    m_pSpinBoxOut->setText(QString::number(number)); 
} 

如果您使用的是Qt 5您也可以使用一個lambda這樣做沒有額外的插槽。

+0

謝謝,我試過後會通知你。 – www 2013-02-18 21:09:59

+1

強烈推薦閱讀[documentation](http://qt-project.org/doc/qt-4.8/signalsandslots.html)。僅通過反覆試驗即可獲得信號,插槽和連接權限,需要比閱讀文檔更多的工作。 – 2013-02-18 21:13:01

+0

@ user1754665我已經添加了一個示例。 – cmannett85 2013-02-22 08:41:49

相關問題