Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Default Parameters for function called by Signal

I receive this error when I try to connect a function with one default parameter and one non-default parameter to a QSlider:

230: error: static assertion failed: The slot requires more arguments than the signal provides.
         Q_STATIC_ASSERT_X(int(SignalType::ArgumentCount) >= int(SlotType::ArgumentCount),
         ^

Does this mean a QWidget connected to a function cannot support default parameters? Is there a way to fix this?

Here is what I attempted:

void MainWindow::connections() {
    connect(sliderA, &QSlider::valueChanged, this, &MainWindow::someSliderChanged);
    connect(sliderB, &QSlider::valueChanged, this, &MainWindow::someSliderChanged);
}

void MainWindow::someSliderChanged(int position, char state = 0) {
    QObject* sender = QObject::sender();
    if (sender == sliderA || state == 1) {
        thing1->setValue(position);
    } else if (sender == sliderB || state == 2) {
        thing2->setValue(position);
    }
}

void MainWindow::default() {
    someSliderChanged(50, 1)
    someSliderChanged(70, 2)
}
like image 232
Graeme Rock Avatar asked Oct 15 '25 15:10

Graeme Rock


1 Answers

Does this mean a QWidget connected to a function cannot support default parameters?

From the documentation of signal and slots:

The signature of a signal must match the signature of the receiving slot.

Therefore I would say that no, it is not possible.

Is there a way to fix this?

As an example, if you compiler support C++11, you can use a lambda function.
Something like this:

connect(sliderA, &QSlider::valueChanged, [this](int pos){ someSliderChanged(pos); });

Otherwise, define a (let me say) forward slot that has not the extra parameter and bind it to the signal.

See the documentation for the overloads of QtObject::connect for further details.

like image 96
skypjack Avatar answered Oct 18 '25 05:10

skypjack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!