實(shí)現(xiàn) Qt 統(tǒng)一風(fēng)格與換膚實(shí)例
實(shí)現(xiàn) Qt 統(tǒng)一風(fēng)格與換膚實(shí)例是本文介紹的內(nèi)容,不多說,進(jìn)圖內(nèi)容。QT既然作為一個成功的跨平臺GUI庫,當(dāng)然會想到界面風(fēng)格統(tǒng)一,本地化與換膚都做了考慮,網(wǎng)上有多種換膚的方法,自己也習(xí)慣于用簡單的方法,總結(jié)了一下自己心得,與大家分享
一、改變與裝飾界面:
通常使用設(shè)置Widget的背景,邊界,子對象的圖片,顏色來進(jìn)行裝飾QWidget,
1、改變被景圖片:
較常用辦法是
- QWidget.setAutoFillBackground(true);
- QPalette palette= QWidget.Palette();
- QPixmap pic("xxx.png"); palette.setBrush(QPalette::Window, new Brush(pic));
- palette.setBrush(QPalette::Base, new Brush(pic));
- palette.setBrush(QPalette::Button, new Brush(pic));
- QWidget.setPalette(palette);
但不同的控件還是要區(qū)別對待,比如對待QPushButton簡單用此方法就不行,還要設(shè)置為QPushButton.setFlat(true),也可以用QPushButton的setImage方法,還有對待 QTabWidget不僅要改變背景圖片,還要改變Tab的圖片,Tab還要區(qū)別改變被選中的Tab與沒被選中的Tab圖片,QTreeWidget,QTreeView中還要針對樹裝結(jié)節(jié)的圖片,還有標(biāo)題欄等,都要用不同的方法來設(shè)置,QT有一種更方便的解決方案,就是設(shè)置StyleSheet被稱為QSS方法,類似網(wǎng)頁設(shè)計中的CSS, 語法也幾乎相同Idential,比如要設(shè)置QPushButton的背景圖,鼠標(biāo)over,及按下的圖片切換,可以用如下方法:
- const char* normal = “bg.png";
- const char* pressed = "pressed.png;
- const char* over = "over.png";
- char str[512] = {0};
- sprintf(str,"QPushButton{background-image:url(%s);border-style.:flat;} QPushButton:hover:pressed{background-image:url(%s);border-
- style.:flat;}QPushButton:hover:!pressed{background-image:url(%s);border-style.:flat;}", normal, pressed, over);
- w->setStyleSheet(QString(str));
2、改變顏色:
常用:QWidget->setBackgroundColor();來改變,當(dāng)然也可以用Style. Sheet方法。
二、全局統(tǒng)一風(fēng)格:
上面提到了QSS,QT還可以通過設(shè)置QApplication的StyleSheet來改變程序中所有控件的風(fēng)格。比如:
QApplication.setStyleSheet(QString("QPushButton{background-image:url(bg.png); border-style.:flat;}")); 這樣的話,所有QPushButton的實(shí)例對象的背景圖片缺省情況下都是bg.png,當(dāng)然,如果你重新設(shè)置某個QPushButton實(shí)例對象的 StyleSheet,那個實(shí)例對象的風(fēng)格以當(dāng)前設(shè)置的為準(zhǔn)。
這樣對界面的風(fēng)格統(tǒng)一提供了一個簡單有效的解決方案。
三、簡單介紹QSS:
google_protectAndRun("render_ads.js::google_render_ad", google_handleError, google_render_ad);
小結(jié):實(shí)現(xiàn) Qt 統(tǒng)一風(fēng)格與換膚的內(nèi)容實(shí)例的內(nèi)容介紹完了,希望本文對你有所幫助!