QToolClass

平时常用会用到的工具类

#ifndef MYHELPER_H
#define MYHELPER_H

#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#endif
#include "../usercontrol/frmmessagebox.h" //自定义的消息框
#include "../usercontrol/frminputbox.h"   //自定义的输入框
#include "app.h"

#define TIMEMS qPrintable (QTime::currentTime().toString("HH:mm:ss zzz"))
#define TIME qPrintable (QTime::currentTime().toString("HH:mm:ss"))
#define DATE qPrintable (QDate::currentDate().toString("yyyy-MM-dd"))
#define DATETIME qPrintable (QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))

class myHelper : public QObject
{

public:

    //! [] 设置为开机启动
    static void AutoRunWithSystem(bool IsAutoRun, QString AppName, QString AppPath) {
        QSettings* reg = new QSettings(
            "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
            QSettings::NativeFormat);

        if (IsAutoRun) {
            reg->setValue(AppName, AppPath);
        }
        else {
            reg->setValue(AppName, "");
        }
    }

    //! [] 设置编码为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& qssFile) {
        QFile file(qssFile);
        if (file.open(QFile::ReadOnly)) {
            QString qss = QLatin1String(file.readAll());
            qApp->setStyleSheet(qss);
            QString PaletteColor = qss.mid(20, 7);
            qApp->setPalette(QPalette(QColor(PaletteColor)));
            file.close();
        }
    }

    //! [] 加载中文字符
    static void SetChinese() {
        QTranslator* translator = new QTranslator(qApp);
        translator->load(":/image/qt_zh_CN.qm");
        qApp->installTranslator(translator);
    }

    //! [] 加载字体
    static void SetFont() {
        qApp->setFont(QFont(App::FontName, App::FontSize)); //App类,表示项目的全局属性
    }

    //! [] 判断是否是IP地址
    static bool IsIP(QString IP) {
        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);
    }

    //! [] 显示输入框
    static QString ShowInputBox(bool& ok, QString title, int type = 0, QString defaultValue = QString(), bool pwd = false) {
        frmInputBox input; //自定义的输入框
        input.SetParameter(title, type, defaultValue, pwd);
        ok = input.exec();
        return input.GetValue();
    }

    //! [] 显示信息框,仅确定按钮
    static void ShowMessageBoxInfo(QString info) {
        frmMessageBox msg; //自定义的信息框
        msg.SetMessage(info, 0);
        msg.exec();
    }

    //! [] 显示错误框,仅确定按钮
    static void ShowMessageBoxError(QString info) {
        frmMessageBox msg; //自定义的错误框
        msg.SetMessage(info, 2);
        msg.exec();
    }

    //! [] 显示询问框,确定和取消按钮
    static int ShowMessageBoxQuestion(QString info) {
        frmMessageBox msg; //自定义询问框
        msg.SetMessage(info, 1);
        return msg.exec();
    }

    //! [] 延时
    static void Sleep(int sec) {
        QTime dieTime = QTime::currentTime().addMSecs(sec);
        while (QTime::currentTime()] 窗体居中显示
    static void FormInCenter(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);
    }

    //! [] 设置系统日期时间
    static void SetSystemDateTime(QString year, QString month, QString day, QString hour, QString min, QString sec) {
#ifdef Q_OS_WIN
        QProcess p(0);
        p.start("cmd");
        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.waitForStarted();
        p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1());
        p.closeWriteChannel();
        p.waitForFinished(1000);
        p.close();
#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");
#endif
    }

    //! [] 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; ) {
            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 QByteArray AsciiStrToByteArray(QString str)
    {
        QByteArray buffer;
        int len = str.length();
        QString letter;
        QString hex;

        for (int i = 0; i < len; i++) {
            letter = str.at(i);

            if (letter == "\\") {
                i++;
                letter = str.mid(i, 1);

                if (letter == "x") {
                    i++;
                    hex = str.mid(i, 2);
                    buffer.append(StrHexToDecimal(hex));
                    i++;
                    continue;
                }
                else if (letter == "N") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "U") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "L") {           //! [] NUL=0x00
                            buffer.append((char)0x00);
                            continue;
                        }
                    }
                    else if (hex == "A") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "K") {           //! [] NAK=0x15
                            buffer.append(0x15);
                            continue;
                        }
                    }
                }
                else if (letter == "S") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "O") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "H") {           //! [] SOH=0x01
                            buffer.append(0x01);
                            continue;
                        }
                        else {                    //! [] SO=0x0E
                            buffer.append(0x0E);
                            i--;
                            continue;
                        }
                    }
                    else if (hex == "T") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "X") {           //! [] STX=0x02
                            buffer.append(0x02);
                            continue;
                        }
                    }
                    else if (hex == "I") {        //! [] SI=0x0F
                        buffer.append(0x0F);
                        continue;
                    }
                    else if (hex == "Y") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "N") {           //! [] SYN=0x16
                            buffer.append(0x16);
                            continue;
                        }
                    }
                    else if (hex == "U") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "B") {           //! [] SUB=0x1A
                            buffer.append(0x1A);
                            continue;
                        }
                    }
                }
                else if (letter == "E") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "T") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "X") {           //! [] ETX=0x03
                            buffer.append(0x03);
                            continue;
                        }
                        else if (hex == "B") {    //! [] ETB=0x17
                            buffer.append(0x17);
                            continue;
                        }
                    }
                    else if (hex == "O") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "T") {           //! [] EOT=0x04
                            buffer.append(0x04);
                            continue;
                        }
                    }
                    else if (hex == "N") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "Q") {           //! [] ENQ=0x05
                            buffer.append(0x05);
                            continue;
                        }
                    }
                    else if (hex == "M") {        //! [] EM=0x19
                        buffer.append(0x19);
                        continue;
                    }
                    else if (hex == "S") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "C") {           //! [] ESC=0x1B
                            buffer.append(0x1B);
                            continue;
                        }
                    }
                }
                else if (letter == "A") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "C") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "K") {           //! [] ACK=0x06
                            buffer.append(0x06);
                            continue;
                        }
                    }
                }
                else if (letter == "B") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "E") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "L") {           //! [] BEL=0x07
                            buffer.append(0x07);
                            continue;
                        }
                    }
                    else if (hex == "S") {        //! [] BS=0x08
                        buffer.append(0x08);
                        continue;
                    }
                }
                else if (letter == "C") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "R") {               //! [] CR=0x0D
                        buffer.append(0x0D);
                        continue;
                    }
                    else if (hex == "A") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "N") {           //! [] CAN=0x18
                            buffer.append(0x18);
                            continue;
                        }
                    }
                }
                else if (letter == "D") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "L") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "E") {           //! [] DLE=0x10
                            buffer.append(0x10);
                            continue;
                        }
                    }
                    else if (hex == "C") {
                        i++;
                        hex = str.mid(i, 1);

                        if (hex == "1") {           //! [] DC1=0x11
                            buffer.append(0x11);
                            continue;
                        }
                        else if (hex == "2") {    //! [] DC2=0x12
                            buffer.append(0x12);
                            continue;
                        }
                        else if (hex == "3") {    //! [] DC3=0x13
                            buffer.append(0x13);
                            continue;
                        }
                        else if (hex == "4") {    //! [] DC2=0x14
                            buffer.append(0x14);
                            continue;
                        }
                    }
                }
                else if (letter == "F") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "F") {               //! [] FF=0x0C
                        buffer.append(0x0C);
                        continue;
                    }
                    else if (hex == "S") {        //! [] FS=0x1C
                        buffer.append(0x1C);
                        continue;
                    }
                }
                else if (letter == "H") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "T") {               //! [] HT=0x09
                        buffer.append(0x09);
                        continue;
                    }
                }
                else if (letter == "L") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "F") {               //! [] LF=0x0A
                        buffer.append(0x0A);
                        continue;
                    }
                }
                else if (letter == "G") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "S") {               //! [] GS=0x1D
                        buffer.append(0x1D);
                        continue;
                    }
                }
                else if (letter == "R") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "S") {               //! [] RS=0x1E
                        buffer.append(0x1E);
                        continue;
                    }
                }
                else if (letter == "U") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "S") {               //! [] US=0x1F
                        buffer.append(0x1F);
                        continue;
                    }
                }
                else if (letter == "V") {
                    i++;
                    hex = str.mid(i, 1);

                    if (hex == "T") {               //! [] VT=0x0B
                        buffer.append(0x0B);
                        continue;
                    }
                }
                else if (letter == "\\") {
                    //! [] 如果连着的是多个\\则对应添加\对应的16进制0x5C
                    buffer.append(0x5C);
                    continue;
                }
                else {
                    //! [] 将对应的\[前面的\\也要加入
                    buffer.append(0x5C);
                    buffer.append(letter.toLatin1());
                    continue;
                }
            }

            buffer.append(str.mid(i, 1).toLatin1());

        }

        return buffer;
    }

    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] 0x20为空格,空格以下都是不可见字符
            char b = data.at(i);

            if (0x00 == b) {
                temp += QString("\\NUL");
            }
            else if (0x01 == b) {
                temp += QString("\\SOH");
            }
            else if (0x02 == b) {
                temp += QString("\\STX");
            }
            else if (0x03 == b) {
                temp += QString("\\ETX");
            }
            else if (0x04 == b) {
                temp += QString("\\EOT");
            }
            else if (0x05 == b) {
                temp += QString("\\ENQ");
            }
            else if (0x06 == b) {
                temp += QString("\\ACK");
            }
            else if (0x07 == b) {
                temp += QString("\\BEL");
            }
            else if (0x08 == b) {
                temp += QString("\\BS");
            }
            else if (0x09 == b) {
                temp += QString("\\HT");
            }
            else if (0x0A == b) {
                temp += QString("\\LF");
            }
            else if (0x0B == b) {
                temp += QString("\\VT");
            }
            else if (0x0C == b) {
                temp += QString("\\FF");
            }
            else if (0x0D == b) {
                temp += QString("\\CR");
            }
            else if (0x0E == b) {
                temp += QString("\\SO");
            }
            else if (0x0F == b) {
                temp += QString("\\SI");
            }
            else if (0x10 == b) {
                temp += QString("\\DLE");
            }
            else if (0x11 == b) {
                temp += QString("\\DC1");
            }
            else if (0x12 == b) {
                temp += QString("\\DC2");
            }
            else if (0x13 == b) {
                temp += QString("\\DC3");
            }
            else if (0x14 == b) {
                temp += QString("\\DC4");
            }
            else if (0x15 == b) {
                temp += QString("\\NAK");
            }
            else if (0x16 == b) {
                temp += QString("\\SYN");
            }
            else if (0x17 == b) {
                temp += QString("\\ETB");
            }
            else if (0x18 == b) {
                temp += QString("\\CAN");
            }
            else if (0x19 == b) {
                temp += QString("\\EM");
            }
            else if (0x1A == b) {
                temp += QString("\\SUB");
            }
            else if (0x1B == b) {
                temp += QString("\\ESC");
            }
            else if (0x1C == b) {
                temp += QString("\\FS");
            }
            else if (0x1D == b) {
                temp += QString("\\GS");
            }
            else if (0x1E == b) {
                temp += QString("\\RS");
            }
            else if (0x1F == b) {
                temp += QString("\\US");
            }
            else if (0x7F == b) {
                temp += QString("\\x7F");
            }
            else if (0x5C == b) {
                temp += QString("\\x5C");
            }
            else if (0x20 >= b) {
                temp += QString("\\x%1").arg(DecimalToStrHex((quint8)b));
            }
            else {
                temp += QString("%1").arg(b);
            }
        }

        return temp.trimmed();
    }

    //! [] 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;
    }

    //! [] 10进制转16进制字符串,补零.
    static QString DecimalToStrHex(int decimal) {
        QString temp = QString::number(decimal, 16);
        if (temp.length() == 1) {
            temp = "0" + temp;
        }
        return temp;
    }

    static QByteArray IntToByte(int i) {
        QByteArray result;
        result.resize(4);
        result[3] = (uchar)(0x000000ff&i);
        result[2] = (uchar)((0x0000ff00&i) >> 8);
        result[1] = (uchar)((0x00ff0000&i) >> 16);
        result[0] = (uchar)((0xff000000&i) >> 24);
        return result;
    }

    static int ByteToInt(QByteArray data) {
        int i = data[3]&0x000000FF;
        i |= ((data[2] << 8)&0x0000FF00);
        i |= ((data[1] << 16)&0x00FF0000);
        i |= ((data[0] << 24)&0xFF000000);
        return i;
    }

    static QByteArray UShortToByte(ushort i) {
        QByteArray result;
        result.resize(2);
        result[1] = (uchar)(0x000000ff&i);
        result[0] = (uchar)((0x0000ff00&i) >> 8);
        return result;
    }

    static int ByteToUShort(QByteArray data) {
        int i = data[1]&0x000000FF;
        i |= ((data[0] << 8)&0x0000FF00);
        return i;
    }

    //! [] 异或加密算法
    static QString getXorEncryptDecrypt(QString str, char key) {
        QByteArray data = str.toLatin1();
        int size = data.size();
        for (int i = 0; i < size; i++) {
            data[i] = data[i] ^ key;
        }
        return QLatin1String(data);
    }

    //! [] 异或校验
    static uchar GetOrCode(QByteArray data) {
        int len = data.length();
        uchar result = 0;
        for (int i = 0; i] 计算校验码
    static uchar GetCheckCode(QByteArray data) {
        int len = data.length();
        uchar temp = 0;
        for (uchar i = 0; i < len; i++) {
            temp += data[i];
        }
        return temp % 256;
    }

    //! [] CRC16校验
    static QByteArray GetCRCCode(QByteArray data) {
        char* updata = data.data();
        uint len = data.length();
        uchar auchCRCHi[] = {
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
            0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
            0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
            0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
            0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
            0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
            0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
            0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
            0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
            0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
            0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
            0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
            0x40
        };
        uchar auchCRCLo[] = {
            0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
            0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
            0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
            0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
            0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
            0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
            0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
            0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
            0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
            0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
            0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
            0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
            0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
            0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
            0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
            0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
            0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
            0x40
        };

        uchar uchCRCHi = 0xFF;
        uchar uchCRCLo = 0xFF;
        uint  uindex;
        while (len--) {
            uindex = uchCRCHi ^ *updata++;
            uchCRCHi = uchCRCLo ^ auchCRCHi[uindex];
            uchCRCLo = auchCRCLo[uindex];
        }
        uint result = (uchCRCHi << 8 | uchCRCLo);
        return UShortToByte(result);
    }

    //! [] 获取选择的文件
    static QString GetFileName(QString filter) {
        return QFileDialog::getOpenFileName(0, "选择文件", QCoreApplication::applicationDirPath(), filter);
    }

    //! [] 获取选择的文件集合
    static QStringList GetFileNames(QString filter) {
        return QFileDialog::getOpenFileNames(0, "选择文件", 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) {
        bool ok;
        ok = QFile::copy(sourceFile, targetFile);
        //! [] 将复制过去的文件只读属性取消
        ok = QFile::setPermissions(targetFile, QFile::WriteOwner);
        return ok;
    }
    //! [] 判断一个字符串是否为纯数字
    static int IsDigitStr(QString src) {
        QByteArray ba = src.toLatin1();//QString 转换为 char*
        const char* s = ba.data();

        while (*s && *s >= '0' && *s <= '9') s++;

        if (*s) { //不是纯数字
            return -1;
        }
        else { //纯数字
            return 0;
        }
    }
};

#endif //! []  MYHELPER_H

QToolClass

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动到顶部