2

pressed()我有一个 QPushButton 可以对released()信号执行冗长的操作。在执行插槽的操作之前,如何确保我完成了buttonPressed()插槽的所有操作buttonReleased()

我已经尝试过使用 QMutex,但是当尝试在按钮释放时锁定时,程序似乎陷入了无限循环,此时互斥锁仍被buttonPressed()函数锁定:

我的主窗口.h:

#include <QMutex>

// ...

QMutex mutex;

我的主窗口.cpp:

#include <QEventLoop>
#include <QTimer>

// ...

// In the main window constructor:
connect(myButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
connect(myButton, SIGNAL(released()), this, SLOT(buttonReleased()));

// ...

void MyMainWindow::buttonPressed()
{
    mutex.lock();

    // Here, I do the lengthy stuff, which is simulated by a loop
    // that waits some time.
    QEventLoop loop;
    QTimer::singleShot(1000, &loop, SLOT(quit()));
    loop.exec();

    mutex.unlock();
}

void MyMainWindow::buttonReleased()
{
    mutex.lock();

    // ... (some stuff)

    mutex.unlock();
}
4

2 回答 2

1

通常使用互斥锁是一种线程同步机制,这里不需要线程同步,因为你在同一个线程中。否则,我建议使用 QWaitCondition 来等待标志/互斥体发生变化(即表示您的条件现在可以继续了)。

在您的情况下,您可以在“buttonPressed”操作完成后发出一个信号(即当您的计时器结束时?)。如果 buttonPressed() 函数的结束是您想要执行 buttonRelease() 函数的时间,那么您可以简单地使用 Qt::QueuedConnection 来确保事件的正确顺序(我通常不喜欢直接连接,因为它们的行为就像函数调用(甚至中断——就像我认为发生在你身上的那样)。所以以下更改可能会以一种简单的方式为你解决这个问题:

// In the main window constructor:
connect(myButton, SIGNAL(pressed()), this, SLOT(buttonPressed()), Qt::QueuedConnection);
connect(myButton, SIGNAL(released()), this, SLOT(buttonReleased()), Qt::QueuedConnection);

我不确定执行你的事件循环来“模拟”你的“长时间”是否会起作用....但是如果你做一些更像下面的事情来模拟你的长执行时间:

QElapsedTimer elapsedTime;
elapsedTime.start();
while (elapsedTime.elapsed() < 1000) // millisecs
{
    // wait....
}

如果这不起作用,那么只需在 buttonPressed() 结束时发出一个信号并在 buttonReleased() 中设置一个标志,这样:

void MyMainWindow::buttonPressed()
{
    // actions here
    emit buttonPressedDone();
}

void MyMainWindow::buttonReleased()
{
    btnReleased = true;
}

void MyMainWindow::buttonPressedCompleted()
{
    if (btnReleased )
    {
        // Do button released actions here
        btnReleased  = false;
    }
    // I am assuming that if the flag is not set then you don't want to do anything... but up to you...
}

并连接 buttonPressedDone --> buttonPressedCompleted

有加载更多选项...这些只是为您提供的几个选项...

于 2016-05-23T16:36:52.570 回答
0

是否需要连接才能释放按钮的SLOT?您只需连接到destroyed()您的 SIGNALQEventLoop并调用buttonReleased()SLOT。

QEventLoop loop;
QTimer::singleShot(1000, &loop, SLOT(quit()));
connect(&loop, &QEventLoop::destroyed, this, &MyMainWindow::buttonReleased);
loop.exec();

由评论编辑:

QEventLoop loop;
QTimer::singleShot(1000, &loop, SLOT(quit()));
connect(&loop, &QEventLoop::destroyed, [=] {
    connect(myButton, &QPushButton::released, this, &MyMainWindow::buttonReleased); 
});
loop.exec();
于 2016-05-23T06:26:46.580 回答