Qt样式表的使用

来源:互联网 发布:ubuntu 编译c 编辑:程序博客网 时间:2024/06/08 04:55

在Qt中,美化GUI界面最主要用到了样式表。具体情况大家可以在网上找一些相关资料了解一下。

现在本人主要讲Qt中样式表的基本用法:

首先给大家看一段基本代码:

*{
  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函数中也可以放在其他函数中
希望此博文能够对大家有帮助吧,如有不足之处,还请大家指正,有什么疑问的话也可以发邮件到本人邮箱,有空必将一一解答.
欢迎转载,请注明出处,谢谢.
原创粉丝点击