96 lines
2.4 KiB
C++
96 lines
2.4 KiB
C++
//#if _MSC_VER >= 1600
|
|
//#pragma execution_character_set("utf-8")
|
|
//#endif
|
|
|
|
#ifndef COMMONHELPER_H
|
|
#define COMMONHELPER_H
|
|
|
|
#include <QtCore>
|
|
#include <QtGui>
|
|
#include <QScreen>
|
|
#include <QApplication>
|
|
#include <QWidget>
|
|
#include "custommessagebox.h"
|
|
|
|
class CommonHelper: public QObject
|
|
{
|
|
|
|
public:
|
|
//设置编码为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);
|
|
// 设置背景色
|
|
qApp->setPalette(QPalette(QColor("#FFFFFF")));
|
|
}
|
|
|
|
//加载中文字符
|
|
static void SetChinese()
|
|
{
|
|
QTranslator *translator = new QTranslator(qApp);
|
|
translator->load("./skin/image/qt_zh_CN.qm");
|
|
qApp->installTranslator(translator);
|
|
}
|
|
|
|
//显示信息框,仅确定按钮
|
|
static void ShowMessageBoxInfo(QString info)
|
|
{
|
|
CustomMessageBox *msg = new CustomMessageBox;
|
|
msg->SetMessage(info, 0);
|
|
msg->exec();
|
|
}
|
|
|
|
//显示错误框,仅确定按钮
|
|
static void ShowMessageBoxError(QString info)
|
|
{
|
|
CustomMessageBox *msg = new CustomMessageBox;
|
|
msg->SetMessage(info, 2);
|
|
msg->exec();
|
|
}
|
|
|
|
//显示询问框,确定和取消按钮
|
|
static int ShowMessageBoxQuesion(QString info)
|
|
{
|
|
CustomMessageBox *msg = new CustomMessageBox;
|
|
msg->SetMessage(info, 1);
|
|
return msg->exec();
|
|
delete msg;
|
|
}
|
|
|
|
//延时
|
|
static void Sleep(int sec)
|
|
{
|
|
QTime dieTime = QTime::currentTime().addMSecs(sec);
|
|
while ( QTime::currentTime() < dieTime ) {
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
}
|
|
}
|
|
|
|
//窗体居中显示
|
|
static void FormInCenter(QWidget *frm)
|
|
{
|
|
QRect screenGeometry = QGuiApplication::primaryScreen()->geometry();
|
|
frm->move((screenGeometry.width() - frm->width()) / 2,
|
|
(screenGeometry.height() - frm->height()) / 2);
|
|
}
|
|
};
|
|
|
|
#endif // COMMONHELPER_H
|