QT样式表单加载

QT样式表单

  • 前言
  • 方法
    • 1、UI界面设置
    • 2、程序添加
    • 3、通过QSS文件添加
  • 总结


前言

基于QT&VS混合开发中,样式表单的加载方法。


方法

1、UI界面设置

鼠标到按钮上右键,“改变样式表”,在编辑样式表对话框中添加QSS样式。

2、程序添加

每一个控件都有setStyleSheet(const QString &styleSheet)方法,样式字符串直接传参即可。

ui.pushButton1->setStyleSheet("QPushButton{background-color: white;  color: rgb(100, 100, 100) ;}");

3、通过QSS文件添加

新建文件StyleSheet.qss文件,添加内容。
读取qss文件,调用setStyleSheet接口加载。
实际项目中一般qss文件直接添加到资源里面,一起打包到EXE文件中。

QFile file;
file.setFileName("StyleSheet.qss");
if (file.open(QFile::ReadOnly)) {
	setStyleSheet(QLatin1String(file.readAll()));
	file.close();
}

示例文件

/*按钮静止无操作样式*/
QPushButton 
{
    background-color:rgb(255,255,255); 
    color:rgb(6,168,255); 
    border:2px solid rgb(6,168,255); 
    font-size:14px; 
    border-radius:10px;
}

/*鼠标悬停在按钮*/
QPushButton:hover
{
    background-color: rgb(212,243,255); 
    color:rgb(6,168,255);
    border:2px solid rgb(6,168,255); 
    border-radius:14px;
}

/*鼠标按下按钮*/
QPushButton:pressed
{
	background-color: rgb(175,232,255); 
    color:white; 
    border:2px solid rgb(6,168,255); 
    border-radius:14px;
}

总结

三种方法各有优缺点,要根据实际需求使用。

你可能感兴趣的:(qt,c++,开发语言)