• Qt通用方法及类库5


    函数名

        //设置标签颜色
        static void setLabStyle(QLabel *lab, quint8 type, const QString &bgColor = "", const QString &textColor = "");
    
        //设置窗体居中显示
        static void setFormInCenter(QWidget *frm);
        //设置翻译文件
        static void setTranslator(const QString &qmFile = ":/image/qt_zh_CN.qm");
        //设置编码
        static void setCode();
        //设置延时
        static void sleep(int msec);
        //设置系统时间
        static void setSystemDateTime(const QString &year, const QString &month, const QString &day,
                                      const QString &hour, const QString &min, const QString &sec);
        //设置开机自启动
        static void runWithSystem(const QString &strName, const QString &strPath, bool autoRun = true);
    

    函数体

    void QUIHelper::setLabStyle(QLabel *lab, quint8 type, const QString &bgColor, const QString &textColor)
    {
        QString colorBg = bgColor;
        QString colorText = textColor;
    
        //如果设置了新颜色则启用新颜色
        if (bgColor.isEmpty() || textColor.isEmpty()) {
            if (type == 0) {
                colorBg = "#D64D54";
                colorText = "#FFFFFF";
            } else if (type == 1) {
                colorBg = "#17A086";
                colorText = "#FFFFFF";
            } else if (type == 2) {
                colorBg = "#47A4E9";
                colorText = "#FFFFFF";
            } else if (type == 3) {
                colorBg = "#282D30";
                colorText = "#FFFFFF";
            } else if (type == 4) {
                colorBg = "#0E99A0";
                colorText = "#FFFFFF";
            } else if (type == 5) {
                colorBg = "#A279C5";
                colorText = "#FFFFFF";
            } else if (type == 6) {
                colorBg = "#8C2957";
                colorText = "#FFFFFF";
            } else if (type == 7) {
                colorBg = "#04567E";
                colorText = "#FFFFFF";
            } else if (type == 8) {
                colorBg = "#FD8B28";
                colorText = "#FFFFFF";
            } else if (type == 9) {
                colorBg = "#5580A2";
                colorText = "#FFFFFF";
            }
        }
    
        QStringList qss;
        //禁用颜色
        qss << QString("QLabel::disabled{background:none;color:%1;}").arg(QUIConfig::BorderColor);
        //正常颜色
        qss << QString("QLabel{border:none;background-color:%1;color:%2;}").arg(colorBg).arg(colorText);
        lab->setStyleSheet(qss.join(""));
    }
    
    void QUIHelper::setFormInCenter(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);
    }
    
    void QUIHelper::setTranslator(const QString &qmFile)
    {
        QTranslator *translator = new QTranslator(qApp);
        translator->load(qmFile);
        qApp->installTranslator(translator);
    }
    
    void QUIHelper::setCode()
    {
    #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
    #if _MSC_VER
        QTextCodec *codec = QTextCodec::codecForName("gbk");
    #else
        QTextCodec *codec = QTextCodec::codecForName("utf-8");
    #endif
        QTextCodec::setCodecForLocale(codec);
        QTextCodec::setCodecForCStrings(codec);
        QTextCodec::setCodecForTr(codec);
    #else
        QTextCodec *codec = QTextCodec::codecForName("utf-8");
        QTextCodec::setCodecForLocale(codec);
    #endif
    }
    
    void QUIHelper::sleep(int msec)
    {
        if (msec > 0) {
    #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
            QTime endTime = QTime::currentTime().addMSecs(msec);
            while (QTime::currentTime() < endTime) {
                QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
            }
    #else
            QThread::msleep(msec);
    #endif
        }
    }
    
    void QUIHelper::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);
        p.start("cmd");
        p.waitForStarted();
        p.write(QString("date %1-%2-%3
    ").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
    ").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
    }
    
    void QUIHelper::runWithSystem(const QString &strName, const QString &strPath, bool autoRun)
    {
    #ifdef Q_OS_WIN
        QSettings reg("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", QSettings::NativeFormat);
        reg.setValue(strName, autoRun ? strPath : "");
    #endif
    }
    
  • 相关阅读:
    Vue 中样式穿透 /deep/
    Vue 数据冻结 Object.freeze
    Vue 启动项目内存溢出
    Typora[ markdown ] 使用3之----- 语法高亮显示
    Typora[ markdown ] 使用2之-----空格显示
    手动创建mysql数据库的语句记录
    api不能自动注入条件的解决方法
    【WTM框架】查询列表显示正常,但是导出的时候查询条件不起作用的问题记录及解决方法
    WTM问题之“数据列表”控件出现横向的滚动条的解决方法
    树莓派docker无法限制内存Your kernel does not support memory limit capabilities or the cgroup is not mounted
  • 原文地址:https://www.cnblogs.com/feiyangqingyun/p/12732166.html
Copyright © 2020-2023  润新知