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