被调色板困惑很久了,查了网上资料也没有自己想要的,就决定写一个自定义调色板。
直接上代码。
头文件:
#pragma once
#include
#include
/*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
QColor color = qvariant_cast
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));
}
参考:https://blog.csdn.net/yyingwei/article/details/9667275