Qt 槽被调用两次

Qt slot called twice

我有一个带有 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

您的插槽似乎有一个冻结的 gui 线程。 您可以在您的插槽中试用此代码

void WidgetCameraParameter::OndoubleSpinBoxExposure_valueChanged(double value)
{
#define NUM_LOOPS 1000000000
    qDebug() << value;
    quint64 i = NUM_LOOPS;
    while(i--);
}

为避免这种情况,您必须将消耗大量 CPU 时间的操作移至另一个线程。

在调试模式下是因为自动重复计时器。 尝试使用此代码在调试中禁用自动重复,我想您会明白:

*.h

...

#ifdef QT_DEBUG
    bool eventFilter(QObject *watched, QEvent *event) override;
#endif

...

*.c

...

    ui->setupUi(this);
#ifdef QT_DEBUG
    ui->doubleSpinBoxExposure->installEventFilter(this);
#endif

...

#ifdef QT_DEBUG
bool WidgetCameraParameter::eventFilter(QObject *watched, QEvent *event)
{
    QDoubleSpinBox *castSBox = static_cast<QDoubleSpinBox*>(watched);
    if(castSBox && event->type()==QEvent::Timer)
    {
        QTimerEvent *tEvent = static_cast<QTimerEvent*>(event);
        if(tEvent)
            qDebug() << "<--QEvent::Timer-->" << tEvent->timerId();
        return true;
    }
    return QObject::eventFilter(watched,event);
}
#endif

查看 QStyle::StyleHint 枚举,有一个有趣的 SH_SpinBox_ClickAutoRepeatThreshold 常量。您可以检查它在您的旋转框上的当前值,如下所示:

qDebug() << ui->doubleSpinBoxExposure->style()->styleHint(QStyle::SH_SpinBox_ClickAutoRepeatThreshold);

这通常是 returns 500,这是触发自动重复的毫秒数(即,如果用户按住鼠标并按下 up 旋转按钮长于该阈值,旋转框值将开始连续增加)。

要查看您是否有计时问题,请尝试使用自定义 QStyle class 更改该值,如下所示:

#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());

现在需要很长时间(两秒长)才能触发自动重复,相应地您的问题应该消失了。