我有一个带有 QDoubleSpinBox 的代码
ui->doubleSpinBoxExposure->setMinimum(0.001);
ui->doubleSpinBoxExposure->setMaximum(1000);
ui->doubleSpinBoxExposure->setSingleStep(1.0);
connect(ui->doubleSpinBoxExposure, SIGNAL(valueChanged(double)),
this, SLOT(OndoubleSpinBoxExposure_valueChanged(double)));
void WidgetCameraParameter::OndoubleSpinBoxExposure_valueChanged(double value)
{
if (!camera)
return;
if (camera->isOpen())
{
float exposure = static_cast<float>(value);
float cameraExposure;
camera->setExposure(exposure);
LOG_INFO() <<" setting exposure to " << value << " ms";
cameraExposure = camera->exposure();
LOG_INFO() <<" resulting exposure is " << cameraExposure << " ms";
}
}
问题是,当我在 gui 中向上或向下时,这种情况会发生两次。 起始参数是 value = 2。StepUp 用 3 调用这个函数,然后直接用 4 调用这个函数。我不知道为什么。
堆栈跟踪没有帮助:
1 WidgetCameraParameter::OndoubleSpinBoxExposure_valueChanged widgetcameraparameter.cpp 311 0x406c17
2 WidgetCameraParameter::qt_static_metacall moc_widgetcameraparameter.cpp 110 0x40811f
3 QMetaObject::activate qobject.cpp 3771 0x12bc2e1
4 QMetaObject::activate qobject.cpp 3633 0x12bc575
5 QDoubleSpinBox::valueChanged moc_qspinbox.cpp 436 0x15e66190 6 QDoubleSpinBoxPrivate::emitSignals qspinbox.cpp 1112 0x15e663b2 7 QAbstractSpinBoxPrivate::setValue qabstractspinbox.cpp 1741 0x15e6174d 8 QAbstractSpinBox::stepBy qabstractspinbox.cpp 643 0x15e62aba 9 QAbstractSpinBox::timerEvent qabstractspinbox.cpp 1246 0x15e5ffea 10 QObject::event qobject.cpp 1232 0x12bc918
11 QWidget::event qwidget.cpp 9347 0x15d0c544 12 QAbstractSpinBox::event qabstractspinbox.cpp 795 0x15e65930 13 QApplicationPrivate::notify_helper qapplication.cpp 3727 0x15cc85ca 14 QApplication::notify qapplication.cpp 3690 0x15cd1f4f 15 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1048 0x1295119
16 QCoreApplication::sendEvent qcoreapplication.h 234 0x12e4d87
17 QEventDispatcherWin32Private::sendTimerEvent qeventdispatcher_win.cpp 447 0x12e4d87
18 qt_internal_proc(HWND__ *, unsigned int, unsigned int, long) *16 qeventdispatcher_win.cpp 242 0x12e53d5
19 gapfnScSendMessage 0x771162fa 20 ?? 0x5c0f30
21 USER32!GetThreadDesktop 0x77116d3a 22 QEventDispatcherWin32Private::sendTimerEvent qeventdispatcher_win.cpp 456 0x12e4dc9
23 ?? 0x5c0f30
24 USER32!CharPrevW 0x771177c4 25 USER32!DispatchMessageW 0x7711788a 26 QEventDispatcherWin32::processEvents qeventdispatcher_win.cpp 629 0x12e4ae8
27 QWindowsGuiEventDispatcher::processEvents qwindowsguieventdispatcher.cpp 74 0x2496dab7 28 QEventLoop::processEvents qeventloop.cpp 136 0x12937c8
29 QEventLoop::exec qeventloop.cpp 214 0x1293c20
30 QCoreApplication::exec qcoreapplication.cpp 1336 0x129c30e
31 QGuiApplication::exec qguiapplication.cpp 1761 0x8461552
32 QApplication::exec qapplication.cpp 2901 0x15cc84a9 33 qMain main.cpp 28 0x40183d
34 WinMain *16 qtmain_win.cpp 104 0x4094c5
35 main 0x4179ad
知道如何进一步调试吗?
编辑: 仅当我在插槽中使用断点进行调试时才会发生这种情况。如果没有槽,则仅调用一次。 槽的第二次调用不会从槽函数内的任何函数中发生,而是仅在槽从事件循环结束之后发生。
您可以循环查看完整代码: https://github.com/pospiech/code/tree/master/libdev/devices/CameraViewer
最佳答案
查看QStyle::StyleHint
枚举,有一个有趣的SH_SpinBox_ClickAutoRepeatThreshold
常量。您可以检查旋转框的当前值,如下所示:
qDebug() << ui->doubleSpinBoxExposure->style()->styleHint(QStyle::SH_SpinBox_ClickAutoRepeatThreshold);
这通常会返回500
,这是触发自动重复之前的毫秒数(即,如果用户按住鼠标按向上
旋转按钮超过该阈值,旋转框值将开始持续增加)。
要查看是否存在计时问题,请尝试使用自定义 QStyle
类更改该值,如下所示:
#include <QProxyStyle>
class MyStyle : public QProxyStyle
{
public:
int styleHint(StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const
{
if(stylehint == QStyle::SH_SpinBox_ClickAutoRepeatThreshold)
{
return 2000; //2 seconds threshold
}
return QProxyStyle::styleHint(stylehint, opt, widget, returnData);
}
};
并将其实例设置为旋转框样式:
ui->doubleSpinBoxExposure->setStyle(new MyStyle());
现在需要很长时间(两秒长的时间)才会触发自动重复,因此您的问题应该消失。
关于c++ - Qt 插槽被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52296702/