Qt:信号调用函数的默认参数
Qt: Default Parameters for function called by Signal
当我尝试将具有一个默认参数和一个非默认参数的函数连接到 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),
^
这是否意味着连接到函数的QWidget不支持默认参数?有办法解决这个问题吗?
这是我的尝试:
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)
}
Does this mean a QWidget connected to a function cannot support default parameters?
来自信号与槽的documentation:
The signature of a signal must match the signature of the receiving slot.
所以我会说不,这是不可能的。
Is there a way to fix this?
例如,如果编译器支持 C++11,则可以使用 lambda 函数。
像这样:
connect(sliderA, &QSlider::valueChanged, [this](int pos){ someSliderChanged(pos); });
否则,定义一个(让我说)没有额外参数的前向槽并将其绑定到信号。
有关 QtObject::connect
重载的详细信息,请参阅 documentation。
当我尝试将具有一个默认参数和一个非默认参数的函数连接到 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),
^
这是否意味着连接到函数的QWidget不支持默认参数?有办法解决这个问题吗?
这是我的尝试:
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)
}
Does this mean a QWidget connected to a function cannot support default parameters?
来自信号与槽的documentation:
The signature of a signal must match the signature of the receiving slot.
所以我会说不,这是不可能的。
Is there a way to fix this?
例如,如果编译器支持 C++11,则可以使用 lambda 函数。
像这样:
connect(sliderA, &QSlider::valueChanged, [this](int pos){ someSliderChanged(pos); });
否则,定义一个(让我说)没有额外参数的前向槽并将其绑定到信号。
有关 QtObject::connect
重载的详细信息,请参阅 documentation。