天天看點

學習QT之調色闆(QPalette)

在實際應用中,經常需要改變某個控件的顔色外觀,如背景、文字顔色等。Qt提供的調色闆類​

​QPalette​

​​專門用于管理對話框的外觀顯示。​

​QPalette​

​​類相當于對話框或控件的調色闆,它管理着控件或窗體的所有顔色資訊。每個窗體或控件都包含一個​

​QPalette​

​​對象,在顯示時,按照它的​

​QPalette​

​對象中對各部分各狀态下的顔色描述進行繪制。

​QPalette​

​有兩個基本的概念:ColorGroup、ColorRole

ColorGroup 說明
QPalette::Active 獲得焦點的狀态
QPalette::InActive 未獲得焦點的狀态
QPalette::Disable 不可用狀态

注:其中,Active狀态與InActive狀态在通常情況下,顔色顯示是一緻的,也可以根據需要設定為不一樣的顔色。

ColorRole 說明
QPalette::Window 窗體部件的背景色
QPalette::WindowText 窗體部件的前景色
QPalette::Base 文本輸入視窗部件(比如QTextEdit,QLinedit,QPlainTextEdit等)的背景色
QPalette::Text 與QPalette::Base一塊使用,指文本輸入視窗部件的前景色
QPalette::Button 按鈕視窗部件的背景色
QPalette::ButtonText 指按鈕視窗部件的前景色
QPalette::Highlight 設定文字高亮時的背景顔色
QPalette::HighlightedText 設定文字高亮時的顔色
QPalette::Link 超連結文字顔色
QPalette::LinkVisted 超連結文字通路後的顔色

​QPalette​

​​類使用最多、最重要的函數是​

​setColor()​

​函數,其原型如下:

void QPalette::setColor(ColorGroup group,ColorRole role,const QColor & color);      

在對主題顔色進行設定的同時,還區分了狀态,即對某個主題在某個狀态下的顔色進行了設定;

void QPalette::setColor(ColorRole role,const QColor & color);      

隻對某個主題的顔色進行設定,并不區分狀态。

​QPalette​

​​類同時還提供了​

​setBrush()​

​函數,通過畫刷的設定對顯示進行更改,這樣就有可能使用圖檔而不是單一的顔色來對主題進行填充。

使用示例

QPalette p;
p.setColor(QPalette::Window,color);//p.setBrush(QPalette::Window,brush);
xxx->setPalette(p);      

以下通過一個執行個體來介紹一下它的使用:

一、運作結果

學習QT之調色闆(QPalette)

二、詳細代碼

#ifndef PALETTE_H
#define PALETTE_H

#include <QDialog>
#include <QComboBox>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>

class Palette : public QDialog
{
    Q_OBJECT

public:
    Palette(QWidget *parent = 0);
    ~Palette();
    void createCtrlFrame();            //完成窗體左半部分顔色選擇區的建立
    void createContentFrame();         //完成窗體右半部分的建立
    void fillColorList(QComboBox *);   //完成顔色下拉清單框中插入顔色的工作

private slots:
    void showWindow();
    void showWindowText();
    void showButton();
    void showButtonText();
    void showBase();

private:
    QFrame *CtrlFrame;  //顔色選擇面闆

    QLabel *windowLabel;
    QComboBox *windowComboBox;
    QLabel *windowTextLabel;
    QComboBox *windowTextComboBox;

    QLabel *buttonLabel;
    QComboBox *buttonComboBox;
    QLabel *buttonTextLabel;
    QComboBox *buttonTextComboBox;

    QLabel *baseLabel;
    QComboBox *baseComboBox;

    QFrame *contentFrame;  //具體顯示面闆
    QLabel *label1;
    QComboBox *comboBox1;
    QLabel *label2;
    QLineEdit *lineEdit2;
    QTextEdit *textEdit;
    QPushButton *okBtn;
    QPushButton *CancelBtn;
};

#endif // PALETTE_H      
#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);
}

Palette::~Palette()
{

}

void Palette::createCtrlFrame()
{
    CtrlFrame = new QFrame;  //顔色選擇面闆
    windowLabel = new QLabel(tr("QPalette::Window: "));
    windowComboBox = new QComboBox;
    fillColorList(windowComboBox);  //向下拉清單框中插入各種不同的顔色選項
    connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(showWindow()));

    windowTextLabel = new QLabel(tr("QPalete::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 *ButtomLayout = new QHBoxLayout;
    ButtomLayout->addStretch(1);
    ButtomLayout->addWidget(okBtn);
    ButtomLayout->addWidget(CancelBtn);
    QVBoxLayout *mainLayout = new QVBoxLayout(contentFrame);
    mainLayout->addLayout(TopLayout);
    mainLayout->addLayout(ButtomLayout);
    okBtn->setAutoFillBackground(true);     //允許自動填充
    CancelBtn->setAutoFillBackground(true);
    contentFrame->setAutoFillBackground(true);
}

void Palette::fillColorList(QComboBox *comboBox)
{
    QStringList colorList = QColor::colorNames();
    QString color;
    foreach (color, colorList) {
       QPixmap pix(QSize(70,20));
       pix.fill(QColor(color));
       comboBox->addItem(QIcon(pix),NULL);
       comboBox->setIconSize(QSize(70,20));
       comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    }
}

void Palette::showWindow()
{
    //獲得目前選擇的顔色值
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Window,color);
    contentFrame->setPalette(p);
    contentFrame->update();
}

void Palette::showWindowText()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::WindowText,color);
    contentFrame->setPalette(p);
}

void Palette::showButton()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->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[windowComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::ButtonText,color);
    contentFrame->setPalette(p);
}

void Palette::showBase()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Base,color);
    contentFrame->setPalette(p);
}