1

All i want to do is call a method when the value of a qspinbox and a doublespinbox are changed.

I do not need the actual value from the spinbox being changed, i just want it to trigger the calling of another method. Why does the code below not error or do anything at all? Not even call the method?

cpp

connect(uiSpinBox, SIGNAL(valueChanged()), this, SLOT(slotInputChanged));
connect(uiDoubleSpinBox, SIGNAL(valueChanged()), this, SLOT(slotInputChanged));

void ColorSwatchEdit::slotInputChanged()
{
    qDebug() << "Im here";
}

header

public:
    QSpinBox *uiSpinBox;
    QDoubleSpinBox *uiDoubleSpinBox;

public slots:
    void slotInputChanged();
4

2 回答 2

4

即使您不使用携带信号的数据,您也必须在连接中建立签名:

connect(uiSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotInputChanged)); 
connect(uiDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(slotInputChanged));

但建议您使用新的连接语法,因为它会指示错误:

connect(uiSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &ColorSwatchEdit::slotInputChanged); 
connect(uiDoubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ColorSwatchEdit::slotInputChanged);
于 2019-09-02T15:11:19.353 回答
3

除了eyllanesc 的回答,如果可能的话,考虑使用 FunctionPointer 语法,即

connect(uiSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &YourClass::slotInputChanged)

connect(uiDoubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YourClass::slotInputChanged)

这样编译器可以在编译时告诉你连接是否无法解析

于 2019-09-02T15:12:52.783 回答