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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值