qt-14 调色板(QPalette)

特别注意点

控件需要更改背景色一定设置
:xxx->setAutoFillBackground(true);这个属性
QPushBotton:默认属性: OkBtn->setFlat(false); 这个是不会绘制背景色,除非按下
那么OkBtn->setFlat(true); 就可以设置了

Palette.h

#ifndef PALETTE_H
#define PALETTE_H

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

class palette : public QDialog
{
    Q_OBJECT

public:
    palette(QWidget *parent = nullptr);
    ~palette();
    void CreateCtrlFram();//完成窗体左半部分颜色选择区的创建
    void CreateContentFram();//完成窗体右半部分的创建
    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;
    QComboBox* ComboBox2;
    QLineEdit* LineEdit2;
    QTextEdit* TextEdit;
    QPushButton* OkBtn;
    QPushButton* CancelBtn;
};
#endif // PALETTE_H

palette.cpp

#include "palette.h"
#include <QHBoxLayout>
#include <QGridLayout>
#include <QColor>

palette::palette(QWidget *parent)
    : QDialog(parent)
{
    CreateCtrlFram();
    CreateContentFram();
    QHBoxLayout* MainLayout = new QHBoxLayout(this);
    MainLayout->addWidget(CtrlFrame);
    MainLayout->addWidget(ContentFrame);

}

palette::~palette() {}

void palette::CreateCtrlFram()
{
    //实例化,并且绑定事件
    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::BaseLabel:"));
    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::CreateContentFram()
{
    ContentFrame =new QFrame;//具体显示面板
    ContentFrame->setAutoFillBackground(true);
    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("确认"));
    OkBtn->setAutoFillBackground(true);
    OkBtn->setFlat(true);
    CancelBtn = new QPushButton(tr("取消"));
    CancelBtn->setAutoFillBackground(true);
    CancelBtn->setFlat(true);
    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::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();//获取ContentFrame面板的信息
    p.setColor(QPalette::Window,Color);
    //修改ContentFrame颜色,更新显示
    ContentFrame->setPalette(p);
    ContentFrame->update();



}

void palette::ShowWindowText()
{
    //获取当前选择的颜色
    QStringList ColorList = QColor::colorNames();
    QColor Color = QColor(ColorList[WindowTextComboBox->currentIndex()]);
    QPalette p = ContentFrame->palette();//获取ContentFrame面板的信息
    p.setColor(QPalette::WindowText,Color);
    //修改ContentFrame颜色,更新显示
    ContentFrame->setPalette(p);
    ContentFrame->update();
}

void palette::ShowButton()
{
    //获取当前选择的颜色

    QStringList ColorList = QColor::colorNames();
    QColor Color = QColor(ColorList[ButtonComboBox->currentIndex()]);

    QPalette p = ContentFrame->palette();//获取ContentFrame面板的信息

    p.setColor(QPalette::Button,Color);
    //修改ContentFrame颜色,更新显示
    ContentFrame->setPalette(p);
    ContentFrame->update();



}

void palette::ShowButtonText()
{
    //获取当前选择的颜色
    QStringList ColorList = QColor::colorNames();
    QColor Color = QColor(ColorList[ButtonTextComboBox->currentIndex()]);
    QPalette p = ContentFrame->palette();//获取ContentFrame面板的信息
    p.setColor(QPalette::ButtonText,Color);
    //修改ContentFrame颜色,更新显示
    ContentFrame->setPalette(p);
    ContentFrame->update();

}

void palette::ShowBase()
{
    //获取当前选择的颜色
    QStringList ColorList = QColor::colorNames();
    QColor Color = QColor(ColorList[BaseComboBox->currentIndex()]);
    QPalette p = ContentFrame->palette();//获取ContentFrame面板的信息

    p.setColor(QPalette::Base,Color);
    //修改ContentFrame颜色,更新显示
    ContentFrame->setPalette(p);
    ContentFrame->update();
}

main.cpp

#include "palette.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    palette w;
    w.show();
    return a.exec();
}

运行图

在这里插入图片描述

PySide6中的调色板(Palette)用于定义窗口和窗口中控件的颜色。调色板类是QPalette,可以通过设置不同的颜色组(Color Group)和颜色角色(Color Role)来定义调色板。 下面是一个简单的例子: ```python from PySide6.QtGui import QColor, QPalette from PySide6.QtWidgets import QApplication, QLabel, QWidget app = QApplication([]) widget = QWidget() # 创建调色板对象 palette = QPalette() # 设置窗口背景颜色 palette.setColor(QPalette.Window, QColor(53, 53, 53)) widget.setPalette(palette) # 创建标签并设置文本和前景色 label = QLabel("Hello, World!") palette.setColor(QPalette.WindowText, QColor(255, 255, 255)) label.setPalette(palette) widget.show() app.exec() ``` 在上面的例子中,我们创建了一个调色板对象,并设置了窗口背景颜色和标签的前景色。 调色板中的颜色组包括窗口(Window)、按钮(Button)、文本(Text)等,颜色角色包括前景色(WindowText)、背景色(Window)、文本颜色(ButtonText)等。 除了使用默认的颜色组和颜色角色外,我们还可以使用自定义的颜色组和颜色角色。例如,我们可以通过以下方式创建一个自定义的颜色组: ```python # 创建自定义颜色组 customColorGroup = QPalette.ColorGroup.Normal palette.setColor(customColorGroup, QPalette.ColorRole.Foreground, QColor(255, 0, 0)) ``` 在这个例子中,我们创建了一个自定义颜色组,并设置了其前景色为红色。 更多关于PySide6调色板的使用方法可以参考官方文档:https://doc.qt.io/qtforpython/PySide6/QtGui/QPalette.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值