Qt学习笔记12:外观设计(QPalette)


零. 参考文献

  1. QPalette类详细使用方法
  2. Qt学习笔记之QPalette调色板类
  3. QPalette实例教程

一. QPalette简介

QPalette( [ˈpælət] 调色板)类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息,每个窗体或控件都包含一个QPalette对象,在显示时按照它的QPalette对象中对各部分各状态下的颜色的描述来进行绘制。

二. QPalette常用函数

void    setBrush(ColorRole role, const QBrush & brush)
void    setBrush(ColorGroup group, ColorRole role, const QBrush & brush)
void    setColor(ColorGroup group, ColorRole role, const QColor & color)
void    setColor(ColorRole role, const QColor & color)

三. ColorGroup & ColorRole

QPalette有两个基本的概念:一个是ColorGroup;一个是ColorRole。

  • ColorGroup(设置widget不同状态的色彩)
名称说明
Active Group该组的颜色用于当前活动的(active)窗口,即具有键盘或鼠标焦点的窗口
Inactive Group该组的颜色用于非活动的(其他)窗口
Disable Group该组的颜色用于窗口为不可用的(disabled)子窗口部件(不包含窗口)
  • ColorRole(设置不同部位的颜色)
名称说明
QPalette::Window窗口部件背景色(QPalette.Background已废弃)
QPalette.WindowText窗口部件前景色(文本颜色)(QPalette.Foreground已废弃)
QPalette.Base文本输入部件(QLineEdit等)背景色
QPalette.Text文本输入部件(QLineEdit等)前景色(文本颜色)
QPalette.Button按钮部件背景色(注: QPushButton的背景颜色涉及样式表, 所以不能通过QPalette修改)
QPalette.ButtonText按钮部件前景色(文件颜色)
QPalette.Highlight文字被选中后的背景色
QPalette.HighlightedText文字被选中后的前景色(文本颜色)
QPalette.ToolTipBaseQToolTip背景色
QPalette.ToolTipTextQToolTip前景色
Qpalette.AlternateBase在交替行颜色的视图中作为交替背景色

四. 实例代码

//.h
#ifndef PALETTE_H
#define PALETTE_H

#include <QDialog>
#include<QComboBox>
#include<QFrame>

namespace Ui {
class Palette;
}

class Palette : public QDialog
{
    Q_OBJECT

public:
    explicit Palette(QWidget *parent = 0);
    ~Palette();

    void createSelectFrame();
    void createContentFrame();
    void fillColorList(QComboBox *);

private:
    Ui::Palette *ui;

public slots:
    void sl_window();
    void sl_windowText();
    void sl_button();
    void sl_buttonText();
    void sl_base();
private:

    QFrame *selectFrame;    //颜色选择面板
    QFrame *contentFrame;   //具体显示面板
    QComboBox *cbbWindow;
    QComboBox *cbbWindowText;
    QComboBox *cbbButton;
    QComboBox *cbbButtonText;
    QComboBox * cbbBase;
};

#endif // PALETTE_H

//.c
#include "palette.h"
#include "ui_palette.h"


#include <QPalette>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QStringList>
#include <QColor>
#include <QPixmap>
#include <QSpinBox>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>

Palette::Palette(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Palette)
{
    ui->setupUi(this);

    createSelectFrame();
    createContentFrame();
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(selectFrame);
    mainLayout->addWidget(contentFrame);
    mainLayout->setMargin(10);
    mainLayout->setSpacing(5);
    //固定窗口的大小,且根据内部控件所占用的位置自动调节大小
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}

Palette::~Palette()
{
    delete ui;
}

void Palette::createSelectFrame()
{
    selectFrame = new QFrame;

    QLabel *labWindow = new QLabel(tr("QPalette::Window:"));
    QLabel *labWindowText = new QLabel(tr("QPalette::WindowText:"));
    QLabel *labButton = new QLabel(tr("QPalette::Button:"));
    QLabel *labButtonText = new QLabel(tr("QPalette::ButtonText:"));
    QLabel *labBase = new QLabel(tr("QPalette::Base:"));

    //组合框
    cbbWindow = new QComboBox;
    fillColorList(cbbWindow);
    connect(cbbWindow, SIGNAL(activated(int)), this, SLOT(sl_window()));
    cbbWindowText = new QComboBox;
    fillColorList(cbbWindowText);
    connect(cbbWindowText, SIGNAL(activated(int)), this, SLOT(sl_windowText()));
    cbbButton = new QComboBox;
    fillColorList(cbbButton);
    connect(cbbButton, SIGNAL(activated(int)), this, SLOT(sl_button()));
    cbbButtonText = new QComboBox;
    fillColorList(cbbButtonText);
    connect(cbbButtonText, SIGNAL(activated(int)), this, SLOT(sl_buttonText()));
    cbbBase = new QComboBox;
    fillColorList(cbbBase);
    connect(cbbBase, SIGNAL(activated(int)), this, SLOT(sl_base()));

    QGridLayout *ctrlLayout = new QGridLayout(selectFrame);
    ctrlLayout->addWidget(labWindow, 0, 0);
    ctrlLayout->addWidget(labWindowText, 1, 0);
    ctrlLayout->addWidget(labButton, 2, 0);
    ctrlLayout->addWidget(labButtonText, 3, 0);
    ctrlLayout->addWidget(labBase, 4, 0);
    ctrlLayout->addWidget(cbbWindow, 0, 1);
    ctrlLayout->addWidget(cbbWindowText, 1, 1);
    ctrlLayout->addWidget(cbbButton, 2, 1);
    ctrlLayout->addWidget(cbbButtonText, 3, 1);
    ctrlLayout->addWidget(cbbBase, 4, 1);

    ctrlLayout->setMargin(5);  //设置边距
    ctrlLayout->setSpacing(5); //间距

}
void Palette::fillColorList(QComboBox *cbb)
{
    QStringList colorNameList = QColor::colorNames();

    QString colorName;
    foreach(colorName, colorNameList)
    {
        QPixmap pix_color(70, 20);
        pix_color.fill(QColor(colorName));

        cbb->addItem(QIcon(pix_color), NULL);
        cbb->setIconSize(QSize(70, 20));
        cbb->setSizeAdjustPolicy(QComboBox::AdjustToContents);   //设置下拉列表的尺寸符合内容的大小
    }
}
void Palette::createContentFrame()
{
    contentFrame = new QFrame;
    QLabel *labValue = new QLabel("Please select one of the values");
    QSpinBox *spbValue = new QSpinBox;

    QHBoxLayout *valueLayout =new QHBoxLayout;
    valueLayout->addWidget(labValue);
    valueLayout->addWidget(spbValue);
    valueLayout->setSpacing(5);

    QLabel *labString = new QLabel("please input a string");
    QLineEdit *edtString = new QLineEdit;
    QHBoxLayout *stringWidget = new QHBoxLayout;
    stringWidget->addWidget(labString);
    stringWidget->addWidget(edtString);
    stringWidget->setSpacing(5);

    QTextEdit *edtHello = new QTextEdit("hello Qt");

    QPushButton *btnOk = new QPushButton("OK");
    QPushButton *btnCancel = new QPushButton("Cancel");

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);  //添加一个填充
    buttonLayout->addWidget(btnOk);
    buttonLayout->addWidget(btnCancel);
    buttonLayout->setSpacing(5);

    QVBoxLayout *contentLayout = new QVBoxLayout(contentFrame);
    contentLayout->addLayout(valueLayout);
    contentLayout->addLayout(stringWidget);
    contentLayout->addWidget(edtHello);
    contentLayout->addLayout(buttonLayout);

    btnOk->setAutoFillBackground(true);
    btnCancel->setAutoFillBackground(true);
    contentFrame->setAutoFillBackground(true);
}

void Palette::sl_window()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[cbbWindow->currentIndex()]);
    //获取当前调色板
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Window,color);
    contentFrame->setPalette(p);
}
void Palette::sl_windowText()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[cbbWindowText->currentIndex()]);
    //获取当前调色板
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::WindowText, color);
    contentFrame->setPalette(p);
    selectFrame->setPalette(p);

}
void Palette::sl_button()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[cbbButton->currentIndex()]);
    //获取当前调色板
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Button, color);
    contentFrame->setPalette(p);

}
void Palette::sl_buttonText()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[cbbButtonText->currentIndex()]);
    //获取当前调色板
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::ButtonText, color);
    contentFrame->setPalette(p);


}
void Palette::sl_base()
{
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[cbbBase->currentIndex()]);
    //获取当前调色板
    QPalette p = contentFrame->palette();
    p.setColor(QPalette::Base, color);
    contentFrame->setPalette(p);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

i胡说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值