我有一个 QPushButton,它对 pressed()
和 released()
信号执行冗长的操作。我如何确保在执行 buttonReleased()
插槽的操作之前完成 buttonPressed()
插槽的所有操作?
我已经尝试过使用 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();
}
最佳答案
一般使用mutex是一种线程同步机制,这里不需要线程同步,因为是同一个线程。否则,我会建议使用 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
加载更多选项...这些只是为您提供的更多选项...
关于c++ - 在执行 buttonReleased() 之前等待 buttonPressed() 插槽完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383341/