Qt使用QSS

来源:互联网 发布:上海德比软件 编辑:程序博客网 时间:2024/05/20 23:39

1.    新建工程Qss,父类为MainWindow

2.    在工程目录下,新建文本文档style.txt,将后缀名改为.qss,即为style.qss

3.    新建资源文件qss.qrc,在该资源文件中添加新文件style.qss,

4.在style.qss中添加下列代码,改变程序中控件的样式

QMainWindow,QTabWidget,QTableWidget,QLineEdit,QComboBox,QTextEdit,QCheckBox,QSpinBox,QHeaderView,QScrollBar

{

    background-color:rgba(134,183,200,255);

}

 

QPushButton

{

    background-color:rgb(134,183,200);

    border:2pxsolidrgb(0,255,255);

    border-radius:5px;

    color:white;

}

QLineEdit,QComboBox

{

    background-color:rgb(134,183,200);

    border:2pxsolid#5F92B2;

    border-radius:3px;

}

 

QPushButton:hover

{

    background-color:rgb(0,130,150);

    border:2pxsolid#5F92B2;

    border-radius:5px;

    color:white;

}

 

QPushButton:hover:pressed

{

    background-color:rgb(85,170,255);

    border:2pxsolid#3C80B1;

    border-radius:5px;

    color:white;

}

5.在main.cpp中添加下列代码
#include "mainwindow.h"
#include <QApplication>
#include <QFile>

int main(int argc, char *argv[])

{

    QApplicationa(argc, argv);

    QString strQss;

    QFileqssFile(":/style.qss");

    qssFile.open(QFile::ReadOnly);

   if(qssFile.isOpen())

    {

       strQss=QLatin1String(qssFile.readAll());

       qApp->setStyleSheet(strQss);

        qssFile.close();

    }

    MainWindow w;

    w.show();

    returna.exec();

}

使用后:


原创粉丝点击