更新代码

This commit is contained in:
feiyangqingyun
2024-04-21 14:41:12 +08:00
parent 41188deae0
commit 47835c1fbe
18 changed files with 599 additions and 239 deletions

View File

@@ -9,11 +9,9 @@
int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
QApplication::setAttribute(Qt::AA_Use96Dpi);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
QApplication a(argc, argv);

View File

@@ -4,21 +4,41 @@
#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
QList<QRect> QtHelper::getScreenRects(bool available)
bool QtHelper::useRatio = true;
//full指宽屏/就是将所有屏幕拼接在一起
QList<QRect> QtHelper::getScreenRects(bool available, bool full)
{
QRect rect;
QList<QRect> rects;
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
int screenCount = qApp->screens().count();
QList<QScreen *> screens = qApp->screens();
int screenCount = screens.count();
for (int i = 0; i < screenCount; ++i) {
QScreen *screen = screens.at(i);
rects << (available ? screen->availableGeometry() : screen->geometry());
if (full) {
rect = (available ? screen->availableVirtualGeometry() : screen->virtualGeometry());
} else {
rect = (available ? screen->availableGeometry() : screen->geometry());
}
//需要根据缩放比来重新调整宽高
qreal ratio = (QtHelper::useRatio ? screen->devicePixelRatio() : 1);
rect.setWidth(rect.width() * ratio);
rect.setHeight(rect.height() * ratio);
rects << rect;
}
#else
int screenCount = qApp->desktop()->screenCount();
QDesktopWidget *desk = qApp->desktop();
int screenCount = desk->screenCount();
for (int i = 0; i < screenCount; ++i) {
rects << (available ? desk->availableGeometry(i) : desk->screenGeometry(i));
if (full) {
rect = (available ? desk->geometry() : desk->geometry());
} else {
rect = (available ? desk->availableGeometry(i) : desk->screenGeometry(i));
}
rects << rect;
}
#endif
return rects;
@@ -42,22 +62,23 @@ int QtHelper::getScreenIndex()
return screenIndex;
}
QRect QtHelper::getScreenRect(bool available)
QRect QtHelper::getScreenRect(bool available, bool full)
{
int screenIndex = getScreenIndex();
QList<QRect> rects = getScreenRects(available);
QList<QRect> rects = getScreenRects(available, full);
return rects.at(screenIndex);
}
qreal QtHelper::getScreenRatio(bool devicePixel)
qreal QtHelper::getScreenRatio(int index, bool devicePixel)
{
qreal ratio = 1.0;
int screenIndex = getScreenIndex();
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
//索引-1则自动获取鼠标所在当前屏幕
int screenIndex = (index == -1 ? getScreenIndex() : index);
#if (QT_VERSION >= QT_VERSION_CHECK(5,5,0))
QScreen *screen = qApp->screens().at(screenIndex);
if (devicePixel) {
//需要开启 AA_EnableHighDpiScaling 属性才能正常获取
ratio = screen->devicePixelRatio() * 100;
ratio = screen->devicePixelRatio() * 96;
} else {
ratio = screen->logicalDotsPerInch();
}
@@ -188,10 +209,15 @@ QString QtHelper::getIniValue(const QString &fileName, const QString &key)
return value;
}
QString QtHelper::getIniValue(char *argv[], const QString &key, const QString &dir)
QString QtHelper::getIniValue(char *argv[], const QString &key, const QString &dir, const QString &file)
{
QString path, name;
QtHelper::getCurrentInfo(argv, path, name);
//指定了名称则取指定的名称/防止程序重命名
if (!file.isEmpty()) {
name = file;
}
QString fileName = QString("%1/%2%3.ini").arg(path).arg(dir).arg(name);
return getIniValue(fileName, key);
}
@@ -236,6 +262,33 @@ QStringList QtHelper::getLocalIPs()
return ips;
}
void QtHelper::initLocalIPs(QComboBox *cbox, const QString &defaultIP, bool local127)
{
QStringList ips;
if (local127) {
ips << "127.0.0.1";
}
//添加本地网卡地址集合
ips << QtHelper::getLocalIPs();
//不在网卡地址列表中则取第一个
QString ip = defaultIP;
if (ips.count() > 0) {
ip = ips.contains(ip) ? ip : ips.first();
}
//设置当前下拉框索引
int index = ips.indexOf(ip);
cbox->addItems(ips);
cbox->setCurrentIndex(index < 0 ? 0 : index);
//如果有文本框还要设置文本框的值
if (cbox->lineEdit()) {
cbox->lineEdit()->setText(ip);
}
}
QList<QColor> QtHelper::colors = QList<QColor>();
QList<QColor> QtHelper::getColorList()
{
@@ -416,7 +469,8 @@ void QtHelper::checkRun()
void QtHelper::setStyle()
{
//打印下所有内置风格的名字
qDebug() << TIMEMS << "QStyleFactory::keys" << QStyleFactory::keys();
//qDebug() << TIMEMS << "QStyleFactory::keys" << QStyleFactory::keys();
//设置内置风格
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
qApp->setStyle("Fusion");
@@ -477,22 +531,16 @@ void QtHelper::setFont(int fontSize)
void QtHelper::setCode(bool utf8)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
//如果想要控制台打印信息中文正常就注释掉这个设置
if (utf8) {
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
}
#else
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#endif
//如果想要控制台打印信息中文正常就注释掉这个设置/setCodecForLocale会影响toLocal8Bit函数
if (utf8) {
QTextCodec::setCodecForLocale(codec);
}
}
void QtHelper::setTranslator(const QString &qmFile)
@@ -585,11 +633,16 @@ void QtHelper::initAll(bool utf8, bool style, int fontSize)
#endif
}
#ifdef webengine
#include "qquickwindow.h"
#endif
void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritical)
{
#ifdef Q_OS_WIN
//Qt6.5开始默认是ffmpeg后端但是不成熟需要换成系统的
qputenv("QT_MEDIA_BACKEND", "windows");
#ifdef Q_OS_LINUX
#ifndef Q_OS_ANDROID
//Qt6开始默认用wayland/由于没有坐标系统导致无边框窗体不可用
//qputenv("QT_QPA_PLATFORM", "xcb");
#endif
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
@@ -597,15 +650,17 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
QApplication::setDesktopSettingsAware(desktopSettingsAware);
#endif
bool highDpi = !use96Dpi;
//安卓必须启用高分屏
#ifdef Q_OS_ANDROID
highDpi = true;
use96Dpi = false;
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0))
QtHelper::useRatio = use96Dpi;
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0) && QT_VERSION < QT_VERSION_CHECK(6,0,0))
//开启高分屏缩放支持
if (highDpi) {
if (!use96Dpi) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
}
#endif
@@ -622,7 +677,7 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
//高分屏缩放策略
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
@@ -636,6 +691,13 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
//设置opengl共享上下文
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#ifdef webengine
//修复openglwidget和webengine共存出现黑屏的bug
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
#endif
#endif
}
void QtHelper::initOpenGL(quint8 type, bool checkCardEnable, bool checkVirtualSystem)
@@ -664,6 +726,39 @@ void QtHelper::initOpenGL(quint8 type, bool checkCardEnable, bool checkVirtualSy
#endif
}
QString QtHelper::getStyle(const QString &qssFile)
{
QString qss;
QFile file(qssFile);
if (file.open(QFile::ReadOnly)) {
#if 0
qss = QLatin1String(file.readAll());
#else
//用下面这种方式读取可以不用区分文件编码
QStringList list;
QTextStream stream(&file);
while (!stream.atEnd()) {
QString line;
stream >> line;
list << line;
}
qss = list.join("\n");
#endif
}
return qss.trimmed();
}
void QtHelper::setStyle(const QString &qssFile)
{
QString qss = QtHelper::getStyle(qssFile);
if (!qss.isEmpty()) {
QString paletteColor = qss.mid(20, 7);
qApp->setPalette(QPalette(QColor(paletteColor)));
qApp->setStyleSheet(qss);
}
}
QString QtHelper::doCmd(const QString &program, const QStringList &arguments, int timeout)
{
QString result;
@@ -728,6 +823,7 @@ bool QtHelper::isVirtualSystem()
return virtualSystem;
}
bool QtHelper::replaceCRLF = true;
QVector<int> QtHelper::msgTypes = QVector<int>() << 0 << 1 << 2 << 3 << 4;
QVector<QString> QtHelper::msgKeys = QVector<QString>() << QString::fromUtf8("发送") << QString::fromUtf8("接收") << QString::fromUtf8("解析") << QString::fromUtf8("错误") << QString::fromUtf8("提示");
QVector<QColor> QtHelper::msgColors = QVector<QColor>() << QColor("#3BA372") << QColor("#EE6668") << QColor("#9861B4") << QColor("#FA8359") << QColor("#22A3A9");
@@ -758,8 +854,11 @@ QString QtHelper::appendMsg(QTextEdit *textEdit, int type, const QString &data,
//过滤回车换行符
QString strData = data;
strData.replace("\r", "");
strData.replace("\n", "");
if (replaceCRLF) {
strData.replace("\r", "");
strData.replace("\n", "");
}
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
textEdit->append(strData);
currentCount++;
@@ -809,7 +908,7 @@ void QtHelper::showMessageBoxInfo(const QString &text, int closeSec, bool exec)
{
QMessageBox box(QMessageBox::Information, "提示", text);
box.setStandardButtons(QMessageBox::Yes);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.button(QMessageBox::Yes)->setText("确 定");
box.exec();
//QMessageBox::information(0, "提示", info, QMessageBox::Yes);
}
@@ -818,7 +917,7 @@ void QtHelper::showMessageBoxError(const QString &text, int closeSec, bool exec)
{
QMessageBox box(QMessageBox::Critical, "错误", text);
box.setStandardButtons(QMessageBox::Yes);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.button(QMessageBox::Yes)->setText("确 定");
box.exec();
//QMessageBox::critical(0, "错误", info, QMessageBox::Yes);
}
@@ -827,8 +926,8 @@ int QtHelper::showMessageBoxQuestion(const QString &text)
{
QMessageBox box(QMessageBox::Question, "询问", text);
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.setButtonText(QMessageBox::No, QString("取 消"));
box.button(QMessageBox::Yes)->setText("确 定");
box.button(QMessageBox::No)->setText("取 消");
return box.exec();
//return QMessageBox::question(0, "询问", info, QMessageBox::Yes | QMessageBox::No);
}
@@ -1165,16 +1264,16 @@ QString QtHelper::getSizeString(quint64 size)
void QtHelper::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);
QProcess p;
//先设置日期
p.start("cmd");
p.start("cmd", QStringList());
p.waitForStarted();
p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toLatin1());
p.closeWriteChannel();
p.waitForFinished(1000);
p.close();
//再设置时间
p.start("cmd");
p.start("cmd", QStringList());
p.waitForStarted();
p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1());
p.closeWriteChannel();
@@ -1203,11 +1302,11 @@ void QtHelper::runWithSystem(const QString &fileName, const QString &filePath, b
#endif
}
void QtHelper::runBin(const QString &path, const QString &name)
void QtHelper::start(const QString &path, const QString &name, bool bin)
{
#ifdef Q_OS_WIN
QString cmd1 = "tasklist";
QString cmd2 = QString("%1/%2.exe").arg(path).arg(name);
QString cmd2 = QString("%1/%2%3").arg(path).arg(name).arg(bin ? ".exe" : "");
#else
QString cmd1 = "ps -aux";
QString cmd2 = QString("%1/%2").arg(path).arg(name);
@@ -1215,7 +1314,7 @@ void QtHelper::runBin(const QString &path, const QString &name)
#ifndef Q_OS_WASM
QProcess p;
p.start(cmd1);
p.start(cmd1, QStringList());
if (p.waitForFinished()) {
QString result = p.readAll();
if (result.contains(name)) {
@@ -1228,7 +1327,11 @@ void QtHelper::runBin(const QString &path, const QString &name)
cmd2 = "\"" + cmd2 + "\"";
}
//QProcess::execute(cmd2);
QProcess::startDetached(cmd2);
//切换到当前目录
QDir::setCurrent(path);
//QProcess::execute(cmd2, QStringList());
QProcess::startDetached(cmd2, QStringList());
//执行完成后切换回默认目录
QDir::setCurrent(QtHelper::appPath());
#endif
}

View File

@@ -7,10 +7,11 @@ class QtHelper
{
public:
//获取所有屏幕区域/当前鼠标所在屏幕索引/区域尺寸/缩放系数
static QList<QRect> getScreenRects(bool available = true);
static bool useRatio;
static QList<QRect> getScreenRects(bool available = true, bool full = false);
static int getScreenIndex();
static QRect getScreenRect(bool available = true);
static qreal getScreenRatio(bool devicePixel = false);
static QRect getScreenRect(bool available = true, bool full = false);
static qreal getScreenRatio(int index = -1, bool devicePixel = false);
//矫正当前鼠标所在屏幕居中尺寸
static QRect checkCenterRect(QRect &rect, bool available = true);
@@ -34,10 +35,12 @@ public:
static void getCurrentInfo(char *argv[], QString &path, QString &name);
//程序最前面读取配置文件节点的值
static QString getIniValue(const QString &fileName, const QString &key);
static QString getIniValue(char *argv[], const QString &key, const QString &dir = QString());
static QString getIniValue(char *argv[], const QString &key, const QString &dir = QString(), const QString &file = QString());
//获取本地网卡IP集合
static QStringList getLocalIPs();
//添加网卡集合并根据默认值设置当前项
static void initLocalIPs(QComboBox *cbox, const QString &defaultIP, bool local127 = true);
//获取内置颜色集合
static QList<QColor> colors;
@@ -84,10 +87,15 @@ public:
//一次性设置所有包括编码样式字体等
static void initAll(bool utf8 = true, bool style = true, int fontSize = 13);
//初始化main函数最前面执行的一段代码
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = true, bool logCritical = true);
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = false, bool logCritical = true);
//初始化opengl类型(1=AA_UseDesktopOpenGL 2=AA_UseOpenGLES 3=AA_UseSoftwareOpenGL)
static void initOpenGL(quint8 type = 0, bool checkCardEnable = false, bool checkVirtualSystem = false);
//读取qss文件获取样式表内容
static QString getStyle(const QString &qssFile);
//设置qss文件到全局样式
static void setStyle(const QString &qssFile);
//执行命令行返回执行结果
static QString doCmd(const QString &program, const QStringList &arguments, int timeout = 1000);
//获取显卡是否被禁用
@@ -96,6 +104,7 @@ public:
static bool isVirtualSystem();
//插入消息
static bool replaceCRLF;
static QVector<int> msgTypes;
static QVector<QString> msgKeys;
static QVector<QColor> msgColors;
@@ -173,8 +182,9 @@ public:
//设置开机自启动
static void runWithSystem(bool autoRun = true);
static void runWithSystem(const QString &fileName, const QString &filePath, bool autoRun = true);
//启动运行程序(已经在运行则不启动)
static void runBin(const QString &path, const QString &name);
static void start(const QString &path, const QString &name, bool bin = true);
};
#endif // QTHELPER_H

View File

@@ -3,13 +3,7 @@
int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
QApplication::setAttribute(Qt::AA_Use96Dpi);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
#endif
QtHelper::initMain();
QApplication a(argc, argv);
a.setWindowIcon(QIcon(":/main.ico"));
@@ -24,7 +18,7 @@ int main(int argc, char *argv[])
AppData::readDeviceData();
frmComTool w;
w.setWindowTitle("串口调试助手 V2023 (QQ: 517216493 WX: feiyangqingyun)");
w.setWindowTitle("串口调试助手 V2024 (QQ: 517216493 WX: feiyangqingyun)");
w.resize(900, 650);
QtHelper::setFormInCenter(&w);
w.show();

View File

@@ -4,21 +4,41 @@
#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
QList<QRect> QtHelper::getScreenRects(bool available)
bool QtHelper::useRatio = true;
//full指宽屏/就是将所有屏幕拼接在一起
QList<QRect> QtHelper::getScreenRects(bool available, bool full)
{
QRect rect;
QList<QRect> rects;
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
int screenCount = qApp->screens().count();
QList<QScreen *> screens = qApp->screens();
int screenCount = screens.count();
for (int i = 0; i < screenCount; ++i) {
QScreen *screen = screens.at(i);
rects << (available ? screen->availableGeometry() : screen->geometry());
if (full) {
rect = (available ? screen->availableVirtualGeometry() : screen->virtualGeometry());
} else {
rect = (available ? screen->availableGeometry() : screen->geometry());
}
//需要根据缩放比来重新调整宽高
qreal ratio = (QtHelper::useRatio ? screen->devicePixelRatio() : 1);
rect.setWidth(rect.width() * ratio);
rect.setHeight(rect.height() * ratio);
rects << rect;
}
#else
int screenCount = qApp->desktop()->screenCount();
QDesktopWidget *desk = qApp->desktop();
int screenCount = desk->screenCount();
for (int i = 0; i < screenCount; ++i) {
rects << (available ? desk->availableGeometry(i) : desk->screenGeometry(i));
if (full) {
rect = (available ? desk->geometry() : desk->geometry());
} else {
rect = (available ? desk->availableGeometry(i) : desk->screenGeometry(i));
}
rects << rect;
}
#endif
return rects;
@@ -42,22 +62,23 @@ int QtHelper::getScreenIndex()
return screenIndex;
}
QRect QtHelper::getScreenRect(bool available)
QRect QtHelper::getScreenRect(bool available, bool full)
{
int screenIndex = getScreenIndex();
QList<QRect> rects = getScreenRects(available);
QList<QRect> rects = getScreenRects(available, full);
return rects.at(screenIndex);
}
qreal QtHelper::getScreenRatio(bool devicePixel)
qreal QtHelper::getScreenRatio(int index, bool devicePixel)
{
qreal ratio = 1.0;
int screenIndex = getScreenIndex();
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
//索引-1则自动获取鼠标所在当前屏幕
int screenIndex = (index == -1 ? getScreenIndex() : index);
#if (QT_VERSION >= QT_VERSION_CHECK(5,5,0))
QScreen *screen = qApp->screens().at(screenIndex);
if (devicePixel) {
//需要开启 AA_EnableHighDpiScaling 属性才能正常获取
ratio = screen->devicePixelRatio() * 100;
ratio = screen->devicePixelRatio() * 96;
} else {
ratio = screen->logicalDotsPerInch();
}
@@ -188,10 +209,15 @@ QString QtHelper::getIniValue(const QString &fileName, const QString &key)
return value;
}
QString QtHelper::getIniValue(char *argv[], const QString &key, const QString &dir)
QString QtHelper::getIniValue(char *argv[], const QString &key, const QString &dir, const QString &file)
{
QString path, name;
QtHelper::getCurrentInfo(argv, path, name);
//指定了名称则取指定的名称/防止程序重命名
if (!file.isEmpty()) {
name = file;
}
QString fileName = QString("%1/%2%3.ini").arg(path).arg(dir).arg(name);
return getIniValue(fileName, key);
}
@@ -236,6 +262,33 @@ QStringList QtHelper::getLocalIPs()
return ips;
}
void QtHelper::initLocalIPs(QComboBox *cbox, const QString &defaultIP, bool local127)
{
QStringList ips;
if (local127) {
ips << "127.0.0.1";
}
//添加本地网卡地址集合
ips << QtHelper::getLocalIPs();
//不在网卡地址列表中则取第一个
QString ip = defaultIP;
if (ips.count() > 0) {
ip = ips.contains(ip) ? ip : ips.first();
}
//设置当前下拉框索引
int index = ips.indexOf(ip);
cbox->addItems(ips);
cbox->setCurrentIndex(index < 0 ? 0 : index);
//如果有文本框还要设置文本框的值
if (cbox->lineEdit()) {
cbox->lineEdit()->setText(ip);
}
}
QList<QColor> QtHelper::colors = QList<QColor>();
QList<QColor> QtHelper::getColorList()
{
@@ -416,7 +469,8 @@ void QtHelper::checkRun()
void QtHelper::setStyle()
{
//打印下所有内置风格的名字
qDebug() << TIMEMS << "QStyleFactory::keys" << QStyleFactory::keys();
//qDebug() << TIMEMS << "QStyleFactory::keys" << QStyleFactory::keys();
//设置内置风格
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
qApp->setStyle("Fusion");
@@ -477,22 +531,16 @@ void QtHelper::setFont(int fontSize)
void QtHelper::setCode(bool utf8)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
//如果想要控制台打印信息中文正常就注释掉这个设置
if (utf8) {
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
}
#else
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#endif
//如果想要控制台打印信息中文正常就注释掉这个设置/setCodecForLocale会影响toLocal8Bit函数
if (utf8) {
QTextCodec::setCodecForLocale(codec);
}
}
void QtHelper::setTranslator(const QString &qmFile)
@@ -585,11 +633,16 @@ void QtHelper::initAll(bool utf8, bool style, int fontSize)
#endif
}
#ifdef webengine
#include "qquickwindow.h"
#endif
void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritical)
{
#ifdef Q_OS_WIN
//Qt6.5开始默认是ffmpeg后端但是不成熟需要换成系统的
qputenv("QT_MEDIA_BACKEND", "windows");
#ifdef Q_OS_LINUX
#ifndef Q_OS_ANDROID
//Qt6开始默认用wayland/由于没有坐标系统导致无边框窗体不可用
//qputenv("QT_QPA_PLATFORM", "xcb");
#endif
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
@@ -597,15 +650,17 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
QApplication::setDesktopSettingsAware(desktopSettingsAware);
#endif
bool highDpi = !use96Dpi;
//安卓必须启用高分屏
#ifdef Q_OS_ANDROID
highDpi = true;
use96Dpi = false;
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0))
QtHelper::useRatio = use96Dpi;
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0) && QT_VERSION < QT_VERSION_CHECK(6,0,0))
//开启高分屏缩放支持
if (highDpi) {
if (!use96Dpi) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
}
#endif
@@ -622,7 +677,7 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
//高分屏缩放策略
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
@@ -636,6 +691,13 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
//设置opengl共享上下文
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#ifdef webengine
//修复openglwidget和webengine共存出现黑屏的bug
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
#endif
#endif
}
void QtHelper::initOpenGL(quint8 type, bool checkCardEnable, bool checkVirtualSystem)
@@ -664,6 +726,39 @@ void QtHelper::initOpenGL(quint8 type, bool checkCardEnable, bool checkVirtualSy
#endif
}
QString QtHelper::getStyle(const QString &qssFile)
{
QString qss;
QFile file(qssFile);
if (file.open(QFile::ReadOnly)) {
#if 0
qss = QLatin1String(file.readAll());
#else
//用下面这种方式读取可以不用区分文件编码
QStringList list;
QTextStream stream(&file);
while (!stream.atEnd()) {
QString line;
stream >> line;
list << line;
}
qss = list.join("\n");
#endif
}
return qss.trimmed();
}
void QtHelper::setStyle(const QString &qssFile)
{
QString qss = QtHelper::getStyle(qssFile);
if (!qss.isEmpty()) {
QString paletteColor = qss.mid(20, 7);
qApp->setPalette(QPalette(QColor(paletteColor)));
qApp->setStyleSheet(qss);
}
}
QString QtHelper::doCmd(const QString &program, const QStringList &arguments, int timeout)
{
QString result;
@@ -728,6 +823,7 @@ bool QtHelper::isVirtualSystem()
return virtualSystem;
}
bool QtHelper::replaceCRLF = true;
QVector<int> QtHelper::msgTypes = QVector<int>() << 0 << 1 << 2 << 3 << 4;
QVector<QString> QtHelper::msgKeys = QVector<QString>() << QString::fromUtf8("发送") << QString::fromUtf8("接收") << QString::fromUtf8("解析") << QString::fromUtf8("错误") << QString::fromUtf8("提示");
QVector<QColor> QtHelper::msgColors = QVector<QColor>() << QColor("#3BA372") << QColor("#EE6668") << QColor("#9861B4") << QColor("#FA8359") << QColor("#22A3A9");
@@ -758,8 +854,11 @@ QString QtHelper::appendMsg(QTextEdit *textEdit, int type, const QString &data,
//过滤回车换行符
QString strData = data;
strData.replace("\r", "");
strData.replace("\n", "");
if (replaceCRLF) {
strData.replace("\r", "");
strData.replace("\n", "");
}
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
textEdit->append(strData);
currentCount++;
@@ -809,7 +908,7 @@ void QtHelper::showMessageBoxInfo(const QString &text, int closeSec, bool exec)
{
QMessageBox box(QMessageBox::Information, "提示", text);
box.setStandardButtons(QMessageBox::Yes);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.button(QMessageBox::Yes)->setText("确 定");
box.exec();
//QMessageBox::information(0, "提示", info, QMessageBox::Yes);
}
@@ -818,7 +917,7 @@ void QtHelper::showMessageBoxError(const QString &text, int closeSec, bool exec)
{
QMessageBox box(QMessageBox::Critical, "错误", text);
box.setStandardButtons(QMessageBox::Yes);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.button(QMessageBox::Yes)->setText("确 定");
box.exec();
//QMessageBox::critical(0, "错误", info, QMessageBox::Yes);
}
@@ -827,8 +926,8 @@ int QtHelper::showMessageBoxQuestion(const QString &text)
{
QMessageBox box(QMessageBox::Question, "询问", text);
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.setButtonText(QMessageBox::No, QString("取 消"));
box.button(QMessageBox::Yes)->setText("确 定");
box.button(QMessageBox::No)->setText("取 消");
return box.exec();
//return QMessageBox::question(0, "询问", info, QMessageBox::Yes | QMessageBox::No);
}
@@ -1165,16 +1264,16 @@ QString QtHelper::getSizeString(quint64 size)
void QtHelper::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);
QProcess p;
//先设置日期
p.start("cmd");
p.start("cmd", QStringList());
p.waitForStarted();
p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toLatin1());
p.closeWriteChannel();
p.waitForFinished(1000);
p.close();
//再设置时间
p.start("cmd");
p.start("cmd", QStringList());
p.waitForStarted();
p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1());
p.closeWriteChannel();
@@ -1203,11 +1302,11 @@ void QtHelper::runWithSystem(const QString &fileName, const QString &filePath, b
#endif
}
void QtHelper::runBin(const QString &path, const QString &name)
void QtHelper::start(const QString &path, const QString &name, bool bin)
{
#ifdef Q_OS_WIN
QString cmd1 = "tasklist";
QString cmd2 = QString("%1/%2.exe").arg(path).arg(name);
QString cmd2 = QString("%1/%2%3").arg(path).arg(name).arg(bin ? ".exe" : "");
#else
QString cmd1 = "ps -aux";
QString cmd2 = QString("%1/%2").arg(path).arg(name);
@@ -1215,7 +1314,7 @@ void QtHelper::runBin(const QString &path, const QString &name)
#ifndef Q_OS_WASM
QProcess p;
p.start(cmd1);
p.start(cmd1, QStringList());
if (p.waitForFinished()) {
QString result = p.readAll();
if (result.contains(name)) {
@@ -1228,7 +1327,11 @@ void QtHelper::runBin(const QString &path, const QString &name)
cmd2 = "\"" + cmd2 + "\"";
}
//QProcess::execute(cmd2);
QProcess::startDetached(cmd2);
//切换到当前目录
QDir::setCurrent(path);
//QProcess::execute(cmd2, QStringList());
QProcess::startDetached(cmd2, QStringList());
//执行完成后切换回默认目录
QDir::setCurrent(QtHelper::appPath());
#endif
}

View File

@@ -7,10 +7,11 @@ class QtHelper
{
public:
//获取所有屏幕区域/当前鼠标所在屏幕索引/区域尺寸/缩放系数
static QList<QRect> getScreenRects(bool available = true);
static bool useRatio;
static QList<QRect> getScreenRects(bool available = true, bool full = false);
static int getScreenIndex();
static QRect getScreenRect(bool available = true);
static qreal getScreenRatio(bool devicePixel = false);
static QRect getScreenRect(bool available = true, bool full = false);
static qreal getScreenRatio(int index = -1, bool devicePixel = false);
//矫正当前鼠标所在屏幕居中尺寸
static QRect checkCenterRect(QRect &rect, bool available = true);
@@ -34,10 +35,12 @@ public:
static void getCurrentInfo(char *argv[], QString &path, QString &name);
//程序最前面读取配置文件节点的值
static QString getIniValue(const QString &fileName, const QString &key);
static QString getIniValue(char *argv[], const QString &key, const QString &dir = QString());
static QString getIniValue(char *argv[], const QString &key, const QString &dir = QString(), const QString &file = QString());
//获取本地网卡IP集合
static QStringList getLocalIPs();
//添加网卡集合并根据默认值设置当前项
static void initLocalIPs(QComboBox *cbox, const QString &defaultIP, bool local127 = true);
//获取内置颜色集合
static QList<QColor> colors;
@@ -84,10 +87,15 @@ public:
//一次性设置所有包括编码样式字体等
static void initAll(bool utf8 = true, bool style = true, int fontSize = 13);
//初始化main函数最前面执行的一段代码
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = true, bool logCritical = true);
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = false, bool logCritical = true);
//初始化opengl类型(1=AA_UseDesktopOpenGL 2=AA_UseOpenGLES 3=AA_UseSoftwareOpenGL)
static void initOpenGL(quint8 type = 0, bool checkCardEnable = false, bool checkVirtualSystem = false);
//读取qss文件获取样式表内容
static QString getStyle(const QString &qssFile);
//设置qss文件到全局样式
static void setStyle(const QString &qssFile);
//执行命令行返回执行结果
static QString doCmd(const QString &program, const QStringList &arguments, int timeout = 1000);
//获取显卡是否被禁用
@@ -96,6 +104,7 @@ public:
static bool isVirtualSystem();
//插入消息
static bool replaceCRLF;
static QVector<int> msgTypes;
static QVector<QString> msgKeys;
static QVector<QColor> msgColors;
@@ -173,8 +182,9 @@ public:
//设置开机自启动
static void runWithSystem(bool autoRun = true);
static void runWithSystem(const QString &fileName, const QString &filePath, bool autoRun = true);
//启动运行程序(已经在运行则不启动)
static void runBin(const QString &path, const QString &name);
static void start(const QString &path, const QString &name, bool bin = true);
};
#endif // QTHELPER_H

View File

@@ -3,13 +3,7 @@
int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
QApplication::setAttribute(Qt::AA_Use96Dpi);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
#endif
QtHelper::initMain();
QApplication a(argc, argv);
a.setWindowIcon(QIcon(":/main.ico"));
@@ -21,7 +15,7 @@ int main(int argc, char *argv[])
AppConfig::readConfig();
frmMain w;
w.setWindowTitle("网络中转服务器 V2023 (QQ: 517216493 WX: feiyangqingyun)");
w.setWindowTitle("网络中转服务器 V2024 (QQ: 517216493 WX: feiyangqingyun)");
w.resize(900, 650);
QtHelper::setFormInCenter(&w);
w.show();

View File

@@ -4,21 +4,41 @@
#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
QList<QRect> QtHelper::getScreenRects(bool available)
bool QtHelper::useRatio = true;
//full指宽屏/就是将所有屏幕拼接在一起
QList<QRect> QtHelper::getScreenRects(bool available, bool full)
{
QRect rect;
QList<QRect> rects;
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
int screenCount = qApp->screens().count();
QList<QScreen *> screens = qApp->screens();
int screenCount = screens.count();
for (int i = 0; i < screenCount; ++i) {
QScreen *screen = screens.at(i);
rects << (available ? screen->availableGeometry() : screen->geometry());
if (full) {
rect = (available ? screen->availableVirtualGeometry() : screen->virtualGeometry());
} else {
rect = (available ? screen->availableGeometry() : screen->geometry());
}
//需要根据缩放比来重新调整宽高
qreal ratio = (QtHelper::useRatio ? screen->devicePixelRatio() : 1);
rect.setWidth(rect.width() * ratio);
rect.setHeight(rect.height() * ratio);
rects << rect;
}
#else
int screenCount = qApp->desktop()->screenCount();
QDesktopWidget *desk = qApp->desktop();
int screenCount = desk->screenCount();
for (int i = 0; i < screenCount; ++i) {
rects << (available ? desk->availableGeometry(i) : desk->screenGeometry(i));
if (full) {
rect = (available ? desk->geometry() : desk->geometry());
} else {
rect = (available ? desk->availableGeometry(i) : desk->screenGeometry(i));
}
rects << rect;
}
#endif
return rects;
@@ -42,22 +62,23 @@ int QtHelper::getScreenIndex()
return screenIndex;
}
QRect QtHelper::getScreenRect(bool available)
QRect QtHelper::getScreenRect(bool available, bool full)
{
int screenIndex = getScreenIndex();
QList<QRect> rects = getScreenRects(available);
QList<QRect> rects = getScreenRects(available, full);
return rects.at(screenIndex);
}
qreal QtHelper::getScreenRatio(bool devicePixel)
qreal QtHelper::getScreenRatio(int index, bool devicePixel)
{
qreal ratio = 1.0;
int screenIndex = getScreenIndex();
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
//索引-1则自动获取鼠标所在当前屏幕
int screenIndex = (index == -1 ? getScreenIndex() : index);
#if (QT_VERSION >= QT_VERSION_CHECK(5,5,0))
QScreen *screen = qApp->screens().at(screenIndex);
if (devicePixel) {
//需要开启 AA_EnableHighDpiScaling 属性才能正常获取
ratio = screen->devicePixelRatio() * 100;
ratio = screen->devicePixelRatio() * 96;
} else {
ratio = screen->logicalDotsPerInch();
}
@@ -188,10 +209,15 @@ QString QtHelper::getIniValue(const QString &fileName, const QString &key)
return value;
}
QString QtHelper::getIniValue(char *argv[], const QString &key, const QString &dir)
QString QtHelper::getIniValue(char *argv[], const QString &key, const QString &dir, const QString &file)
{
QString path, name;
QtHelper::getCurrentInfo(argv, path, name);
//指定了名称则取指定的名称/防止程序重命名
if (!file.isEmpty()) {
name = file;
}
QString fileName = QString("%1/%2%3.ini").arg(path).arg(dir).arg(name);
return getIniValue(fileName, key);
}
@@ -236,6 +262,33 @@ QStringList QtHelper::getLocalIPs()
return ips;
}
void QtHelper::initLocalIPs(QComboBox *cbox, const QString &defaultIP, bool local127)
{
QStringList ips;
if (local127) {
ips << "127.0.0.1";
}
//添加本地网卡地址集合
ips << QtHelper::getLocalIPs();
//不在网卡地址列表中则取第一个
QString ip = defaultIP;
if (ips.count() > 0) {
ip = ips.contains(ip) ? ip : ips.first();
}
//设置当前下拉框索引
int index = ips.indexOf(ip);
cbox->addItems(ips);
cbox->setCurrentIndex(index < 0 ? 0 : index);
//如果有文本框还要设置文本框的值
if (cbox->lineEdit()) {
cbox->lineEdit()->setText(ip);
}
}
QList<QColor> QtHelper::colors = QList<QColor>();
QList<QColor> QtHelper::getColorList()
{
@@ -416,7 +469,8 @@ void QtHelper::checkRun()
void QtHelper::setStyle()
{
//打印下所有内置风格的名字
qDebug() << TIMEMS << "QStyleFactory::keys" << QStyleFactory::keys();
//qDebug() << TIMEMS << "QStyleFactory::keys" << QStyleFactory::keys();
//设置内置风格
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
qApp->setStyle("Fusion");
@@ -477,22 +531,16 @@ void QtHelper::setFont(int fontSize)
void QtHelper::setCode(bool utf8)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
//如果想要控制台打印信息中文正常就注释掉这个设置
if (utf8) {
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
}
#else
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#endif
//如果想要控制台打印信息中文正常就注释掉这个设置/setCodecForLocale会影响toLocal8Bit函数
if (utf8) {
QTextCodec::setCodecForLocale(codec);
}
}
void QtHelper::setTranslator(const QString &qmFile)
@@ -585,11 +633,16 @@ void QtHelper::initAll(bool utf8, bool style, int fontSize)
#endif
}
#ifdef webengine
#include "qquickwindow.h"
#endif
void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritical)
{
#ifdef Q_OS_WIN
//Qt6.5开始默认是ffmpeg后端但是不成熟需要换成系统的
qputenv("QT_MEDIA_BACKEND", "windows");
#ifdef Q_OS_LINUX
#ifndef Q_OS_ANDROID
//Qt6开始默认用wayland/由于没有坐标系统导致无边框窗体不可用
//qputenv("QT_QPA_PLATFORM", "xcb");
#endif
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
@@ -597,15 +650,17 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
QApplication::setDesktopSettingsAware(desktopSettingsAware);
#endif
bool highDpi = !use96Dpi;
//安卓必须启用高分屏
#ifdef Q_OS_ANDROID
highDpi = true;
use96Dpi = false;
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0))
QtHelper::useRatio = use96Dpi;
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0) && QT_VERSION < QT_VERSION_CHECK(6,0,0))
//开启高分屏缩放支持
if (highDpi) {
if (!use96Dpi) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
}
#endif
@@ -622,7 +677,7 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
//高分屏缩放策略
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
@@ -636,6 +691,13 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
//设置opengl共享上下文
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#ifdef webengine
//修复openglwidget和webengine共存出现黑屏的bug
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
#endif
#endif
}
void QtHelper::initOpenGL(quint8 type, bool checkCardEnable, bool checkVirtualSystem)
@@ -664,6 +726,39 @@ void QtHelper::initOpenGL(quint8 type, bool checkCardEnable, bool checkVirtualSy
#endif
}
QString QtHelper::getStyle(const QString &qssFile)
{
QString qss;
QFile file(qssFile);
if (file.open(QFile::ReadOnly)) {
#if 0
qss = QLatin1String(file.readAll());
#else
//用下面这种方式读取可以不用区分文件编码
QStringList list;
QTextStream stream(&file);
while (!stream.atEnd()) {
QString line;
stream >> line;
list << line;
}
qss = list.join("\n");
#endif
}
return qss.trimmed();
}
void QtHelper::setStyle(const QString &qssFile)
{
QString qss = QtHelper::getStyle(qssFile);
if (!qss.isEmpty()) {
QString paletteColor = qss.mid(20, 7);
qApp->setPalette(QPalette(QColor(paletteColor)));
qApp->setStyleSheet(qss);
}
}
QString QtHelper::doCmd(const QString &program, const QStringList &arguments, int timeout)
{
QString result;
@@ -728,6 +823,7 @@ bool QtHelper::isVirtualSystem()
return virtualSystem;
}
bool QtHelper::replaceCRLF = true;
QVector<int> QtHelper::msgTypes = QVector<int>() << 0 << 1 << 2 << 3 << 4;
QVector<QString> QtHelper::msgKeys = QVector<QString>() << QString::fromUtf8("发送") << QString::fromUtf8("接收") << QString::fromUtf8("解析") << QString::fromUtf8("错误") << QString::fromUtf8("提示");
QVector<QColor> QtHelper::msgColors = QVector<QColor>() << QColor("#3BA372") << QColor("#EE6668") << QColor("#9861B4") << QColor("#FA8359") << QColor("#22A3A9");
@@ -758,8 +854,11 @@ QString QtHelper::appendMsg(QTextEdit *textEdit, int type, const QString &data,
//过滤回车换行符
QString strData = data;
strData.replace("\r", "");
strData.replace("\n", "");
if (replaceCRLF) {
strData.replace("\r", "");
strData.replace("\n", "");
}
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
textEdit->append(strData);
currentCount++;
@@ -809,7 +908,7 @@ void QtHelper::showMessageBoxInfo(const QString &text, int closeSec, bool exec)
{
QMessageBox box(QMessageBox::Information, "提示", text);
box.setStandardButtons(QMessageBox::Yes);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.button(QMessageBox::Yes)->setText("确 定");
box.exec();
//QMessageBox::information(0, "提示", info, QMessageBox::Yes);
}
@@ -818,7 +917,7 @@ void QtHelper::showMessageBoxError(const QString &text, int closeSec, bool exec)
{
QMessageBox box(QMessageBox::Critical, "错误", text);
box.setStandardButtons(QMessageBox::Yes);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.button(QMessageBox::Yes)->setText("确 定");
box.exec();
//QMessageBox::critical(0, "错误", info, QMessageBox::Yes);
}
@@ -827,8 +926,8 @@ int QtHelper::showMessageBoxQuestion(const QString &text)
{
QMessageBox box(QMessageBox::Question, "询问", text);
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
box.setButtonText(QMessageBox::Yes, QString("确 定"));
box.setButtonText(QMessageBox::No, QString("取 消"));
box.button(QMessageBox::Yes)->setText("确 定");
box.button(QMessageBox::No)->setText("取 消");
return box.exec();
//return QMessageBox::question(0, "询问", info, QMessageBox::Yes | QMessageBox::No);
}
@@ -1165,16 +1264,16 @@ QString QtHelper::getSizeString(quint64 size)
void QtHelper::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);
QProcess p;
//先设置日期
p.start("cmd");
p.start("cmd", QStringList());
p.waitForStarted();
p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toLatin1());
p.closeWriteChannel();
p.waitForFinished(1000);
p.close();
//再设置时间
p.start("cmd");
p.start("cmd", QStringList());
p.waitForStarted();
p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1());
p.closeWriteChannel();
@@ -1203,11 +1302,11 @@ void QtHelper::runWithSystem(const QString &fileName, const QString &filePath, b
#endif
}
void QtHelper::runBin(const QString &path, const QString &name)
void QtHelper::start(const QString &path, const QString &name, bool bin)
{
#ifdef Q_OS_WIN
QString cmd1 = "tasklist";
QString cmd2 = QString("%1/%2.exe").arg(path).arg(name);
QString cmd2 = QString("%1/%2%3").arg(path).arg(name).arg(bin ? ".exe" : "");
#else
QString cmd1 = "ps -aux";
QString cmd2 = QString("%1/%2").arg(path).arg(name);
@@ -1215,7 +1314,7 @@ void QtHelper::runBin(const QString &path, const QString &name)
#ifndef Q_OS_WASM
QProcess p;
p.start(cmd1);
p.start(cmd1, QStringList());
if (p.waitForFinished()) {
QString result = p.readAll();
if (result.contains(name)) {
@@ -1228,7 +1327,11 @@ void QtHelper::runBin(const QString &path, const QString &name)
cmd2 = "\"" + cmd2 + "\"";
}
//QProcess::execute(cmd2);
QProcess::startDetached(cmd2);
//切换到当前目录
QDir::setCurrent(path);
//QProcess::execute(cmd2, QStringList());
QProcess::startDetached(cmd2, QStringList());
//执行完成后切换回默认目录
QDir::setCurrent(QtHelper::appPath());
#endif
}

View File

@@ -7,10 +7,11 @@ class QtHelper
{
public:
//获取所有屏幕区域/当前鼠标所在屏幕索引/区域尺寸/缩放系数
static QList<QRect> getScreenRects(bool available = true);
static bool useRatio;
static QList<QRect> getScreenRects(bool available = true, bool full = false);
static int getScreenIndex();
static QRect getScreenRect(bool available = true);
static qreal getScreenRatio(bool devicePixel = false);
static QRect getScreenRect(bool available = true, bool full = false);
static qreal getScreenRatio(int index = -1, bool devicePixel = false);
//矫正当前鼠标所在屏幕居中尺寸
static QRect checkCenterRect(QRect &rect, bool available = true);
@@ -34,10 +35,12 @@ public:
static void getCurrentInfo(char *argv[], QString &path, QString &name);
//程序最前面读取配置文件节点的值
static QString getIniValue(const QString &fileName, const QString &key);
static QString getIniValue(char *argv[], const QString &key, const QString &dir = QString());
static QString getIniValue(char *argv[], const QString &key, const QString &dir = QString(), const QString &file = QString());
//获取本地网卡IP集合
static QStringList getLocalIPs();
//添加网卡集合并根据默认值设置当前项
static void initLocalIPs(QComboBox *cbox, const QString &defaultIP, bool local127 = true);
//获取内置颜色集合
static QList<QColor> colors;
@@ -84,10 +87,15 @@ public:
//一次性设置所有包括编码样式字体等
static void initAll(bool utf8 = true, bool style = true, int fontSize = 13);
//初始化main函数最前面执行的一段代码
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = true, bool logCritical = true);
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = false, bool logCritical = true);
//初始化opengl类型(1=AA_UseDesktopOpenGL 2=AA_UseOpenGLES 3=AA_UseSoftwareOpenGL)
static void initOpenGL(quint8 type = 0, bool checkCardEnable = false, bool checkVirtualSystem = false);
//读取qss文件获取样式表内容
static QString getStyle(const QString &qssFile);
//设置qss文件到全局样式
static void setStyle(const QString &qssFile);
//执行命令行返回执行结果
static QString doCmd(const QString &program, const QStringList &arguments, int timeout = 1000);
//获取显卡是否被禁用
@@ -96,6 +104,7 @@ public:
static bool isVirtualSystem();
//插入消息
static bool replaceCRLF;
static QVector<int> msgTypes;
static QVector<QString> msgKeys;
static QVector<QColor> msgColors;
@@ -173,8 +182,9 @@ public:
//设置开机自启动
static void runWithSystem(bool autoRun = true);
static void runWithSystem(const QString &fileName, const QString &filePath, bool autoRun = true);
//启动运行程序(已经在运行则不启动)
static void runBin(const QString &path, const QString &name);
static void start(const QString &path, const QString &name, bool bin = true);
};
#endif // QTHELPER_H

View File

@@ -3,13 +3,7 @@
int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
QApplication::setAttribute(Qt::AA_Use96Dpi);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
#endif
QtHelper::initMain();
QApplication a(argc, argv);
a.setWindowIcon(QIcon(":/main.ico"));
@@ -25,7 +19,7 @@ int main(int argc, char *argv[])
AppData::readDeviceData();
frmMain w;
w.setWindowTitle("网络调试助手 V2023 (QQ: 517216493 WX: feiyangqingyun)");
w.setWindowTitle("网络调试助手 V2024 (QQ: 517216493 WX: feiyangqingyun)");
w.resize(950, 700);
QtHelper::setFormInCenter(&w);
w.show();

View File

@@ -24,7 +24,7 @@ AppInit::AppInit(QObject *parent) : QObject(parent)
}
bool AppInit::eventFilter(QObject *watched, QEvent *event)
{
{
QWidget *w = (QWidget *)watched;
if (!w->property("canMove").toBool()) {
return QObject::eventFilter(watched, event);
@@ -34,26 +34,17 @@ bool AppInit::eventFilter(QObject *watched, QEvent *event)
static bool mousePressed = false;
int type = event->type();
QPoint p;
QMouseEvent *mouseEvent = (QMouseEvent *)(event);
if (mouseEvent) {
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
p = mouseEvent->globalPos();
#else
p = mouseEvent->globalPosition().toPoint();
#endif
}
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (type == QEvent::MouseButtonPress) {
if (mouseEvent->button() == Qt::LeftButton) {
mousePressed = true;
mousePoint = p - w->pos();
mousePoint = mouseEvent->globalPos() - w->pos();
}
} else if (type == QEvent::MouseButtonRelease) {
mousePressed = false;
} else if (type == QEvent::MouseMove) {
if (mousePressed) {
w->move(p - mousePoint);
w->move(mouseEvent->globalPos() - mousePoint);
return true;
}
}

View File

@@ -19,9 +19,6 @@ DESTDIR = $$PWD/../bin
#把所有警告都关掉眼不见为净
CONFIG += warn_off
#QMAKE_CXXFLAGS_WARN_ON -= -w34100
#QMAKE_CXXFLAGS *= -Wno-unused-parameter
#开启大资源支持
CONFIG += resources_big
#开启后会将打印信息用控制台输出
@@ -29,8 +26,7 @@ CONFIG += resources_big
#开启后不会生成空的 debug release 目录
#CONFIG -= debug_and_release
#引入全志H3芯片依赖(不需要的用户可以删除)
include ($$PWD/h3.pri)
include ($$PWD/core_util.pri)
#将当前目录加入到头文件路径
INCLUDEPATH += $$PWD

View File

@@ -0,0 +1,37 @@
#定义复制文件到目录的函数
#为什么上面不加win{}这种
#因为还有在win/linux上的安卓套件/他并不是win/linux套件
#所以两种命令都执行保证任意系统可用
defineTest(copyToDestDir) {
#取出对应的参数变量
srcFile = $$1
dstPath = $$2
#linux和mac系统拷贝
system($$QMAKE_COPY $$srcFile $$dstPath)
#win上需要转换路径
srcFile2 = $$srcFile
dstPath2 = $$dstPath
srcFile2 ~= s,/,\\,g
dstPath2 ~= s,/,\\,g
system($$QMAKE_COPY $$srcFile2 $$dstPath2)
}
#新建目录/在win上目录不存在的话需要主动新建/linux会自动
defineTest(newPath) {
path = $$1
win32 {
path ~= s,/,\\,g
}
system(mkdir $$path)
}
#引入全志H3芯片依赖(不需要的用户可以删除)
unix:!macx {
contains(QT_ARCH, arm) {
contains(DEFINES, arma7) {
INCLUDEPATH += /usr/local/openssl-1.0.2m-h3-gcc-4.9.2/include
LIBS += -L/usr/local/openssl-1.0.2m-h3-gcc-4.9.2/lib -lssl -lcrypto
LIBS += -L/usr/local/h3_rootfsv -lXdmcp
}}}

View File

@@ -4,26 +4,40 @@
#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
QList<QRect> QtHelper::getScreenRects(bool available)
bool QtHelper::useRatio = true;
//full指宽屏/就是将所有屏幕拼接在一起
QList<QRect> QtHelper::getScreenRects(bool available, bool full)
{
QRect rect;
QList<QRect> rects;
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
int screenCount = qApp->screens().count();
QList<QScreen *> screens = qApp->screens();
int screenCount = screens.count();
for (int i = 0; i < screenCount; ++i) {
QScreen *screen = screens.at(i);
if (full) {
rect = (available ? screen->availableVirtualGeometry() : screen->virtualGeometry());
} else {
rect = (available ? screen->availableGeometry() : screen->geometry());
}
//需要根据缩放比来重新调整宽高
qreal ratio = screen->devicePixelRatio();
QRect rect = (available ? screen->availableGeometry() : screen->geometry());
qreal ratio = (QtHelper::useRatio ? screen->devicePixelRatio() : 1);
rect.setWidth(rect.width() * ratio);
rect.setHeight(rect.height() * ratio);
rects << rect;
}
#else
int screenCount = qApp->desktop()->screenCount();
QDesktopWidget *desk = qApp->desktop();
int screenCount = desk->screenCount();
for (int i = 0; i < screenCount; ++i) {
QRect rect = (available ? desk->availableGeometry(i) : desk->screenGeometry(i));
if (full) {
rect = (available ? desk->geometry() : desk->geometry());
} else {
rect = (available ? desk->availableGeometry(i) : desk->screenGeometry(i));
}
rects << rect;
}
#endif
@@ -48,10 +62,10 @@ int QtHelper::getScreenIndex()
return screenIndex;
}
QRect QtHelper::getScreenRect(bool available)
QRect QtHelper::getScreenRect(bool available, bool full)
{
int screenIndex = getScreenIndex();
QList<QRect> rects = getScreenRects(available);
QList<QRect> rects = getScreenRects(available, full);
return rects.at(screenIndex);
}
@@ -455,7 +469,8 @@ void QtHelper::checkRun()
void QtHelper::setStyle()
{
//打印下所有内置风格的名字
qDebug() << TIMEMS << "QStyleFactory::keys" << QStyleFactory::keys();
//qDebug() << TIMEMS << "QStyleFactory::keys" << QStyleFactory::keys();
//设置内置风格
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
qApp->setStyle("Fusion");
@@ -618,6 +633,9 @@ void QtHelper::initAll(bool utf8, bool style, int fontSize)
#endif
}
#ifdef webengine
#include "qquickwindow.h"
#endif
void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritical)
{
#ifdef Q_OS_LINUX
@@ -632,15 +650,17 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
QApplication::setDesktopSettingsAware(desktopSettingsAware);
#endif
bool highDpi = !use96Dpi;
//安卓必须启用高分屏
#ifdef Q_OS_ANDROID
highDpi = true;
use96Dpi = false;
#endif
QtHelper::useRatio = use96Dpi;
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0) && QT_VERSION < QT_VERSION_CHECK(6,0,0))
//开启高分屏缩放支持
if (highDpi) {
if (!use96Dpi) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
}
#endif
@@ -657,7 +677,7 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
//高分屏缩放策略
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
@@ -671,6 +691,13 @@ void QtHelper::initMain(bool desktopSettingsAware, bool use96Dpi, bool logCritic
//设置opengl共享上下文
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#ifdef webengine
//修复openglwidget和webengine共存出现黑屏的bug
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
#endif
#endif
}
void QtHelper::initOpenGL(quint8 type, bool checkCardEnable, bool checkVirtualSystem)
@@ -1275,11 +1302,11 @@ void QtHelper::runWithSystem(const QString &fileName, const QString &filePath, b
#endif
}
void QtHelper::runBin(const QString &path, const QString &name)
void QtHelper::start(const QString &path, const QString &name, bool bin)
{
#ifdef Q_OS_WIN
QString cmd1 = "tasklist";
QString cmd2 = QString("%1/%2.exe").arg(path).arg(name);
QString cmd2 = QString("%1/%2%3").arg(path).arg(name).arg(bin ? ".exe" : "");
#else
QString cmd1 = "ps -aux";
QString cmd2 = QString("%1/%2").arg(path).arg(name);

View File

@@ -7,9 +7,10 @@ class QtHelper
{
public:
//获取所有屏幕区域/当前鼠标所在屏幕索引/区域尺寸/缩放系数
static QList<QRect> getScreenRects(bool available = true);
static bool useRatio;
static QList<QRect> getScreenRects(bool available = true, bool full = false);
static int getScreenIndex();
static QRect getScreenRect(bool available = true);
static QRect getScreenRect(bool available = true, bool full = false);
static qreal getScreenRatio(int index = -1, bool devicePixel = false);
//矫正当前鼠标所在屏幕居中尺寸
@@ -86,7 +87,7 @@ public:
//一次性设置所有包括编码样式字体等
static void initAll(bool utf8 = true, bool style = true, int fontSize = 13);
//初始化main函数最前面执行的一段代码
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = true, bool logCritical = true);
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = false, bool logCritical = true);
//初始化opengl类型(1=AA_UseDesktopOpenGL 2=AA_UseOpenGLES 3=AA_UseSoftwareOpenGL)
static void initOpenGL(quint8 type = 0, bool checkCardEnable = false, bool checkVirtualSystem = false);
@@ -181,8 +182,9 @@ public:
//设置开机自启动
static void runWithSystem(bool autoRun = true);
static void runWithSystem(const QString &fileName, const QString &filePath, bool autoRun = true);
//启动运行程序(已经在运行则不启动)
static void runBin(const QString &path, const QString &name);
static void start(const QString &path, const QString &name, bool bin = true);
};
#endif // QTHELPER_H

View File

@@ -6,13 +6,6 @@
int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
QApplication::setAttribute(Qt::AA_Use96Dpi);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
#endif
QApplication a(argc, argv);
QFont font;
font.setFamily("Microsoft Yahei");

View File

@@ -10,7 +10,7 @@ frmScreenWidget::frmScreenWidget(QWidget *parent) : QWidget(parent), ui(new Ui::
frmScreenWidget::~frmScreenWidget()
{
delete ui;
exit(0);
exit(0);
}
void frmScreenWidget::on_pushButton_clicked()

View File

@@ -6,14 +6,9 @@
int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
QApplication::setAttribute(Qt::AA_Use96Dpi);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
#endif
qputenv("QT_FONT_DPI", "96");
QApplication a(argc, argv);
QFont font;
font.setFamily("Microsoft Yahei");
font.setPixelSize(13);