在Qt中,美化GUI界面最主要用到了样式表。具体情况大家可以在网上找一些相关资料了解一下。
现在本人主要讲Qt中样式表的基本用法:
首先给大家看一段基本代码:
QPushButton{
color: white;
font: bold 10pt;
border:none;
min-height: 24px;
min-width: 60px;
background:url(:images/SimpleButtom.png) -60px 0px no-repeat;
}
QPushButton:hover{
color: lightgray;
background:url(:images/SimpleButtom.png) 0px 0px no-repeat;;
}
QPushButton:pressed {
color: lightgray;
background:url(:images/SimpleButtom.png) -120px 0px no-repeat;;
padding-top: -15px;
padding-bottom: -17px;
}
*{ font-size:13px; color:white; font-family:"宋体"; } CallWidget QLineEdit#telEdt { font-size:24px; } QMainWindow,QDialog{ background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #113845, stop: 1.0 #15A8FF); } QWidget{ background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #113845, stop: 1.0 #15A8FF); } QLabel{ background:transparent; } QTreeWidget{ background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #113845, stop: 1.0 #15A8FF); } QMenuBar { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #15A8FF, stop: 1.0 #113845); } QMenuBar::item { spacing: 3px; padding: 1px 4px; border-radius: 1px; font:#BBDDFF; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #113845, stop: 1.0 #15A8FF); } QMenuBar::item:selected { background: #1A2432; font:#FFFFFF; } QMenuBar::item:pressed { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #10151E, stop:1 #2C547E); font:#FFFFFF; } QMenuBar::item:hover{ border-width:1px; border-color:#516589; background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #9979A, stop:1 #2D567F); font:#FFFFFF; } QMenu { background-color: #030406; border-width:0px; margin: 2px; } QMenu::item { background-color: #1D2838; padding: 2px 25px 2px 20px; border-color:transparent; color:#858E94; } QMenu::item:!enabled { background-color: #1D2838; padding: 2px 25px 2px 20px; border-color:transparent; color:#76746C; } QMenu::item:selected { border-width:1px; border-color: #516589; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #1E293A, stop: 1.0 #2C547E); color:#E6FFFF; } QMenu::icon:checked { background: gray; border: 1px inset gray; position: absolute; top: 1px; right: 1px; bottom: 1px; left: 1px; } QMenu::separator { height: 1px; background: #1D99A9; } QMenu::indicator { width: 13px; height: 13px; }
以上代码只是个别的控件的样式设置,其他控件的样式设置仿照上面的语法即可。
那么下面我们就看一下应该如何使用样式表(具体代码):
QString StyleString; StyleString=underlineBox->currentText(); if(StyleString=="皮肤1") { QFile file(":/qss/abc.qss"); file.open(QFile::ReadOnly); styleSheet = QLatin1String(file.readAll()); qApp->setStyleSheet(styleSheet); file.close(); }
因为文件已经被我添加到资源文件中,所以文件路径才写成(":/qss/abc.qss");,具体情况当然由大家定了。
当然了,你要现在对应的头文件中声明:QString styleSheet;//用于设置样式表
以上的语句可以放在main函数中也可以放在其他函数中