It is necessary to log out by idle user. I decided to track mouse movements. Those. if the mouse does not move for a while, then perform some exit function:

this->setMouseTracking(true); void MainWindow::mouseMoveEvent(QMouseEvent *event) { if (event->type() == QEvent::MouseMove) { QTimer *mousetimer = new QTimer(this); connect(mousetimer, SIGNAL(timeout()), this, SLOT(on_logoutPushButton_clicked())); mousetimer->start(5000); } } void MainWindow::on_logoutPushButton_clicked() { this->close(); newauth = new AuthDialog; newauth->show(); } 

What am I doing wrong?

  • Explain what the problem is. - Cerbo
  • That is, every time the user pulls the mouse, you create a new timer? - KoVadim
  • Well, if the mouse tugs then the timer is reset. If 5 seconds have passed. then we leave. Or better? Currently not working. - Alexey Smirnov
  • 3
    the mouse is not reliably obtained, and if the table faltered and the mouse moved, well, or do not use the mouse on debt, because data is entered from the keyboard. It seems to me that you need to keep a system log, and suppose that if there are no events in the system for 10 minutes, then the output is Alex.B.
  • one
    Need to know inaction at the level of the entire system, or just inaction in this application? That is, if a user has turned off a program and is actively working in another, this program will not receive any events - is this considered to be inaction? If, nevertheless, at the level of the entire system, then it is necessary to turn to system resources and they are different in each system. What is yours? - cassini

1 answer 1

On linux / X11, you can learn about the user inactivity time through the screensaver API (screensaver).

On the development machine, install libxss-dev :

 $ sudo apt-get install libxss-dev 

Add to the .pro file

 LIBS += -lX11 -lXss 

In the new project, the default code is as follows:

mainwindow.h

 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void checkIdle(); private: void logout(); Ui::MainWindow *ui; }; #endif // MAINWINDOW_H 

mainwindow.cpp

 #include "mainwindow.h" #include "ui_mainwindow.h" #include <QTimer> #include <X11/extensions/scrnsaver.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); checkIdle(); // запускаСм отслСТиваниС } MainWindow::~MainWindow() { delete ui; } void MainWindow::checkIdle() // отслСТиваниС ΠΏΠ΅Ρ€ΠΈΠΎΠ΄Π° бСздСйствия ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ { static Display *display = XOpenDisplay(NULL); if (display == NULL) { return; } static XScreenSaverInfo *info = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(display, DefaultRootWindow(display), info); const int targetIdleMs = 5 * 1000; // Ρ†Π΅Π»Π΅Π²ΠΎΠ΅ врСмя бСздСйствия Π² миллисСкундах const int actualIdleMs = info->idle; // фактичСскоС врСмя бСздСйствия Π² мс if (actualIdleMs < targetIdleMs) { // ΠΆΠ΄Π΅ΠΌ ΠΎΡΡ‚Π°Π²ΡˆΠ΅Π΅ΡΡ врСмя Π΄ΠΎ Ρ†Π΅Π»Π΅Π²ΠΎΠ³ΠΎ ΠΈ ΠΏΠΎΡ‚ΠΎΠΌ снова провСряСм QTimer::singleShot(targetIdleMs - actualIdleMs, this, SLOT(checkIdle())); } else { // Ρ†Π΅Π»Π΅Π²ΠΎΠ΅ врСмя достигнуто logout(); } } void MainWindow::logout() // ΠΆΠ΅Π»Π°Π΅ΠΌΡ‹Π΅ дСйствия ΠΏΡ€ΠΈ бСздСйствии ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ { close(); } 

For clarification. Added two features

 private slots: void checkIdle(); private: void logout(); 

Added to .cpp

 #include <QTimer> #include <X11/extensions/scrnsaver.h> 

From the constructor, call checkIdle() . In it, the targetIdleMs constant targetIdleMs target inactivity time, compare it with the actual and wait on the timer until the next check. At the next check, if the required period was not formed, we again wait; otherwise, call logout() , which in this case closes the program.

  • Each time XOpenDisplay() is called XOpenDisplay() will it return a pointer to the information stored in Xlib? (just in the manpage it says only that this call establishes a connection with the server and not a word about its reuse) - avp