QT学习:调色板
在實際應用中,經常需要改變某個控件的顏色外觀,如背景、文字顏色等。Qt提供的調色板類QPalette專門用于管理對話框的外觀顯示。QPalette類相當于對話框或控件的調色板,它管理著控件或窗體的所有顏色信息。每個窗體或控件都包含一個QPalette對象,在顯示時,按照它的QPalette對象中對各部分各狀態下的顏色的描述進行繪制。
在介紹具體用法時我們先來了解以下基本概念,QPalette類有兩個基本的概念: 一個是 ColorGroup另一個是ColorRole。其中,ColorGroup 指的是以下三種不同的狀態。
(1)QPalette::Active:獲得焦點的狀態。
(2)QPalette:Inactive:未獲得焦點的狀態。
(3)QPalette:Disable:不可用狀態。
其中,Active 狀態與Inactive狀態在通常情況下,顏色顯示是一致的, 也可以根據需要設置為不一樣 的顏色。
ColorRole指的是顏色主題,即對窗體中不同部位顏色的分類。例如,Plte:Window是指背景色,QPalette::WindowText 是指前景色。
QPalette類使用最多、最重要的函數是setColor()函數,此外QPalette類同時還提供了setBrush()函數, 通過畫刷的設置對顯示進行更改,這樣就有可能使用圖片而不僅是單一的顏色來對主題進行填充。Qt之前的版本中有關背景色設置的函數如setBackgroundColor()在 Qt5中都被廢止,統一由QPalette類進行管理。
接下來我們用代碼來熟悉具體用法:
頭文件為:
cpp文件為:
#include "palette.h" #include <QHBoxLayout> #include <QGridLayout> Palette::Palette(QWidget *parent): QDialog(parent) {createCtrlFrame();createContentFrame();QHBoxLayout *mainLayout =new QHBoxLayout(this);mainLayout->addWidget(ctrlFrame);mainLayout->addWidget(contentFrame); }void Palette::createCtrlFrame() {ctrlFrame =new QFrame; //顏色選擇面板windowLabel =new QLabel(tr("QPalette::Window: "));windowComboBox =new QComboBox; //創建一個QComboBox對象fillColorList(windowComboBox); //向下拉列表框中插入各種不同的顏色選項connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow()));//連接下拉列表框的active()信號與高邊背景色的槽函數ShowWindow()windowTextLabel =new QLabel(tr("QPalette::WindowText: "));windowTextComboBox =new QComboBox;fillColorList(windowTextComboBox);connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindowText()));buttonLabel =new QLabel(tr("QPalette::Button: "));buttonComboBox =new QComboBox;fillColorList(buttonComboBox);connect(buttonComboBox,SIGNAL(activated(int)),this,SLOT(ShowButton()));buttonTextLabel =new QLabel(tr("QPalette::ButtonText: "));buttonTextComboBox =new QComboBox;fillColorList(buttonTextComboBox);connect(buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowButtonText()));baseLabel =new QLabel(tr("QPalette::Base: "));baseComboBox =new QComboBox;fillColorList(baseComboBox);connect(baseComboBox,SIGNAL(activated(int)),this,SLOT(ShowBase()));QGridLayout *mainLayout=new QGridLayout(ctrlFrame);mainLayout->setSpacing(20);mainLayout->addWidget(windowLabel,0,0);mainLayout->addWidget(windowComboBox,0,1);mainLayout->addWidget(windowTextLabel,1,0);mainLayout->addWidget(windowTextComboBox,1,1);mainLayout->addWidget(buttonLabel,2,0);mainLayout->addWidget(buttonComboBox,2,1);mainLayout->addWidget(buttonTextLabel,3,0);mainLayout->addWidget(buttonTextComboBox,3,1);mainLayout->addWidget(baseLabel,4,0);mainLayout->addWidget(baseComboBox,4,1); }void Palette::createContentFrame() {contentFrame =new QFrame; //具體顯示面板label1 =new QLabel(tr("請選擇一個值:"));comboBox1 =new QComboBox;label2 =new QLabel(tr("請輸入字符串:"));lineEdit2 =new QLineEdit;textEdit =new QTextEdit;QGridLayout *TopLayout =new QGridLayout;TopLayout->addWidget(label1,0,0);TopLayout->addWidget(comboBox1,0,1);TopLayout->addWidget(label2,1,0);TopLayout->addWidget(lineEdit2,1,1);TopLayout->addWidget(textEdit,2,0,1,2);OkBtn =new QPushButton(tr("確認"));CancelBtn =new QPushButton(tr("取消"));QHBoxLayout *BottomLayout =new QHBoxLayout;BottomLayout->addStretch(1);BottomLayout->addWidget(OkBtn);BottomLayout->addWidget(CancelBtn);QVBoxLayout *mainLayout =new QVBoxLayout(contentFrame);mainLayout->addLayout(TopLayout);mainLayout->addLayout(BottomLayout); }void Palette::ShowWindow() {//獲得當前選擇的顏色值QStringList colorList = QColor::colorNames();QColor color = QColor(colorList[windowComboBox->currentIndex()]);QPalette p = contentFrame->palette(); //獲得右部窗體contentFrame的調色板信息p.setColor(QPalette::Window,color); //設置窗體的Window類顏色,即背景色//把修改后的調色板信息應用到contentFrame窗體中,更新顯示contentFrame->setPalette(p);contentFrame->update(); }void Palette::ShowWindowText() {QStringList colorList = QColor::colorNames();QColor color = colorList[windowTextComboBox->currentIndex()];QPalette p = contentFrame->palette();p.setColor(QPalette::WindowText,color);contentFrame->setPalette(p); }void Palette::ShowButton() {QStringList colorList = QColor::colorNames();QColor color =QColor(colorList[buttonComboBox->currentIndex()]);QPalette p = contentFrame->palette();p.setColor(QPalette::Button,color);contentFrame->setPalette(p);contentFrame->update(); }void Palette::ShowButtonText() {QStringList colorList = QColor::colorNames();QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]);QPalette p =contentFrame->palette();p.setColor(QPalette::ButtonText,color);contentFrame->setPalette(p); }void Palette::ShowBase() {QStringList colorList = QColor::colorNames();QColor color = QColor(colorList[baseComboBox->currentIndex()]);QPalette p = contentFrame->palette();p.setColor(QPalette::Base,color);contentFrame->setPalette(p); }void Palette::fillColorList(QComboBox *comboBox) {QStringList colorList = QColor::colorNames(); //獲得qt所有內置名稱的顏色名列表QString color; foreach(color,colorList) //對顏色名列表進行遍歷{QPixmap pix(QSize(70,20)); //新建對象作為顯示顏色的圖標pix.fill(QColor(color)); //為pix填充當前遍歷的顏色comboBox->addItem(QIcon(pix),NULL); //為下拉列表框插入條目comboBox->setIconSize(QSize(70,20)); //設置圖標尺寸comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);//設置下拉列表的尺寸調整策略} }Palette::~Palette() {}main函數內容為:
#include "palette.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Palette w;w.show();return a.exec(); }運行結果如下圖所示:
總結
- 上一篇: QT学习:进度条
- 下一篇: QT学习:QTime类