Qt插槽调用了两次

时间:2018-09-12 13:45:03

标签: c++ qt signals-slots

我有一个带有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或下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 ::通知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个主要的0x4179ad

有什么想法可以进一步调试吗?

编辑: 仅当我使用插槽中的断点调试时,才会发生这种情况。没有插槽只能调用一次。 插槽的第二个调用不会在插槽功能中的任何函数上发生,而只会在插槽从事件循环结束后发生。

您可以循环查看完整的代码: https://github.com/pospiech/code/tree/master/libdev/devices/CameraViewer

2 个答案:

答案 0 :(得分:1)

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

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

这通常会返回500,这是触发自动重复的毫秒数(即,如果用户按住up旋转按钮上的鼠标的时间超过该阈值,则旋转框值将开始持续增加。)

要查看是否存在计时问题,请尝试使用类似这样的自定义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());

现在,触发自动重复需要花费很多时间(两秒钟),因此,您的问题应该消失了。

答案 1 :(得分:0)

似乎您的插槽中有冻结的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