I built a small GUI that has shutdown feature after 1 minute. There is a count down that the user can see. The GUI could be left open and inactive if the user does not interact with it of course.
The problem I have is that I am trying to reset the QTimer
back to 60 seconds every time the user interacts with the GUI after 5 seconds of inactivity. How do I do that?
Below and here a small verifiable example that shows only a count down time implemented in a QLabel
, I know from official documentation that the proper way to do it is to re implement QCoreApplication::notify
and that is what I did but something is still not working as expected and I don't see the count down resetting to 60 seconds after I move the mouse after a period of inactivity of 5 seconds.
The minimal code below is perfectly compiling with no errors. You can copy/paste on your machine:
mainwindow.h
#include <QMainWindow>
#include <QTime>
#include <QTimer>
#include <QProcess>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void stopAtZeroCountDown();
public slots:
void timerUpdate();
private:
Ui::MainWindow *ui;
QTimer *timer;
QTime time;
QProcess *stopAtZero;
};
#include <QApplication>
class QMyApplication : public QApplication
{
Q_OBJECT
public:
QTimer m_timer;
QMyApplication(int &argc, char **argv) : QApplication(argc,argv)
{
m_timer.setInterval(5000);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(inactive()));
m_timer.start();
}
public slots:
void inactive() {
m_timer.stop();
}
protected:
bool notify ( QObject * receiver, QEvent * event )
{
if (event->type() == QEvent::MouseMove || event->type() == QEvent::KeyPress) {
m_timer.stop(); // reset timer
m_timer.start();
}
return QApplication::notify(receiver, event);
};
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//stopAtZeroCountDown();
ui->countDown->setText("1:00");
time.setHMS(0,1,0);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::stopAtZeroCountDown()
{
this->stopAtZero->execute(QStringLiteral("/bin/sh"), QStringList() << QStringLiteral("/home/emanuele/catkin_docking_ws/src/lidarboatsproject/stop_lidar_deck_and_gui.sh"));
}
void MainWindow::timerUpdate()
{
time = time.addSecs(-1);
ui->countDown->setText(time.toString("mm:ss"));
// Check if we have a zero time 00:00 on the count down
if (time == QTime(0, 0)) {
stopAtZeroCountDown();
}
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
The errors I am getting and how I am trying to solve them:
The majority of the errors I am gettings are in the last impementation of the protected function as below:
protected:
bool QMyApplication::notify ( QObject * receiver, QEvent * event ) // <-- Error here
{
if (event->type() == QEvent::MouseMove || event->type() == QEvent::KeyPress) {
m_timer.stop(); // reset timer
m_timer.start();
}
return QApplicaiton::notify(receiver, event); // <-- Error here
};
1) I solved the bool QMyApplication::notify ( QObject * receiver, QEvent * event )
error by just declaring the notify
function in the following way:
bool notify ( QObject * receiver, QEvent * event ) // <-- This way I solved it
2) But for the last error what I tried was to return the QMyApplication::notify
instead the return QApplicaiton::notify
but gave the warning that "All path through this function will call itself"
return QMyApplication::notify(receiver, event); // <-- Still error here
How could I fix this and reset the QTimer
back to 60 seconds every time the user interacts with the GUI after 5 seconds of inactivity?
Thanks for pointing to the right direction for solving this issue.