Qt判断当前操作系统?
可使用宏判断,例如:
#ifdef Q_OS_MAC //mac
...
#endif
#ifdef Q_OS_LINUX //linux
...
#endif
#ifdef Q_OS_WIN32 //win
...
#endif
#ifdef __arm__ //arm
...
#endif
Qt获取屏幕分辨率?
#include <QDesktopWidget>
//获取设备屏幕大小
QRect screenRect = QApplication::desktop()->screenGeometry();
qDebug() << "设备屏幕分辨率: " << screenRect.width() <<" " << screenRect.height();
Qt实现应用程序关闭和重启?
//关机按钮-点击槽函数
void SystemD::on_shutdownButton_clicked()
{
//关闭应用程序
QCoreApplication::exit();
}
//重启按钮-点击槽函数
void SystemD::on_rebootButton_clicked()
{
//重启应用程序
qApp->quit();
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
}
Qt实现Linux下的系统关机和重启?
先使Linux的普通用户可以在不输入密码的情况下,执行sudo reboot
命令实现重启,具体步骤可以参考我的另一篇博客 - Linux常见问题及解决方案 的第13小结。
//关机按钮-点击槽函数
void SystemD::on_shutdownButton_clicked()
{
QProcess::execute("sudo halt"); //UBuntu下执行关机命令(需要root权限)
}
//重启按钮-点击槽函数
void SystemD::on_rebootButton_clicked()
{
QProcess::execute("sudo reboot"); //UBuntu下执行重启命令(需要root权限)
}