|
|
#include "quiwidget.h"
|
|
|
// 引入自定义的头文件 "quiwidget.h",通常包含了与QUIWidget类相关的声明等信息
|
|
|
|
|
|
int QUIWidget::deskWidth()
|
|
|
{
|
|
|
return qApp->desktop()->availableGeometry().width();
|
|
|
// 返回应用程序所在桌面的可用宽度,通过qApp获取应用程序实例,再调用desktop()方法获取桌面相关信息,最后获取宽度
|
|
|
}
|
|
|
|
|
|
int QUIWidget::deskHeight()
|
|
|
{
|
|
|
return qApp->desktop()->availableGeometry().height();
|
|
|
// 返回应用程序所在桌面的可用高度,原理同获取宽度的函数,只是这里获取的是高度值
|
|
|
}
|
|
|
|
|
|
QString QUIWidget::appName()
|
|
|
{
|
|
|
QString name = qApp->applicationFilePath();
|
|
|
// 获取应用程序的完整文件路径
|
|
|
QStringList list = name.split("/");
|
|
|
// 将文件路径以'/'为分隔符拆分成字符串列表
|
|
|
name = list.at(list.count() - 1).split(".").at(0);
|
|
|
// 取列表中最后一个元素(文件名部分),再以'.'为分隔符拆分,取第一个元素(文件名去除后缀)作为应用程序名并返回
|
|
|
return name;
|
|
|
}
|
|
|
|
|
|
QString QUIWidget::appPath()
|
|
|
{
|
|
|
#ifdef __arm__
|
|
|
return "/sdcard/Android/lys";
|
|
|
// 如果定义了__arm__宏,返回安卓设备上特定的路径(可能是用于存储应用相关数据的路径)
|
|
|
//return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
|
|
|
#else
|
|
|
return qApp->applicationDirPath();
|
|
|
// 如果未定义__arm__宏,返回应用程序所在的目录路径
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
void QUIWidget::newDir(const QString &dirName)
|
|
|
{
|
|
|
QString strDir = dirName;
|
|
|
// 将要创建的目录路径赋值给strDir变量
|
|
|
|
|
|
//如果路径中包含斜杠字符则说明是绝对路径
|
|
|
//linux系统路径字符带有 / windows系统 路径字符带有 :/
|
|
|
if (!strDir.startsWith("/") &&!strDir.contains(":/")) {
|
|
|
strDir = QString("%1/%2").arg(QUIWidget::appPath()).arg(strDir);
|
|
|
// 如果不是绝对路径,将应用程序路径和传入的目录名拼接成完整的绝对路径
|
|
|
}
|
|
|
|
|
|
QDir dir(strDir);
|
|
|
// 创建QDir对象,用于操作目录相关操作
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
dir.mkpath(strDir);
|
|
|
// 如果目录不存在,则创建该目录及其所有必要的父目录
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void QUIWidget::writeInfo(const QString &info, const QString &filePath)
|
|
|
{
|
|
|
QString fileName = QString("%1/%2/%3_runinfo_%4.txt").arg(QUIWidget::appPath())
|
|
|
.arg(filePath).arg(QUIWidget::appName()).arg(QDate::currentDate().year());
|
|
|
// 构建要写入信息的文件名,包含应用程序路径、传入的文件路径、应用程序名以及当前年份
|
|
|
|
|
|
QFile file(fileName);
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Append | QFile::Text);
|
|
|
// 以只写、追加模式打开文件,并指定文本格式
|
|
|
QTextStream stream(&file);
|
|
|
stream << DATETIME << info << NEWLINE;
|
|
|
// 将特定的时间信息(这里假设DATETIME和NEWLINE是预定义的常量或宏表示时间格式等相关内容)和传入的信息写入文件流
|
|
|
file.close();
|
|
|
// 关闭文件
|
|
|
}
|
|
|
|
|
|
void QUIWidget::setStyle(QUIWidget::Style style)
|
|
|
{
|
|
|
QString qssFile = ":/qss/blue.css";
|
|
|
// 初始化样式表文件名为默认的":/qss/blue.css"
|
|
|
|
|
|
if (style == QUIWidget::Style_Silvery) {
|
|
|
qssFile = ":/qss/silvery.css";
|
|
|
} else if (style == QUIWidget::Style_Blue) {
|
|
|
qssFile = ":/qss/blue.css";
|
|
|
} else if (style == QUIWidget::Style_LightBlue) {
|
|
|
qssFile = ":/qss/lightblue.css";
|
|
|
} else if (style == QUIWidget::Style_DarkBlue) {
|
|
|
qssFile = ":/qss/darkblue.css";
|
|
|
} else if (style == QUIWidget::Style_Gray) {
|
|
|
qssFile = ":/qss/gray.css";
|
|
|
} else if (style == QUIWidget::Style_LightGray) {
|
|
|
qssFile = ":/qss/lightgray.css";
|
|
|
} else if (style == QUIWidget::Style_DarkGray) {
|
|
|
qssFile = ":/qss/darkgray.css";
|
|
|
} else if (style == QUIWidget::Style_Black) {
|
|
|
qssFile = ":/qss/black.css";
|
|
|
} else if (style == QUIWidget::Style_LightBlack) {
|
|
|
qssFile = ":/qss/lightblack.css";
|
|
|
} else if (style == QUIWidget::Style_DarkBlack) {
|
|
|
qssFile = ":/qss/darkblack.css";
|
|
|
} else if (style == QUIWidget::Style_PSBlack) {
|
|
|
qssFile = ":/qss/psblack.css";
|
|
|
} else if (style == QUIWidget::Style_FlatBlack) {
|
|
|
qssFile = ":/qss/flatblack.css";
|
|
|
} else if (style == QUIWidget::Style_FlatWhite) {
|
|
|
qssFile = ":/qss/flatwhite.css";
|
|
|
}
|
|
|
// 根据传入的不同样式枚举值,设置对应的样式表文件名
|
|
|
|
|
|
QFile file(qssFile);
|
|
|
// 创建QFile对象,用于读取样式表文件
|
|
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
QString qss = QLatin1String(file.readAll());
|
|
|
// 读取样式表文件的全部内容,并转换为QString类型
|
|
|
QString paletteColor = qss.mid(20, 7);
|
|
|
// 从样式表内容中提取特定位置(这里假设是第20个字符开始,长度为7个字符的部分)的颜色值作为调色板颜色
|
|
|
qApp->setPalette(QPalette(QColor(paletteColor)));
|
|
|
// 根据提取的调色板颜色设置应用程序的调色板
|
|
|
qApp->setStyleSheet(qss);
|
|
|
// 设置应用程序的样式表为读取到的样式表内容
|
|
|
file.close();
|
|
|
// 关闭文件
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void QUIWidget::setStyle(const QString &qssFile, QString &paletteColor, QString &textColor)
|
|
|
{
|
|
|
QFile file(qssFile);
|
|
|
// 创建QFile对象用于读取指定的样式表文件
|
|
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
QString qss = QLatin1String(file.readAll());
|
|
|
// 读取样式表文件的全部内容转换为QString
|
|
|
paletteColor = qss.mid(20, 7);
|
|
|
// 提取样式表中特定位置的颜色值作为调色板颜色
|
|
|
textColor = qss.mid(49, 7);
|
|
|
// 提取样式表中另一个特定位置的颜色值作为文本颜色
|
|
|
qApp->setPalette(QPalette(QColor(paletteColor)));
|
|
|
// 根据提取的调色板颜色设置应用程序调色板
|
|
|
qApp->setStyleSheet(qss);
|
|
|
// 设置应用程序的样式表为读取的内容
|
|
|
file.close();
|
|
|
// 关闭文件
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void QUIWidget::setStyle(const QString &qssFile, QString &textColor, QString &panelColor, QString &borderColor,
|
|
|
QString &normalColorStart, QString &normalColorEnd,
|
|
|
QString &darkColorStart, QString &darkColorEnd, QString &highColor)
|
|
|
{
|
|
|
QFile file(qssFile);
|
|
|
// 创建QFile对象用于读取指定样式表文件
|
|
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
QString qss = QLatin1String(file.readAll());
|
|
|
// 读取样式表文件全部内容并转换为QString
|
|
|
getQssColor(qss, textColor, panelColor, borderColor, normalColorStart, normalColorEnd, darkColorStart, darkColorEnd, highColor);
|
|
|
// 调用函数从样式表内容中提取多个不同用途的颜色值
|
|
|
qApp->setPalette(QPalette(QColor(panelColor)));
|
|
|
// 根据提取的面板颜色设置应用程序调色板
|
|
|
qApp->setStyleSheet(qss);
|
|
|
// 设置应用程序的样式表为读取的内容
|
|
|
file.close();
|
|
|
// 关闭文件
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void QUIWidget::getQssColor(const QString &qss, QString &textColor, QString &panelColor, QString &borderColor,
|
|
|
QString &normalColorStart, QString &normalColorEnd,
|
|
|
QString &darkColorStart, QString &darkColorEnd, QString &highColor)
|
|
|
{
|
|
|
QString str = qss;
|
|
|
// 将传入的样式表内容赋值给临时变量str
|
|
|
|
|
|
QString flagTextColor = "TextColor:";
|
|
|
int indexTextColor = str.indexOf(flagTextColor);
|
|
|
// 查找样式表内容中"TextColor:"字符串的位置
|
|
|
|
|
|
if (indexTextColor >= 0) {
|
|
|
textColor = str.mid(indexTextColor + flagTextColor.length(), 7);
|
|
|
// 如果找到该字符串,提取其后面特定长度(7个字符)的内容作为文本颜色值
|
|
|
}
|
|
|
|
|
|
QString flagPanelColor = "PanelColor:";
|
|
|
int indexPanelColor = str.indexOf(flagPanelColor);
|
|
|
// 查找样式表内容中"PanelColor:"字符串的位置
|
|
|
|
|
|
if (indexPanelColor >= 0) {
|
|
|
panelColor = str.mid(indexPanelColor + flagPanelColor.length(), 7);
|
|
|
// 如果找到该字符串,提取其后面特定长度(7个字符)的内容作为面板颜色值
|
|
|
}
|
|
|
|
|
|
QString flagBorderColor = "BorderColor:";
|
|
|
int indexBorderColor = str.indexOf(flagBorderColor);
|
|
|
// 查找样式表内容中"BorderColor:"字符串的位置
|
|
|
|
|
|
if (indexBorderColor >= 0) {
|
|
|
borderColor = str.mid(indexBorderColor + flagBorderColor.length(), 7);
|
|
|
// 如果找到该字符串,提取其后面特定长度(7个字符)的内容作为边框颜色值
|
|
|
}
|
|
|
|
|
|
QString flagNormalColorStart = "NormalColorStart:";
|
|
|
int indexNormalColorStart = str.indexOf(flagNormalColorStart);
|
|
|
// 查找样式表内容中"NormalColorStart:"字符串的位置
|
|
|
|
|
|
if (indexNormalColorStart >= 0) {
|
|
|
normalColorStart = str.mid(indexNormalColorStart + flagNormalColorStart.length(), 7);
|
|
|
// 如果找到该字符串,提取其后面特定长度(7个字符)的内容作为普通颜色起始值
|
|
|
}
|
|
|
|
|
|
QString flagNormalColorEnd = "NormalColorEnd:";
|
|
|
int indexNormalColorEnd = str.indexOf(flagNormalColorEnd);
|
|
|
// 查找样式表内容中"NormalColorEnd:"字符串的位置
|
|
|
|
|
|
if (indexNormalColorEnd >= 0) {
|
|
|
normalColorEnd = str.mid(indexNormalColorEnd + flagNormalColorEnd.length(), 7);
|
|
|
// 如果找到该字符串,提取其后面特定长度(7个字符)的内容作为普通颜色结束值
|
|
|
}
|
|
|
|
|
|
QString flagDarkColorStart = "DarkColorStart:";
|
|
|
int indexDarkColorStart = str.indexOf(flagDarkColorStart);
|
|
|
// 查找样式表内容中"DarkColorStart:"字符串的位置
|
|
|
|
|
|
if (indexDarkColorStart >= 0) {
|
|
|
darkColorStart = str.mid(indexDarkColorStart + flagDarkColorStart.length(), 7);
|
|
|
// 如果找到该字符串,提取其后面特定长度(7个字符)的内容作为深色起始值
|
|
|
}
|
|
|
|
|
|
QString flagDarkColorEnd = "DarkColorEnd:";
|
|
|
int indexDarkColorEnd = str.indexOf(flagDarkColorEnd);
|
|
|
// 查找样式表内容中"DarkColorEnd:"字符串的位置
|
|
|
|
|
|
if (indexDarkColorEnd >= 0) {
|
|
|
darkColorEnd = str.mid(indexDarkColorEnd + flagDarkColorEnd.length(), 7);
|
|
|
// 如果找到该字符串,提取其后面特定长度(7个字符)的内容作为深色结束值
|
|
|
}
|
|
|
|
|
|
QString flagHighColor = "HighColor:";
|
|
|
int indexHighColor = str.indexOf(flagHighColor);
|
|
|
// 查找样式表内容中"HighColor:"字符串的位置
|
|
|
|
|
|
if (indexHighColor >= 0) {
|
|
|
highColor = str.mid(indexHighColor + flagHighColor.length(), 7);
|
|
|
// 如果找到该字符串,提取其后面特定长度(7个字符)的内容作为高亮颜色值
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void QUIWidget::setFormInCenter(QWidget *frm)
|
|
|
{
|
|
|
int frmX = frm->width();
|
|
|
int frmY = frm->height();
|
|
|
// 获取传入窗口部件的宽度和高度
|
|
|
QDesktopWidget w;
|
|
|
int deskWidth = w.availableGeometry().width();
|
|
|
int deskHeight = w.availableGeometry().height();
|
|
|
// 获取桌面可用区域的宽度和高度
|
|
|
QPoint movePoint(deskWidth / 2 - frmX / 2, deskHeight / 2 - frmY / 2);
|
|
|
// 计算将窗口部件移动到桌面中心的坐标偏移量
|
|
|
frm->move(movePoint);
|
|
|
// 将窗口部件移动到计算好的位置,使其在桌面中心显示
|
|
|
}
|
|
|
|
|
|
void QUIWidget::setTranslator(const QString &qmFile)
|
|
|
{
|
|
|
QTranslator *translator = new QTranslator(qApp);
|
|
|
// 创建一个QTranslator对象,用于加载翻译文件来实现多语言支持
|
|
|
translator->load(qmFile);
|
|
|
// 加载指定的翻译文件(qm格式)
|
|
|
qApp->installTranslator(translator);
|
|
|
// 将翻译器安装到应用程序中,使其生效
|
|
|
}
|
|
|
|
|
|
void QUIWidget::setCode()
|
|
|
{
|
|
|
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
|
|
|
#if _MSC_VER
|
|
|
QTextCodec *codec = QTextCodec::codecForName("gbk");
|
|
|
// 如果使用的是微软编译器(_MSC_VER)且Qt版本小于等于5.0.0,设置文本编码为GBK编码
|
|
|
#else
|
|
|
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
|
|
// 如果不是微软编译器且Qt版本小于等于5.0.0,设置文本编码为UTF-8编码
|
|
|
#endif
|
|
|
QTextCodec::setCodecForLocale(codec);
|
|
|
QTextCodec::setCodecForCStrings(codec);
|
|
|
QTextCodec::setCodecForTr(codec);
|
|
|
// 将设置的编码应用到本地环境、C字符串以及翻译相关的编码处理中
|
|
|
#else
|
|
|
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
|
|
// 如果Qt版本大于5.0.0,设置文本编码为UTF-8编码
|
|
|
QTextCodec::setCodecForLocale(codec);
|
|
|
// 将编码应用到本地环境编码处理中
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
void QUIWidget::sleep(int sec)
|
|
|
{
|
|
|
QTime dieTime = QTime::currentTime().addMSecs(sec);
|
|
|
// 根据传入的秒数,计算出结束时间(当前时间加上指定的毫秒数)
|
|
|
|
|
|
while (QTime::currentTime() < dieTime) {
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
|
// 在等待时间到达的过程中,处理应用程序的其他事件,避免界面卡死,每次处理事件的超时时间设为100毫秒
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void QUIWidget::setSystemDateTime(const QString &year, const QString &month, const QString &day, const QString &hour, const QString &min, const QString &sec)
|
|
|
{
|
|
|
#ifdef Q_OS_WIN
|
|
|
QProcess p(0);
|
|
|
p.start("cmd");
|
|
|
p.waitForStarted();
|
|
|
// 在Windows系统下,创建一个QProcess对象用于执行命令行操作,启动命令提示符(cmd)并等待启动成功
|
|
|
p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toLatin1());
|
|
|
p.closeWriteChannel();
|
|
|
p.waitForFinished(1000);
|
|
|
p.close();
|
|
|
// 向命令提示符写入设置日期的命令(格式为"date 年-月-日"),关闭写入通道,等待命令执行完成(超时1秒),然后关闭进程
|
|
|
p.start("cmd");
|
|
|
p.waitForStarted();
|
|
|
p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1());
|
|
|
p.closeWriteChannel();
|
|
|
p.waitForFinished(1000);
|
|
|
p.close();
|
|
|
// 再次启动命令提示符,写入设置时间的命令(格式为"time 时:分:秒.00"),同样进行相关操作等待命令执行和关闭进程
|
|
|
#else
|
|
|
QString cmd = QString("date %1%2%3%4%5.%6").arg(month).arg(day).arg(hour).arg(min).arg(year).arg(sec);
|
|
|
system(cmd.toLatin1());
|
|
|
system("hwclock -w");
|
|
|
// 在非Windows系统下(这里假设是类Unix系统),构建设置日期和时间的命令(格式可能根据系统要求不同),先执行设置时间命令,然后执行将硬件时钟写入的命令
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
void QUIWidget::runWithSystem(const QString &strName, const QString &strPath, bool autoRun)
|
|
|
{
|
|
|
#ifdef Q_OS_WIN
|
|
|
QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
|
|
|
// 在Windows系统下,创建QSettings对象用于操作Windows注册表,指定了注册表中用于设置开机自启的键路径
|