Object :: connect:没有这样的插槽AllWidgets :: m_pSpinBoxOut-> setText

时间:2013-02-18 20:34:56

标签: qt cmake signals-slots

编译cpp文件时收到以下错误:

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网站上显示它有它...

我还尝试按如下方式更改此第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")

我做错了什么?

1 个答案:

答案 0 :(得分: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来执行此操作,而无需额外的插槽。