对象::连接:没有这样的插槽AllWidgets :: m_pSpinBoxOut->的setText

问题描述:

我收到以下错误而编译我的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") 

我做错了什么?

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