http://www.cnblogs.com/coffeegg/archive/2011/11/15/2249452.html(轉)
http://blog.csdn.net/cgzhello1/article/details/8514113
加載Qss文件http://www.tuicool.com/articles/fAF7zq
Qss 實例http://blog.csdn.net/sdljlcming/article/details/8602659
將文件添加到樹形工程中:http://blog.csdn.net/lyc_daniel/article/details/8813121
qt值qss(stylesheet)用法簡介(一) :使用qt寫程序,總要有界面,而美工給程序猿的設計總是那么美輪美奐,那么該怎么樣將效果圖轉化為實際圖http://blog.csdn.net/cgzhello1/article/details/8514113
------------------------------------------------
摘要:
QT Create中,對button或者window等部件設置背景、顏色、透明等樣式時,需要用到style sheet,可以直接在代碼里寫,也可以在ui設計師里通過右鍵change style sheet的方法來設置,本人是使用的后面的方式,下面介紹幾種常見的用法。
介紹:
作者使用的開發環境為:qt creator 201005, qt4.7。
1、利用style sheet給窗體設置背景
使用stylesheet設置背景圖片還是有一些要注意的,如果是在mainwindow和dialog中,直接右鍵change style sheet在add resource中選擇background-image或者border-image,選擇資源文件中的圖片就行了(前者是按像素顯示圖片,后者可根據窗體大小來自動縮放圖片),如下圖:
但在widget中就不行,你會發現,用同樣的方法,背景並沒有發生改變,而僅僅是它的子窗體背景圖片發生了改變。
那么在widget中要如何做呢,我們在widget中放置一個frame,然后對frame通過stylesheet設置背景,后面窗體里所有的部件都放在這個frame里。
我們知道,子窗體會繼承父窗體的屬性,也就是說,父窗體的背景,在子窗體中也會有,那如何讓子窗體不繼承父窗體的背景呢,同樣的,還是在Edit Style Sheet里,需下輸入如下代碼:
#desktop {
border-image: url(:/images/desktop.jpg);
}
#desktop * {
border-image:url();
}
desktop是你的窗體名。
2、menubar設置透明
我想做一個菜單按鈕,像ubuntu的應用程序菜單一樣,能在點擊時彈出一個下拉框,我選擇了用mainwindow來實現,但我如現menuba顯示在頂層很難看,如何才能不讓它顯示呢。
設置menuba的stylesheet,
background-color:transparent
將背景設置為透明。
3、tool button不顯示邊框
當我們設置button的icon時,發現icon的形狀並不與button完全一致,如下圖:
設置stylesheet
border-style: flat;
效果如下:
注意,一定要選擇tool button,而不要選擇push button,因為push button活動的時候會有一個虛線框。
要達到上圖的效果,除了設置border-style:flat,可不可以將style設置為transparent呢?設置成transparent后,顯示上看,效果是和上圖的一樣,但當按下時,button沒有被圖片覆蓋的地方就會顯示被按下時的顏色。
4、在父窗體的stylesheet中設置子部件的屬性
假設有多個button,且它們的樣式是一樣的,那是不是需要一個個設置呢?不需要的,我們可以在父窗體中進行設置,那么子部件都會繼承它的屬性。
如果某個button的樣式特殊,再單獨修改就行了,下面舉個實例,在父窗體的stylesheet中設置如下
QDialog{background:rgb(229, 255, 239)}
QMenuBar{background:url(:/image/header.bmp)}
QStatusBar{background:url(:/image/header.bmp)}
QPushButton{background:url(:/image/header.bmp)}
QLCDNumber{background:url(:/image/lcd.bmp)}
QLabel{color: rgb(0, 0, 255)}
QDial{background-color: rgb(67, 67, 67)}
QGroupBox {
border-width:2px;
border-style:solid;
border-color:#FFFFFF;
}
QGroupBox{color: rgb(255,255, 255)}
QToolButton{border-style: flat;}
結語:
style sheet用起來很方便,以上是總結的幾種用法,后面會繼續補充。
--------------------
關於控件styleSheet的設置方法: 1、Designer方法:直接在屬性的styleSheet添加如下行: color:red;background-image:url("D:/back.jpg"); //設置字體為紅色;設置背景圖像 ;表示共存; 2、代碼方法:setStyleSheet(...)等 3、這樣加載styleSheet的話可能引起整個窗體字號等的變化;而且加載圖像很慢,嚴重影響程序執行速度。 |
在mainwindow.cpp里作如下修改:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>//加入這兩個頭文件
#include <QTextStream>
void loadStyleSheet(QString qssName)//加入下面函數
{
QFile data(qssName);
QString qssFile;
if(data.open(QFile::ReadOnly))
{
QTextStream styleIn(&data);
qssFile = styleIn.readAll();
data.close();
qApp->setStyleSheet(qssFile);
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
loadStyleSheet(QString(":/style1.qss"));//在構造函數里添加這一句,加載stylesheet
ui->setupUi(this);
}
=============================================
QT stylesheet 操作
- #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 ...


