1. 程式人生 > >Qt: 調色盤QPalette類用法詳解(附例項、原始碼)

Qt: 調色盤QPalette類用法詳解(附例項、原始碼)

  • 在實際的應用中,經常需要對某個控制元件的顏色外觀,如背景、前景色等,進行設定。

  • Qt中提供的調色盤QPalette類就是專門用於管理控制元件的外觀顯示。QPalette類相當於對話方塊或控制元件的調色盤,管理著控制元件和窗體的所有顏色。

  • 每個窗體和控制元件都包含一個QPalette物件,在顯示時,對其做相應的設定即可。

QPalette有兩個基本的概念:一個是ColorGroup;一個是ColorRole。

ColorGroup有三種不同的狀態:

  • Active:啟用狀態(獲得焦點狀態)

  • Disabled:禁用狀態(未獲得焦點狀態)

  • Inactive:未啟用狀態(不可用狀態)

通常情況下:在大多數風格,活躍的和不活躍的看起來一樣。

原版英文描述如下:

The color groups:

The Active group is used for the window that has keyboard focus.
The Inactive group is used for other windows.
The Disabled group is used for widgets (not windows) that are disabled for some reason.
Both active and inactive windows can contain disabled widgets. (Disabled widgets are often called inaccessible or grayed out.)

ColorRole:


設定控制元件顏色的方法是先呼叫QWidget::palette()獲取當前面板,修改它為自定義的值後再通過方法QWidget::setPalette設定為新修改的面板。程式碼如下所示

QPalette palette = widget->palette();  
palette.setColor(QPalette::Window, Qt::lightGray);  //改變控制元件背景色  
palette.setColor(QPalette::WindowText, Qt::blue);   //改變控制元件字型顏色  
...  
widget->setPalette(palette); 

常用的設定顏色方法如下:
(1) void QPalette::setBrush ( ColorRole role, const QBrush & brush )
改變所有組下指定角色role的畫刷顏色值。

(2) void QPalette::setBrush ( ColorGroup group, ColorRole role, const QBrush & brush )
改變指定組group下的指定角色role的畫刷顏色值。

(3) void QPalette::setColor ( ColorRole role, const QColor & color )
改變所有組下指定角色role的顏色值。

(4) void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color )
改變指定組group下指定角色role的顏色值。

注意:在以上程式碼之前,必須先呼叫函式 setAutoFillBackground(true),設定窗體執行自動填充。

例項:

內容:利用QPalette改變控制元件顏色

效果如下:


步驟:

1、新建Qt GUI應用,專案名稱為“Palette” (可以自定義,不過儘量見名知義),基類選擇“QDialog”,類名為“Palette”,取消建立介面按鈕。

一直單擊下一步,完成建立工程。專案工程結構如下:


2、開啟“Palette.h”檔案,新增如下程式碼:

#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


3、開啟“Palette.cpp”檔案,新增如下程式碼:
#include "palette.h"
#include <QBoxLayout>
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;
    fillColorList(windowComboBox);
    connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(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);

    okBtn->setAutoFillBackground(true);     //允許自動填充
    cancelBtn->setAutoFillBackground(true);
    contentFrame->setAutoFillBackground(true);
}

void Palette::showWindow()  //用於控制背景顏色的顯示
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox->currentIndex()]);

   // contentFrame->setAutoFillBackground(true);

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Window, color);
    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::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();
    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::showButton()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[buttonComboBox->currentIndex()]);

    //contentFrame->setAutoFillBackground(true);

    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Button , color);
    contentFrame->setPalette(p);

    contentFrame->update();

}

Palette::~Palette()
{

}

4、執行專案,效果如前所示。

注意:在設定控制元件背景色填充時,一定要先呼叫setAutoFillBackground(true)函式,來執行自動填充背景。不然,程式中填充背景的程式碼不會起作用的。

(陸文周先生的《Qt5開發及例項》這本教材中便忘記了新增這一句程式碼,所以程式中設定Window和Button的背景色這個功能沒有效果,我就掉進了這個陷阱)。

英文文件讀不懂怎麼辦,當然是多讀書多記單詞~\(≧▽≦)/~啦啦啦。

為什麼要讀書?

舉個例子:
當你看到夕陽餘暉…

你的腦海浮現的是:“落霞與孤鶩齊飛,秋水共長天一色。”

而不是:“臥槽,這麼多鳥,真好看,真他媽太好看了!真屌,屌爆了!!”