这篇文章主要介绍“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。