Third generation software development-implementation requirements return to the login interface after a long period of inactivity
Article directory
- Third generation software development-implementation requirements return to the login interface after no operation for a long time
-
- Project Introduction
- To realize the requirement, return to the login interface after no operation for a long time.
-
- Implementation ideas
- User operation monitoring
- QML logic processing
Keywords:
Qt
、
Qml
、
QTimer
、
timeout
、
eventFilter
Project introduction
Welcome to our QML & C++ project! This project combines the power of QML (Qt Meta-Object Language) and C++ to develop excellent user interfaces and high-performance backend logic.
In the project, we leveraged QML’s declarative syntax and visual design capabilities to create a modern user interface. Through intuitive coding and reusable components, we can quickly develop rich and diverse interface effects and animation effects. At the same time, we use QML’s powerful integration capabilities to easily integrate the underlying logic and data model of C++ into the front-end interface.
On the backend side, we use C++ to write high-performance algorithms, data processing, and computational logic. C++ is a powerful programming language that offers excellent performance and scalability. Our team is committed to optimizing code and reducing resource consumption to ensure that our projects run efficiently on a variety of platforms and devices.
Whether you’re interested in QML and C++ development or need us to build complex user interfaces and backend logic for you, we’re ready to support you. Please feel free to contact us and let’s build a modern, high-performance QML & C++ project together!
Important?
?The price of this column will increase after the third generation soft development update.
Return to the login interface if no operation is performed for a long time to achieve the requirements
Implementation ideas
Define a timer object QTimer *timer
and a time interval in the main window class to record the time of the user’s last operation.
QTimer *timer; int idleTime;
Initialize the timer in the constructor and connect the timer’s timeout()
signal to a slot function.
timer = new QTimer(this); connect(timer, & amp;QTimer::timeout, this, & amp;MainWindow::checkIdleTime);
Implement an event filter function eventFilter()
in the main window class to monitor user operation events. In this function, if a mouse click, keyboard key, or movement event is received, the time recorded by the timer is updated.
bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress || event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseMove) { idleTime = 0; //Reset timer } return QObject::eventFilter(obj, event); }
Set the event filter in the showEvent()
function of the main window to start monitoring user operation events.
void MainWindow::showEvent(QShowEvent *event) { QWidget::showEvent(event); qApp->installEventFilter(this); idleTime = 0; //Reset timer timer->start(1000); // Start the timer and trigger the timeout() signal every second }
Implement the slot function checkIdleTime()
and check whether the idle time exceeds the set threshold in this function. If it exceeds, the login interface is displayed and the timer is stopped.
void MainWindow::checkIdleTime() { idleTime + + ; if (idleTime >= MAX_IDLE_TIME) { qApp->removeEventFilter(this); // Stop event filter timer->stop(); // Stop the timer //Code to display the login interface showLoginDialog(); } }
Through the above steps, we can realize the function of returning to the login interface after a long period of inactivity. When the user performs any operation, the timer will be reset; if the user does not operate for a period of time, the timer will exceed the preset threshold, triggering the operation of returning to the login interface. You need to adjust the value of MAX_IDLE_TIME
according to the actual situation, in seconds. At the same time, a custom function showLoginDialog()
related to the login interface needs to be added to the main window class to display the login interface.
If you happened to read my last article [Third Generation Software Development-TabView
], and you also read my other published article https://blog.csdn.net/ z609932088/article/details/130926692?spm=1001.2014.3001.5501
?
Then you already know how to do it.
User operation monitoring
What can users operate, keyboard, mouse and touch screen, there should be nothing else, so we only need to deal with these here. This part has been mentioned in my last blog post. Let’s just attach the code to make up the word count. .
bool XXXX::eventFilter(QObject *obj, QEvent *event) { /** * Capture keyboard events **/ if( event->type() == QEvent::KeyPress || // Keyboard press event->type() == QEvent::MouseMove || // Mouse movement event->type() == QEvent::MouseButtonPress || // Mouse button pressed event->type() == QEvent::Wheel || // Mouse wheel event event->type() == QEvent::TouchBegin) // Screen touch event { m_lockScreenTimer->start(m_timerLong); setLockScreen(false); } #ifdef QT_NO_DEBUG /** * Disable normal program shutdown **/ if(event->type() == QEvent::Close & amp; & amp; !m_closeSystem) event->ignore(); #endif return QObject::eventFilter(obj,event); }
Note that this is C++ code. We need to register this as a module that QML can recognize. Another thing to note here is that my filter is installed to QApplication. code show as below
QApplication::instance()->installEventFilter(this);
QML logic processing
XXXX { id:keymonitor timerLong: UserProfile.screenProtectTime // Screen saver time onLockScreenChanged: { if(tabview_mainFrame.currentIndex === 0) { if(lockScreen) { tabview_mainFrame.getTab(0).item.lockScreen() } else tabview_mainFrame.getTab(0).item.unLockScreen() return } if(tabview_mainFrame.getTab(1).item) { if(lockScreen & amp; & amp; !(tabview_mainFrame.getTab(1).item.frameIndex === 1 || tabview_mainFrame.getTab(1).item.frameIndex === 0)) { tabview_mainFrame.currentIndex = 0; tabview_mainFrame.getTab(0).item.lockScreen() tabview_mainFrame.getTab(1).item.signalLockScreen() } else tabview_mainFrame.getTab(0).item.unLockScreen() } else { if(lockScreen) { tabview_mainFrame.currentIndex = 0; tabview_mainFrame.getTab(0).item.lockScreen() } else tabview_mainFrame.getTab(0).item.unLockScreen() } } }
We don’t need to explain too much here, because some of the code is for business, that is, the screen cannot be locked in a certain mode, just like when we watch a movie in full screen and cannot enter the screensaver.