- #include <QTextStream>
- #include <QFile>
- QFile file("./styles/default.qss");
- file.open(QFile::ReadOnly);
- QString styleSheet = file.readAll();//QLatin1String(file.readAll());
- a.setStyleSheet(styleSheet);
#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); l ayout1->addWidget(btn3,1,0); layout1->addWidget(btn4,1,1); layout1->addWidget(edit1,2,0); setLayout(layout1);}Dialog::~Dialog(){ delete ui;}
------------------------------QPushBUtton美化
第一篇
http://blog.csdn.net/yiyaaixuexi/article/details/6706729
第二篇
http://blog.csdn.net/yiyaaixuexi/article/details/6343337
有不少朋友都抱怨,爲何本身使Qt作的小項目,UI那麼土那麼俗,方方框框,基本控件很醜,要不是Qt的跨平臺,纔不去學習它。呵呵,其實我想說,嵌入式系統中的圖形界面,統統交給QT,絕對沒問題!
簡單說說自定義Button,QPushButton是經常使用組件之一,先看下效果。
當單擊按鈕後,效果如圖:
實現代碼:
這下明白嘍?咱們只是須要在mousePressEvent和mouseReleaseEvent中,添加setIcon(QIcon(*buttonPicture))的處理,就讓Button動起來了o(∩_∩)o ...