I have a QPushButton that performs lengthy actions on pressed()
and released()
signals. How can I make sure that I finished executing all actions of the buttonPressed()
slot, before executing the ones of the buttonReleased()
slot?
I have tried with QMutex, but the program seems to be stuck in an endless loop when trying to lock upon button release, when the mutex is still locked by the buttonPressed()
function:
mymainwindow.h:
#include <QMutex>
// ...
QMutex mutex;
mymainwindow.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();
}