Qt 自定义调色板

被调色板困惑很久了,查了网上资料也没有自己想要的,就决定写一个自定义调色板。

直接上代码。

       

头文件:

#pragma once

#include <QToolButton>
#include <QtWidgets>

/*Ivy:下拉框中调色板的颜色*/
const QColor colors[6][10] =
{
    { QColor("#ffffff"), QColor("#e5e5e7"), QColor("#cccccc"), QColor("#9a9a9a"), QColor("#7f7f7f"),
      QColor("#666666"), QColor("#4c4c4c"), QColor("#333333"), QColor("#191919"), QColor("#000000")},

    { QColor("#f759ab"), QColor("#ff4d4f"), QColor("#ffa940"), QColor("#ffdf3d"), QColor("#a0d911"),
      QColor("#52c41a"), QColor("#13c2c2"), QColor("#1890ff"), QColor("#2f54eb"), QColor("#722ed1")},

    { QColor("#ffd6e7"), QColor("#ffccc7"), QColor("#ffe7ba"), QColor("#ffffb8"), QColor("#f4ffb8"),
      QColor("#d9f7be"), QColor("#b5f5ec"), QColor("#bae7ff"), QColor("#d6e4ff"), QColor("#efdbff")},

    { QColor("#ff85c0"), QColor("#ff7875"), QColor("#ffc069"), QColor("#fff566"), QColor("#d3f261"),
      QColor("#95de64"), QColor("#5cdbd3"), QColor("#69c0ff"), QColor("#85a5ff"), QColor("#b37feb")},

    { QColor("#eb2f96"), QColor("#f5222d"), QColor("#fa8c16"),QColor("#fadb14"), QColor("#a0d911"),
      QColor("#52c41a"), QColor("#13c2c2"), QColor("#1890ff"), QColor("#2f54eb"), QColor("#722ed1")},

    { QColor("#9e1068"), QColor("#a8171b"), QColor("#ad4e00"), QColor("#ad8b00"), QColor("#5b8c00"),
      QColor("#006075"), QColor("#006d75"), QColor("#0050b3"), QColor("#10239e"), QColor("#391085")}
};


class BLColorCombox : public QToolButton
{
    Q_OBJECT

public:
    BLColorCombox(QWidget *parent);
    ~BLColorCombox();

    void setButtonIcon(const QString &imageFile, QColor color);
    void setBtnColorChange(const QColor &color);

private:
    QMenu* createColorMenu(const char *slot, const char *slotColorBoard);
    QIcon createColorToolButtonIcon(const QString &imageFile, QColor color);
    QIcon createColorIcon(QColor color);

signals:
    void signColorChanged(QColor color);

private slots:
    void OnColorChanged();           // 文本颜色设置
    void OnShowColorBoard();         // 颜色板
    void slotBtnColorChange(QColor color);

private:
    QLabel * m_pShowColorLable = nullptr;
};

源文件:

#pragma execution_character_set("utf-8") 
#include "bl_color_combox.h"

BLColorCombox::BLColorCombox(QWidget *parent)
    : QToolButton(parent)
{
    setPopupMode(QToolButton::MenuButtonPopup);
    setMenu(createColorMenu(SLOT(OnColorChanged()), SLOT(OnShowColorBoard())));
    setAutoFillBackground(true);

    m_pShowColorLable = new QLabel(this);
    m_pShowColorLable->setGeometry(5, 5, 12, 12);
    m_pShowColorLable->setVisible(true);
    m_pShowColorLable->setAutoFillBackground(true);
    m_pShowColorLable->setPalette(QPalette(QColor("#FF0000")));

    connect(this, &BLColorCombox::signColorChanged , this, &BLColorCombox::slotBtnColorChange);
}

BLColorCombox::~BLColorCombox()
{
}

void BLColorCombox::setButtonIcon(const QString &imageFile, QColor color)
{
    setIcon(createColorToolButtonIcon(imageFile, color));
}

void BLColorCombox::setBtnColorChange(const QColor &color)
{
    m_pShowColorLable->setPalette(QPalette(color));
}

QMenu* BLColorCombox::createColorMenu(const char *slot, const char *slotColorBoard)
{
    // 设置透明色
    QAction *pActionTransparent = new QAction(this);
    pActionTransparent->setData(QColor(0, 0, 0, 0));
    pActionTransparent->setText(QObject::tr("None"));
    connect(pActionTransparent, SIGNAL(triggered()), this, slot);
    QToolButton *pBtnTransparent = new QToolButton;
    pBtnTransparent->setFixedSize(QSize(142, 16));
    pBtnTransparent->setText((QObject::tr("自定义1")));
    pBtnTransparent->setDefaultAction(pActionTransparent);

    // 选择其他颜色
    QToolButton *pBtnOtherColor = new QToolButton;
    pBtnOtherColor->setText((QObject::tr("自定义2")));
    pBtnOtherColor->setFixedSize(QSize(142, 20));
    pBtnOtherColor->setAutoRaise(true);
    pBtnOtherColor->setToolTip((QObject::tr("自定义3")));
    connect(pBtnOtherColor, SIGNAL(clicked()), this, slotColorBoard);

    // 基本色
    QGridLayout *pGridLayout = new QGridLayout;
    pGridLayout->setAlignment(Qt::AlignCenter);
    pGridLayout->setContentsMargins(0, 0, 0, 0);
    pGridLayout->setSpacing(2);

    for (int iRow = 0; iRow < 6; iRow++)
    {
        for (int iCol = 0; iCol < 8; iCol++)
        {
            QAction *action = new QAction(this);
            action->setData(colors[iRow][iCol]);
            action->setIcon(createColorIcon(colors[iRow][iCol]));
            connect(action, SIGNAL(triggered()), this, slot);

            QToolButton *pBtnColor = new QToolButton;
            pBtnColor->setFixedSize(QSize(16, 16));
            pBtnColor->setAutoRaise(true);
            pBtnColor->setDefaultAction(action);
            pBtnColor->setToolTip("white");

            pGridLayout->addWidget(pBtnColor, iRow, iCol);
        }
    }

    QWidget *widget = new QWidget;
    widget->setLayout(pGridLayout);

    QVBoxLayout *pVLayout = new QVBoxLayout;
    pVLayout->addWidget(pBtnTransparent);
    pVLayout->addWidget(widget);
    pVLayout->addWidget(pBtnOtherColor);

    QMenu *colorMenu = new QMenu(this);
    colorMenu->setLayout(pVLayout);

    return colorMenu;
}

QIcon BLColorCombox::createColorToolButtonIcon(const QString &imageFile, QColor color)
{
    QPixmap pixmap(16, 18);
    pixmap.fill(Qt::transparent);

    QPainter painter(&pixmap);
    QPixmap image(imageFile);

    QRect target(0, 0, 16, 16);
    QRect source(0, 0, 16, 16);
    painter.fillRect(QRect(0, 13, 16, 4), color);
    painter.drawPixmap(target, image, source);
    return QIcon(pixmap);

    //return QIcon("");
}

QIcon BLColorCombox::createColorIcon(QColor color)
{
    QPixmap pixmap(16, 16);
    QPainter painter(&pixmap);
    painter.setPen(Qt::NoPen);
    painter.fillRect(QRect(0, 0, 16, 16), color);

    return QIcon(pixmap);
}

void BLColorCombox::OnColorChanged()
{
    QAction *pFillColorAction = new QAction(this);
    pFillColorAction = qobject_cast<QAction *>(sender());
    QColor color = qvariant_cast<QColor>(pFillColorAction->data());

    this->menu()->close();
    emit signColorChanged(color);
}

void BLColorCombox::OnShowColorBoard()
{
    this->menu()->close();
    QColor color = QColorDialog::getColor(Qt::black, this);
    if (!color.isValid())
        return;

    emit signColorChanged(color);
}

void BLColorCombox::slotBtnColorChange(QColor color)
{
    m_pShowColorLable->setPalette(QPalette(color));
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值