QT stylesheet 操作

QT style sheet的基本原理就是利用QSS語言對軟件的外觀進行定製。QSS可以直接在代碼中輸入,也可以保存在一個文本文件中,利用文件讀取函數在軟件運行時 讀入到一個文本串中,再利用QApplication類的函數setStyleSheet(&QString)使其生效。實例代碼:

 

  1. #include <QTextStream>  
  2. #include <QFile>  
  3.     QFile file("./styles/default.qss");  
  4.     file.open(QFile::ReadOnly);  
  5.     QString styleSheet = file.readAll();//QLatin1String(file.readAll());  
  6.     a.setStyleSheet(styleSheet);  
[喝小酒的網摘]http://blog.const.net.cn/a/12544.htm
//-----------------------------------------------------------------------------------------------使用的例子:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    
private:
    Ui::Dialog *ui;
    QGridLayout *layout1;
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;
    QPushButton *btn4;
    QLineEdit   *edit1;
};
#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
        this->setWindowFlags(this->windowFlags()&Qt::WindowMaximizeButtonHint&Qt::WindowMinimizeButtonHint); //爲對話框添加上最大化和最小化按鈕
    //    layout=new QBoxLayout(this);
        layout1=new QGridLayout(this);
        btn1=new QPushButton(this);
        btn1->setStyleSheet("QPushButton{color:red;background:yellow}"); //設定前景顏色,就是字體顏色
    //    btn1->setStyleSheet("QPushButton{background:yellow}");
        btn1->setText("Button1");
        btn2=new QPushButton(this);
        btn2->setStyleSheet("QPushButton{color:red;background-color:rgb(200,155,100)}"); //使用rgb來設定背景顏色
        btn2->setText("Button2");
         btn3=new QPushButton(this);
         btn3->setStyleSheet("QPushButton{background-image:url(image/1.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-attachment: fixed;background-attachment: fixed;;background-clip: padding}");
         //設定按鈕的背景圖片,background-repeat可以設定背景圖片的重複規則,這裏設定僅在xy方向都重複,所以圖片會被重複一次
         //background-position用來設定圖片的位置,是左(left)還是右(right),還是在中間(center),是上(top)還是底部(bottom)
         //background-attachment用來這定背景圖片是否捲動或者和窗口大小相匹配,默認是捲動的
         btn3->setText("Button3");
         btn4=new QPushButton(this);
         btn4->setStyleSheet("QPushButton{border: 3px solid red;border-radius:8px}"); //設定邊框寬度以及顏色
         //可以使用border-top,border-right,border-bottom,border-left分別設定按鈕的上下左右邊框,
         //同樣有border-left-color, border-left-style, border-left-width.等分別來設定他們的顏色,樣式和寬度
         //border-image用來設定邊框的背景圖片。
         //border-radius用來設定邊框的弧度。可以設定圓角的按鈕
         btn4->setText("Button4");
         //字體設定
         //font-family來設定字體所屬家族,
         //font-size來設定字體大小
         //font-style來設定字體樣式
         //font-weight來設定字體深淺
         //height用來設定其高低
         //selection-color用來設定選中時候的顏色
         edit1=new QLineEdit(this);
         edit1->setStyleSheet("QLineEdit{font: bold italic large "Times New Roman";font-size:25px;color:rgb(55,100,255);height:50px;border:4px solid rgb(155,200,33);border-radius:15px;selection-color:pink}");
         //父窗口的設定
         //icon-size來設定圖片大小
         this->setWindowIcon(QIcon("image/1.png"));
          this->setStyleSheet("QWidget{background:write url(image/2.png);icon-size:20px 5px}");  //設定整個對話框的背景顏色
    //      this->setStyleSheet("QWidget{icon-size:20px 5px}");
        layout1->addWidget(btn1,0,0);
        layout1->addWidget(btn2,0,1);
        layout1->addWidget(btn3,1,0);
        layout1->addWidget(btn4,1,1);
        layout1->addWidget(edit1,2,0);
  setLayout(layout1);
}
Dialog::~Dialog()
{
    delete ui;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章