代码拉取完成,页面将自动刷新
#ifndef MHELPER_H
#define MHELPER_H
#include <QDesktopWidget>
#include <QFile>
#include <QTextCodec>
#include <QMessageBox>
#include <QAbstractButton>
#include <QCoreApplication>
#include <QFileDialog>
#include <QTime>
#include <QProcess>
#include <QDir>
#include <QSound>
#include <QApplication>
#include <QStyleFactory>
#include <QInputDialog>
#include <QStyle>
#include <QDebug>
/*描述:辅助类,包含对话框,中文显示,文件处理等
*作者:刘典武
*时间:2013-12-12
*/
class mHelper:public QObject
{
public:
//设置全局为plastique样式
static void SetStyle()
{
QApplication::setStyle(QStyleFactory::create("Plastique"));
//QApplication::setPalette(QApplication::style()->standardPalette());//替换应用程序的颜色组合
}
//设置编码为GB2312
static void SetGB2312Code()
{
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
QTextCodec *codec=QTextCodec::codecForName("GB2312");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec=QTextCodec::codecForName("GB2312");
QTextCodec::setCodecForLocale(codec);
#endif
}
//设置编码为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);
#else
QTextCodec *codec=QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
#endif
}
//显示信息框,仅确定按钮
static void ShowMessageBoxInfo(QString info)
{
QMessageBox msg;
msg.setStyleSheet("font:12pt '宋体'");
msg.setWindowTitle("提示");
msg.setText(info);
msg.setIcon(QMessageBox::Information);
msg.addButton("确定",QMessageBox::ActionRole);
msg.exec();
}
static void ShowMessageBoxInfoX(QString info)
{
QMessageBox::information(0,"提示",info,QMessageBox::Ok);
}
//显示错误框,仅确定按钮
static void ShowMessageBoxError(QString info)
{
QMessageBox msg;
msg.setStyleSheet("font:12pt '宋体'");
msg.setWindowTitle("错误");
msg.setText(info);
msg.setIcon(QMessageBox::Critical);
msg.addButton("确定",QMessageBox::ActionRole);
msg.exec();
}
static void ShowMessageBoxErrorX(QString info)
{
QMessageBox::critical(0,"错误",info,QMessageBox::Ok);
}
//显示询问框,确定和取消按钮
static int ShowMessageBoxQuesion(QString info)
{
QMessageBox msg;
msg.setStyleSheet("font:12pt '宋体'");
msg.setWindowTitle("询问");
msg.setText(info);
msg.setIcon(QMessageBox::Question);
msg.addButton("确定",QMessageBox::ActionRole);
msg.addButton("取消",QMessageBox::RejectRole);
return msg.exec();
}
static int ShowMessageBoxQuesionX(QString info)
{
return QMessageBox::question(0,"询问",info,QMessageBox::Yes|QMessageBox::No);
}
//显示标准输入框
static QString ShowInputBox(QWidget *frm,QString info)
{
bool ok;
return QInputDialog::getText(frm,"提示",info,QLineEdit::Password,"",&ok);
}
//16进制字符串转字节数组
static QByteArray HexStrToByteArray(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; )
{
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
hstr=str[i].toAscii();
#else
hstr =str[i].toLatin1();
#endif
if(hstr == ' ')
{
i++;
continue;
}
i++;
if(i >= len)
break;
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
lstr = str[i].toAscii();
#else
lstr = str[i].toLatin1();
#endif
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);
}
//字节数组转16进制字符串
static QString ByteArrayToHexStr(QByteArray data)
{
QString temp="";
QString hex=data.toHex();
for (int i=0;i<hex.length();i=i+2)
{
temp+=hex.mid(i,2)+" ";
}
return temp.trimmed().toUpper();
}
//16进制字符串转10进制
static int StrHexToDecimal(QString strHex)
{
bool ok;
return strHex.toInt(&ok,16);
}
//10进制字符串转10进制
static int StrDecimalToDecimal(QString strDecimal)
{
bool ok;
return strDecimal.toInt(&ok,10);
}
//2进制字符串转10进制
static int StrBinToDecimal(QString strBin)
{
bool ok;
return strBin.toInt(&ok,2);
}
//16进制字符串转2进制字符串
static QString StrHexToStrBin(QString strHex)
{
uchar decimal=StrHexToDecimal(strHex);
QString bin=QString::number(decimal,2);
uchar len=bin.length();
if (len<8)
{
for (int i=0;i<8-len;i++)
{
bin="0"+bin;
}
}
return bin;
}
//10进制转2进制字符串一个字节
static QString DecimalToStrBin1(int decimal)
{
QString bin=QString::number(decimal,2);
uchar len=bin.length();
if (len<=8)
{
for (int i=0;i<8-len;i++)
{
bin="0"+bin;
}
}
return bin;
}
//10进制转2进制字符串两个字节
static QString DecimalToStrBin2(int decimal)
{
QString bin=QString::number(decimal,2);
uchar len=bin.length();
if (len<=16)
{
for (int i=0;i<16-len;i++)
{
bin="0"+bin;
}
}
return bin;
}
//计算校验码
static uchar GetCheckCode(uchar data[],uchar len)
{
uchar temp=0;
for (uchar i=0;i<len;i++)
{
temp+=data[i];
}
return temp%256;
}
//将溢出的char转为正确的uchar
static uchar GetUChar(char data)
{
uchar temp;
if (data>127)
{
temp=data+256;
}
else
{
temp=data;
}
return temp;
}
//延时
static void Sleep(int sec)
{
QTime dieTime = QTime::currentTime().addMSecs(sec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
//获取当前路径
static QString GetCurrentPath()
{
return QString(QCoreApplication::applicationDirPath()+"/");
}
//播放声音
static void PlaySound(QString soundName)
{
QSound::play(soundName);
}
//设置系统日期时间
static void SetSystemDateTime(int year,int month,int day,int hour,int min,int sec)
{
QProcess p(0);
p.start("cmd");
p.waitForStarted();
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toAscii());
#else
p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toLatin1());
#endif
p.closeWriteChannel();
p.waitForFinished(1000);
p.close();
p.start("cmd");
p.waitForStarted();
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toAscii());
#else
p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1());
#endif
p.closeWriteChannel();
p.waitForFinished(1000);
p.close();
}
//窗体居中,并且只有关闭按钮,不能调整大小
static void FormOnlyCloseInCenter(QWidget *frm)
{
//设置窗体居中
QDesktopWidget desktop;
int screenX=desktop.availableGeometry().width();
int screenY=desktop.availableGeometry().height()-40;
int frmX=frm->width();
int frmY=frm->height();
QPoint movePoint(screenX/2-frmX/2,screenY/2-frmY/2);
frm->move(movePoint);
//设置窗体不能调整大小
frm->setFixedSize(frmX,frmY);
//设置窗体只有最小化按钮
frm->setWindowFlags(Qt::WindowCloseButtonHint);
}
//窗体居中显示
static void FormInCenter(QWidget *frm)
{
int screenX=qApp->desktop()->width();
int screenY=qApp->desktop()->height()-60;
int wndX=frm->width();
int wndY=frm->height();
QPoint movePoint((screenX-wndX)/2,(screenY-wndY)/2);
frm->move(movePoint);
}
//窗体没有最大化按钮
static void FormNoMaxButton(QWidget *frm)
{
frm->setWindowFlags(Qt::WindowMinimizeButtonHint);
}
//窗体只有关闭按钮
static void FormOnlyCloseButton(QWidget *frm)
{
frm->setWindowFlags(Qt::WindowCloseButtonHint);
}
//窗体不能改变大小
static void FormNotResize(QWidget *frm)
{
frm->setFixedSize(frm->width(),frm->height());
}
//获取桌面大小
static QSize GetDesktopSize()
{
QDesktopWidget desktop;
return QSize(desktop.availableGeometry().width(),desktop.availableGeometry().height());
}
//获取选择的文件
static QString GetFileName(QString filter)
{
return QFileDialog::getOpenFileName(NULL,"选择文件",QCoreApplication::applicationDirPath(),filter);
}
//获取选择的文件集合
static QStringList GetFileNames(QString filter)
{
return QFileDialog::getOpenFileNames(NULL,"选择文件",QCoreApplication::applicationDirPath(),filter);
}
//获取选择的目录
static QString GetFolderName()
{
return QFileDialog::getExistingDirectory();;
}
//获取文件名,含拓展名
static QString GetFileNameWithExtension(QString strFilePath)
{
QFileInfo fileInfo(strFilePath);
return fileInfo.fileName();
}
//获取选择文件夹中的文件
static QStringList GetFolderFileNames(QStringList filter)
{
QStringList fileList;
QString strFolder = QFileDialog::getExistingDirectory();
if (!strFolder.length()==0)
{
QDir myFolder(strFolder);
if (myFolder.exists())
{
fileList= myFolder.entryList(filter);
}
}
return fileList;
}
//文件夹是否存在
static bool FolderIsExist(QString strFolder)
{
QDir tempFolder(strFolder);
return tempFolder.exists();
}
//文件是否存在
static bool FileIsExist(QString strFile)
{
QFile tempFile(strFile);
return tempFile.exists();
}
//复制文件
static bool CopyFile(QString sourceFile, QString targetFile)
{
if (FileIsExist(targetFile))
{
int ret=QMessageBox::information(NULL,"提示","文件已经存在,是否替换?",QMessageBox::Yes | QMessageBox::No);
if (ret!=QMessageBox::Yes)
{
return false;
}
}
return QFile::copy(sourceFile,targetFile);
}
};
#endif // MYHELPER_H
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。