2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Use QT to adjust the screen brightness: when there is no screen and no touch, lower the screen brightness; if there is touch, adjust it to the brightest.
The target device uses an embedded Linux system, and the command to adjust the screen brightness is:
echo x > /sys/class/backlight/backlight/brightness #x范围为0~10
There is no problem in executing this command using the terminal. However, when using QProcess to execute this command, there is no response and no error message is prompted.
Finally found the command to use:
/bin/sh -c "echo x > /sys/class/backlight/backlight/brightness" #x为亮度值
The program code is implemented as follows:
- QProcess setBright(this);// 进程函数,调用外部指令窗口
- setBright.start("cat /sys/class/backlight/backlight/brightness n"); #获取当前亮度值
- setBright.waitForFinished(); // 等待命令执行完成
- QString strCmd = QString("/bin/sh -c "echo %1 > /sys/class/backlight/backlight/brightness"").arg(brightness/10);
- //执行指令
- setBright.start(strCmd.toLocal8Bit().data()); //convert QString to char
- setBright.waitForFinished(1000);// 等待指令执行完毕
- QString strErrors = setBright.readAllStandardError();// 获取返回值
- qDebug()<<strErrors;
If the screen is not clicked for a period of time, the screen will be dimmed or turned off. If there is a touch event (i.e. mouse press/move), the screen will be woken up and lit.
When there is a window, the implementation is relatively simple. Just implement the mouse event response in the window control class. The sample code is as follows:
- //h
- protected:
- void mousePressEvent(QMouseEvent *event);
- void mouseMoveEvent(QMouseEvent *event);
- void mouseReleaseEvent(QMouseEvent *event);
- //cpp
- void MainWindow::mousePressEvent(QMouseEvent *event)
- {
- qDebug() << "Mouse pressed";
- mScreenSleep->ToWake(); //唤醒屏幕
- }
- void MainWindow::mouseMoveEvent(QMouseEvent *event)
- {
- qDebug() << "Mouse moved";
- mScreenSleep->ToWake(); //唤醒屏幕
- }
- void MainWindow::mouseReleaseEvent(QMouseEvent *event)
- {
- qDebug() << "Mouse released";
- mScreenSleep->ToWake(); //唤醒屏幕
- }
Note: You can only respond to mouse events within the window control. You can use the QApplication class to respond to events outside the window control.
Problem: When the window is full screen and set to transparent, the screen cannot be turned on after it is turned off.
The sample code is as follows:
- //h
- #ifndef GLOBALAPP_H
- #define GLOBALAPP_H
-
- #include <QApplication>
- #include "screensleep.h" //屏幕亮度控制类
-
- class globalapp : public QApplication
- {
- public:
- globalapp(int &argc,char **argv);
- ~globalapp();
- ScreenSleep *mScreenSleep;
-
- bool notify(QObject*, QEvent *);
- };
-
- #endif // GLOBALAPP_H
-
- //cpp
- #include "globalapp.h"
- #include <QDebug>
-
- globalapp::globalapp(int &argc,char **argv):QApplication(argc,argv)
- {
- mScreenSleep = new ScreenSleep(this);
- }
- globalapp::~globalapp()
- {
- delete mScreenSleep;
- }
- bool globalapp::notify(QObject *obj, QEvent *e)
- {
- if(e->type() == QEvent::KeyPress || e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseMove)
- {
- mScreenSleep->ToWake();
- }
- else if(e->type() == QEvent::SockAct) //为什么响应的是这个事件???
- {
- mScreenSleep->ToWake();
- }
- return QApplication::notify(obj,e);
- }
- //main
- int main(int argc, char *argv[])
- {
- globalapp a(argc, argv); //使用自建的QApplication对象
- return a.exec();
- }
The sample code is as follows:
- #include <QApplication>
- #include <QMouseEvent>
- #include <QDebug>
-
- class MouseHook : public QObject {
- bool eventFilter(QObject *obj, QEvent *event) override {
- if (event->type() == QEvent::MouseMove) {
- QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
- //放置事件处理
- }
- else if(event->type() == QEvent::SockAct)
- {
- QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
- //放置事件处理
- }
- return QObject::eventFilter(obj, event);
- }
- };
- int main(int argc, char *argv[])
- {
- MouseHook mouseHook;
- a.installEventFilter(&mouseHook);
- return a.exec();
- }
As can be seen from the above comments, when there is no window, the mouse click or touch event is not equal to the mouse event defined by QT, but a QEvent::SockAct event!
As to why this is the case, the reason has not been found yet, but this is a big pit!