加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
commonhelper.h 3.27 KB
一键复制 编辑 原始数据 按行查看 历史
TianSong 提交于 2022-02-20 13:17 . Init Qt DBoS Desktop Gui Source Code.
#ifndef COMMONHELPER_H
#define COMMONHELPER_H
#include <QApplication>
#include <QDesktopWidget>
#include <QDir>
#include <QFile>
#include <QObject>
#include <QProcess>
#include <QRegExp>
#include <QSettings>
#include <QStringLiteral>
#include <QTime>
#include <QTranslator>
/**
* @brief 公共辅助类
* 1. 设置皮肤样式
* 2. 判断是否是IP地址
* 3. 模糊延时
* 4. 窗体剧中显示
* 5. 设置为开机启动
* 6. 设置为应用重启
*/
class CommonHelper : public QObject
{
public:
CommonHelper() = delete;
CommonHelper(const CommonHelper&) = delete;
CommonHelper &operator=(const CommonHelper&) = delete;
// 设置皮肤样式
static void setStyleSheet(const QString &styleSheet, QObject *widget)
{
QFile file(styleSheet);
if (file.open(QIODevice::ReadOnly))
{
QWidget *ptr = qobject_cast<QWidget *>(widget);
if (ptr) {
ptr->setStyleSheet(file.readAll());
}
else {
qApp->setStyleSheet(file.readAll());
}
file.close();
}
}
// 判断是否是IP地址
static bool isIP(const QString &ip)
{
QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
return regExp.exactMatch(ip);
}
// 模糊延时
static void sleep(unsigned long msec)
{
QTime deiTime = QTime::currentTime().addMSecs(msec);
while (QTime::currentTime() < deiTime)
{
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
}
// 窗体剧中显示
static void formInCenter(QWidget &widget)
{
QDesktopWidget w;
int deskWidth = w.width();
int deskHeight = w.height();
QPoint centerPoint((deskWidth - widget.width()) / 2, (deskHeight - widget.height()) / 2);
widget.move(centerPoint);
}
// 设置为开机启动
static void autoRunWithSystem(bool isAutoRun, const QString &appName, const QString &appPath)
{
QSettings reg(QStringLiteral("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), QSettings::NativeFormat);
if (isAutoRun)
reg.setValue(appName, appPath);
else
reg.setValue(appName, "");
}
// 应用重启
static void reboot()
{
QString program = QApplication::applicationFilePath();
QStringList argument = QApplication::arguments();
QString workDirectory = QDir::currentPath();
QProcess::startDetached(program, argument, workDirectory);
QApplication::exit();
}
static void setTranslator(const QString &file)
{
auto translator = new QTranslator(qApp);
if (translator->load(file))
qApp->installTranslator(translator);
}
// 文字转语音
/*
#include <QTextToSpeech>
QT += texttospeech
static bool textToSpeech(const QString &text, double volume=1.0, double rate=0.0, double pitch=0.0)
{
if (QTextToSpeech::availableEngines().isEmpty())
return false;
static QTextToSpeech speech(qApp);
if (speech.state() != QTextToSpeech::Ready)
return false;
speech.setVolume(volume);
speech.setRate(rate);
speech.setPitch(pitch);
speech.say(text);
return true;
}
*/
};
#endif // COMMONHELPER_H
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化