您好,登錄后才能下訂單哦!
這篇文章主要介紹“Qt通用方法及類庫怎么使用”,在日常操作中,相信很多人在Qt通用方法及類庫怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Qt通用方法及類庫怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
//設置標簽顏色 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\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 } 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通用方法及類庫怎么使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。