文章目录
- 一、效果(动态图)
- 二、样式代码
- 三,加载单个样式表
- 四,换皮肤--切换样式
在项目开发过程中,不可缺少的一部分就是界面的美化,软件做的怎么样,界面风格样式很重要。在qt中通过样式表,*.qss文件加载到程序中美化界面
一、效果(动态图)
二、样式代码
代码如下:
/*灰色*/
QWidget
{background-color: rgb(240,240,240); /*背景色*/color: black; /*字体颜色*/
}
/*按钮正常状态*/
QPushButton
{background-color:rgba(198,198,198);/*背景色*/min-height:30px; /*最小高度*/min-width:180px; /*最小宽度*/border-style:solid;/*边框样式 solid实线 none无边框 inset/outset 3D边框*/border-width:4px; /*边框宽度像素*/border-radius:10px;/*边框圆角半径像素*/border-color:rgba(205,205,205);/*边框颜色*/font-size:12pt;/*字体 字体大小*/color: black;/*字体颜色*/padding:6px; /*填衬*/
}
/*鼠标按下状态*/
QPushButton:pressed
{background-color:rgba(198,198,220);/*背景色*/border-color:rgba(205,205,205);/*边框颜色*/border-style:solid;/*边框样式 solid实线 none无边框 inset/outset 3D边框*/color: black;/*字体颜色*/
}
/*鼠标悬停状态*/
QPushButton:hover
{background-color:rgba(198,198,190);/*背景色*/border-color:rgba(205,205,205);/*边框颜色*/color: black;/*字体颜色*/
}
QLineEdit
{background-color:rgb(240,240,240);/*背景色*/min-height:30px; /*最小高度*/min-width:180px; /*最小宽度*/border-style:solid;/*边框样式 solid实线 none无边框 inset/outset 3D边框*/border-width:4px; /*边框宽度像素*/border-radius:10px;/*边框圆角半径像素*/border-color:rgba(205,205,205);/*边框颜色*/font-size:12pt;/*字体 字体大小*/color: black;/*字体颜色*/padding:6px;/*填衬*/
}
QCheckBox
{color:black;/*字体颜色*/background-color:rgb(240,240,240);/*背景色*/
}
QComboBox
{background-color:rgb(240,240,240);color: black;/*字体颜色*/border-style:solid;/*边框样式 solid实线 none无边框 inset/outset 3D边框*/border-width:4px;/*边框宽度像素*/border-radius:10px;/*边框圆角半径像素*/border-color:rgba(205,205,205);/*边框颜色*/min-height:35px; /*最小高度*/font-size:12pt;
}
/*向下的三角形箭头样式*/
QComboBox::drop-down
{width:20px;border:none;background:transparent;}
/*向下的三角形箭头*/
QComboBox::down-arrow
{image:url(:/image/array_down.png);
}
/*下拉后的整个下拉窗体*/
QComboBox QAbstractItemView
{background-color: rgb(240,240,240); /*背景色*/color: black;/*字体颜色*/
outline:none;
}
/*下拉框下面的item*/
QComboBox QAbstractItemView::item
{height:30px;color: black;/*字体颜色*/
}
/*下拉框item选中*/
QComboBox QAbstractItemView::item:selected
{background-color: rgb(240,240,240); /*背景色*/color: black;/*字体颜色*/
}
QLabel
{color: black;/*字体颜色*/
font-size:12pt;
border:none;/*边框样式*/
}
QTabWidget
{background-color: rgb(240,240,240); /*背景色*/color: black;/*字体颜色*/
}
QTabBar::tab
{background-color: rgb(240,240,240); /*背景色*/color: black;/*字体颜色*/font-size:12pt;/*字体大小*/height:30px; /*高度*/min-width:100px;/*宽度*/border-top-left-radius:4px;/*左上边框圆角半径像素*/border-top-right-radius:4px;/*右上边框圆角半径像素*/margin-right: 5px;/*右边距 右外边距*/padding-left:5px;/*左填充--左内边距*/padding-right:5px;/*右填充--右内边距*/
}
QTabBar::tab:hover
{background-color: rgb(198,198,208); /*背景色*/
}
QTabBar::tab:selected
{background-color: rgb(198,198,198); /*背景色*/
}
QTableView,QTableWidget{background-color: rgb(240,240,240); /*背景色*/color: black;/*字体颜色*/selection-background-color:rgba(192,221,244);/*背景色*/;/*点击选中颜色*/border:1px solid #E0DDDC;/*边框为1像素,灰色*/gridline-color:lightgray;/*这个是表格的格子线的颜色,为亮灰*/font:bold 12pt;/*字体 字体大小*/
}
/*表格表头样式*/
QHeaderView::section{background-color:rgb(240,240,240); /*背景色*/border:0px solid #E0DDDC;/*先把边框宽度设为0,即隐藏所有表头边框*/border-bottom:1px solid #E0DDDC;/*然后只显示下边框,因为上边框和左右边框是整个Table的边框,都显示会有2px的边框宽度*/min-height:30px;;/*表头高度*/font-size:12pt;/*字体大小*/
}
QTreeWidget,QTreeView
{background-color: rgb(240,240,240); /*背景色*/color: rgb(79,129,168);/*字体颜色*/selection-background-color:rgba(5,23,200);/*点击选中颜色*/font-size:12pt;/*字体大小*/
}
/*收起状态*/
QTreeWidget::branch:has-children:!has-siblings:closed,
QTreeWidget::branch:closed:has-children:has-siblings {border-image: none;image: url(:/image/add-line_horizontal.png);
}
/*展开状态*/
QTreeWidget::branch:open:has-children:!has-siblings,
QTreeWidget::branch:open:has-children:has-siblings {border-image: none;image: url(:/image/array_down.png);
}
/*收起状态*/
QTreeView::branch:has-children:!has-siblings:closed,
QTreeView::branch:closed:has-children:has-siblings {border-image: none;image: url(:/image/add-line_horizontal.png);
}
/*展开状态*/
QTreeView::branch:open:has-children:!has-siblings,
QTreeView::branch:open:has-children:has-siblings {border-image: none;image: url(:/image/array_down.png);
}
三,加载单个样式表
QFile file(":/user.qss");
file.open(QFile::ReadOnly);
if (file.isOpen())
{this->setStyleSheet(file.readAll());
}
file.close();
四,换皮肤–切换样式
class CommonStyle
{
public:/*** @brief : 添加样式表* @params : strStyle [in] 路径*/static void setStyle(const QString& strStyle){QFile qss(strStyle);qss.open(QFile::ReadOnly);qApp->setStyleSheet(qss.readAll());qss.close();}
};
在QComboBox的槽函数中实现切换
void StyleWidget::on_m_typeComboBox_currentIndexChanged(int index)
{if(index == 0){CommonStyle::setStyle(":/user.qss");}else if(index ==1){CommonStyle::setStyle(":/user2.qss");}else if(index ==2){CommonStyle::setStyle(":/user3.qss");}else if(index ==3){CommonStyle::setStyle(":/user4.qss");}else if(index ==4){CommonStyle::setStyle(":/user5.qss");}else if(index ==5){CommonStyle::setStyle(":/user6.qss");}
}