237 lines
6.8 KiB
C++
237 lines
6.8 KiB
C++
#ifndef COMMONHELPER_H
|
|
#define COMMONHELPER_H
|
|
|
|
#include <QtCore>
|
|
#include <QtGui>
|
|
#include <QApplication>
|
|
#include <QScreen>
|
|
|
|
#include "frmmessagebox.h"
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
#include <QRegularExpression>
|
|
#else
|
|
#include <QRegExp>
|
|
#endif
|
|
|
|
class CommonHelper: public QObject
|
|
{
|
|
signals:
|
|
void sigCurrentStyleIndex(int index); // 主题切换信号
|
|
|
|
public:
|
|
|
|
//设置为开机启动
|
|
static void AutoRunWithSystem(bool IsAutoRun, const QString& strAppName, const QString& strAppPath)
|
|
{
|
|
QSettings* reg = new QSettings(
|
|
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
|
|
QSettings::NativeFormat);
|
|
if (IsAutoRun)
|
|
{
|
|
reg->setValue(strAppName, strAppPath);
|
|
}
|
|
else
|
|
{
|
|
reg->setValue(strAppName, "");
|
|
}
|
|
}
|
|
|
|
//设置编码为UTF8
|
|
static void SetUTF8Code()
|
|
{
|
|
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
|
|
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
|
|
QTextCodec::setCodecForLocale(codec);
|
|
QTextCodec::setCodecForCStrings(codec);
|
|
QTextCodec::setCodecForTr(codec);
|
|
#endif
|
|
}
|
|
|
|
//设置皮肤样式
|
|
static void SetStyle(const QString& styleName)
|
|
{
|
|
// 使用资源路径加载
|
|
QFile file(QString(":/skin/stylesheet/%1.qss").arg(styleName));
|
|
file.open(QFile::ReadOnly);
|
|
QString qss = QLatin1String(file.readAll());
|
|
// 设置样式表
|
|
qApp->setStyleSheet(qss);
|
|
}
|
|
|
|
//加载中文字符
|
|
static void SetChinese()
|
|
{
|
|
QTranslator* translator = new QTranslator(qApp);
|
|
translator->load(":/skin/image/qt_zh_CN.qm");
|
|
qApp->installTranslator(translator);
|
|
}
|
|
|
|
//判断是否是IP地址
|
|
static bool IsIP(QString IP)
|
|
{
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
// Qt6 使用 QRegularExpression
|
|
QRegularExpression RegExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
|
|
return RegExp.match(IP).hasMatch();
|
|
#else
|
|
// Qt5 使用 QRegExp
|
|
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);
|
|
#endif
|
|
}
|
|
|
|
//显示信息框,仅确定按钮
|
|
static void ShowMessageBoxInfo(QString info, QWidget* parent)
|
|
{
|
|
frmMessageBox *msg = new frmMessageBox(nullptr);
|
|
msg->SetMessage(info, 0);
|
|
|
|
if(nullptr != parent)
|
|
{
|
|
QRect rcParent = parent->geometry();
|
|
QPoint ptParent = parent->mapToGlobal(QPoint(0, 0) );
|
|
ptParent.setX(ptParent.x() + rcParent.width()/2 - msg->width()/2);
|
|
ptParent.setY(ptParent.y() + rcParent.height()/2 - msg->height()/2);
|
|
msg->move(ptParent);
|
|
}
|
|
|
|
msg->exec();
|
|
}
|
|
|
|
//显示错误框,仅确定按钮
|
|
static void ShowMessageBoxError(QString info, QWidget* parent)
|
|
{
|
|
frmMessageBox *msg = new frmMessageBox(nullptr);
|
|
msg->SetMessage(info, 2);
|
|
|
|
if(nullptr != parent)
|
|
{
|
|
QRect rcParent = parent->geometry();
|
|
QPoint ptParent = parent->mapToGlobal(QPoint(0, 0) );
|
|
ptParent.setX(ptParent.x() + rcParent.width()/2 - msg->width()/2);
|
|
ptParent.setY(ptParent.y() + rcParent.height()/2 - msg->height()/2);
|
|
msg->move(ptParent);
|
|
}
|
|
|
|
msg->exec();
|
|
}
|
|
|
|
//显示询问框,确定和取消按钮
|
|
static int ShowMessageBoxQuesion(QString info, QWidget* parent, QString strOK = "确定", QString strCancel = "取消")
|
|
{
|
|
frmMessageBox* msg = new frmMessageBox(nullptr, strOK, strCancel);
|
|
msg->SetMessage(info, 1);
|
|
|
|
if(nullptr != parent)
|
|
{
|
|
QRect rcParent = parent->geometry();
|
|
QPoint ptParent = parent->mapToGlobal(QPoint(0, 0) );
|
|
ptParent.setX(ptParent.x() + rcParent.width()/2 - msg->width()/2);
|
|
ptParent.setY(ptParent.y() + rcParent.height()/2 - msg->height()/2);
|
|
msg->move(ptParent);
|
|
}
|
|
|
|
return msg->exec();
|
|
}
|
|
|
|
|
|
//延时
|
|
static void Sleep(int msec)
|
|
{
|
|
QTime dieTime = QTime::currentTime().addMSecs(msec);
|
|
while (QTime::currentTime() < dieTime)
|
|
{
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
}
|
|
}
|
|
|
|
//窗体居中显示
|
|
static void FormInCenter(QWidget* frm)
|
|
{
|
|
int frmX = frm->width();
|
|
int frmY = frm->height();
|
|
QScreen *screen = QApplication::primaryScreen();
|
|
QRect screenGeometry = screen->geometry();
|
|
int deskWidth = screenGeometry.width();
|
|
int deskHeight = screenGeometry.height();
|
|
QPoint movePoint(deskWidth / 2 - frmX / 2, deskHeight / 2 - frmY / 2);
|
|
frm->move(movePoint);
|
|
}
|
|
|
|
static QString ShowHex(QByteArray str)
|
|
{
|
|
//将str的数据 读到out里面去
|
|
QDataStream out(&str, QIODevice::ReadWrite);
|
|
QString buf;
|
|
while (!out.atEnd())
|
|
{
|
|
qint8 outChar = 0;
|
|
out >> outChar; //每次一个字节的填充到 outchar
|
|
QString str = QString("%1").arg(outChar & 0xFF, 2, 16, QLatin1Char('0')).toUpper() + QString(" "); //2 字符宽度
|
|
buf += str;
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
static QByteArray QString2Hex(QString str)
|
|
{
|
|
QByteArray senddata;
|
|
int hexdata, lowhexdata;
|
|
int hexdatalen = 0;
|
|
int len = str.length();
|
|
senddata.resize(len / 2);
|
|
char lstr, hstr;
|
|
for (int i = 0; i < len;)
|
|
{
|
|
//字符型
|
|
hstr = str[i].toLatin1();
|
|
if (hstr == ' ')
|
|
{
|
|
i++;
|
|
continue;
|
|
}
|
|
i++;
|
|
if (i >= len)
|
|
break;
|
|
lstr = str[i].toLatin1();
|
|
hexdata = convertHexChar(hstr);
|
|
lowhexdata = convertHexChar(lstr);
|
|
if ((hexdata == 16) || (lowhexdata == 16))
|
|
break;
|
|
else
|
|
hexdata = hexdata * 16 + lowhexdata;
|
|
i++;
|
|
senddata[hexdatalen] = (char)hexdata;
|
|
hexdatalen++;
|
|
}
|
|
senddata.resize(hexdatalen);
|
|
return senddata;
|
|
}
|
|
|
|
static char convertHexChar(char ch)
|
|
{
|
|
if ((ch >= '0') && (ch <= '9'))
|
|
return ch - 0x30;
|
|
else if ((ch >= 'A') && (ch <= 'F'))
|
|
return ch - 'A' + 10;
|
|
else if ((ch >= 'a') && (ch <= 'f'))
|
|
return ch - 'a' + 10;
|
|
else
|
|
return (-1);
|
|
}
|
|
|
|
//buf:低位在前,高位在后
|
|
static void IntToHex(int data, unsigned char* buf, int len)
|
|
{
|
|
if (len < 4)
|
|
return;
|
|
buf[0] = data & 0xFF;
|
|
buf[1] = (data >> 8) & 0xFF;
|
|
buf[2] = (data >> 16) & 0xFF;
|
|
buf[3] = (data >> 24) & 0xFF;
|
|
}
|
|
};
|
|
|
|
#endif // COMMONHELPER_H
|