Qt通用方法及类库5
Posted sggggr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于Qt通用方法及类库5的主要内容,如果未能解决你的问题,请参考以下文章