本文主要是介绍《QT实用小工具·二十七》各种炫酷的样式表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、概述
源码放在文章末尾
该项目实现了各种炫酷的样式表,如单选、多选、按钮、日历、表格、下拉框、滚轮等,下面是项目demo演示:
项目部分代码如下:
#include "frmmain.h"
#include "ui_frmmain.h"
#include "head.h"frmMain::frmMain(QWidget *parent) : QMainWindow(parent), ui(new Ui::frmMain)
{ui->setupUi(this);this->initForm();
}frmMain::~frmMain()
{delete ui;
}void frmMain::initForm()
{ui->rbtn1->setChecked(true);ui->ck2->setChecked(true);ui->ck3->setCheckState(Qt::PartiallyChecked);ui->textEdit->setText("我爱你");ui->treeWidget->expandAll();int rowHeight = 23;ui->tableWidget->horizontalHeader()->setFixedHeight(rowHeight);ui->tableWidget->verticalHeader()->setDefaultSectionSize(rowHeight);//加载鼠标右键菜单翻译文件QTranslator *translator1 = new QTranslator(qApp);if (translator1->load(":/qm/qt_zh_CN.qm")) {qApp->installTranslator(translator1);}//加载富文本框鼠标右键菜单翻译文件QTranslator *translator2 = new QTranslator(qApp);if (translator2->load(":/qm/widgets.qm")) {qApp->installTranslator(translator2);}on_btnStyle1_clicked();
}void frmMain::loadStyle(const QString &qssFile)
{//开启计时QElapsedTimer time;time.start();//加载样式表QString qss;QFile file(qssFile);if (file.open(QFile::ReadOnly)) {//用QTextStream读取样式文件不用区分文件编码 带bom也行QStringList list;QTextStream in(&file);//in.setCodec("utf-8");while (!in.atEnd()) {QString line;in >> line;list << line;}file.close();qss = list.join("\n");QString paletteColor = qss.mid(20, 7);qApp->setPalette(QPalette(paletteColor));//用时主要在下面这句qApp->setStyleSheet(qss);}qDebug() << "用时:" << time.elapsed();
}void frmMain::on_btnStyle1_clicked()
{loadStyle(":/qss/lightblue.css");
}void frmMain::on_btnStyle2_clicked()
{loadStyle(":/qss/flatgray.css");
}void frmMain::on_btnStyle3_clicked()
{loadStyle(":/qss/blacksoft.css");
}void frmMain::on_btnInfo_clicked()
{//QFileDialog::getOpenFileName(this, "", "", "", 0, QFileDialog::DontUseNativeDialog);//QFileDialog::getExistingDirectory(0, "", "", QFileDialog::DontUseNativeDialog);
}
源码下载
这篇关于《QT实用小工具·二十七》各种炫酷的样式表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!