Qt颜色下拉框菜单
头文件:
#pragma once
#include <QMenu>
#include <QWidgetAction>
class ColorMenu : public QMenu
{
Q_OBJECT
public:
ColorMenu(QWidget *parent = Q_NULLPTR);
void init();
QIcon ColorToolButtonIcon(QColor color);
Q_SIGNALS:
void colorSelected(QColor);
public Q_SLOTS:
void OnColorSelected();
private:
QWidgetAction *m_colorAction;
QWidget *m_colorWidget;
};
CPP:
#include "ColorMenu.h"
#include <QToolButton>
#include <QGridLayout>
#include <QPainter>
const QColor colors[6][8] =
{
{ QColor(0, 0, 0, 255), QColor(170, 0, 0, 255), QColor(0, 85, 0, 255), QColor(170, 85, 0, 255),
QColor(0, 170, 0, 255), QColor(170, 170, 0, 255), QColor(0, 255, 0, 255), QColor(170, 250, 0, 255) },
{ QColor(0, 0, 127, 255), QColor(170, 0, 127, 255), QColor(0, 85, 127, 255), QColor(170, 85, 127, 255),
QColor(0, 170, 127, 255), QColor(170, 170, 127, 255), QColor(0, 255, 127, 255), QColor(170, 255, 127, 255) },
{ QColor(0, 0, 255, 255), QColor(170, 0, 255, 255), QColor(0, 85, 255, 255), QColor(170, 85, 255, 255),
QColor(0, 170, 255, 255), QColor(170, 170, 255, 255), QColor(0, 255, 255, 255), QColor(170, 255, 255, 255) },
{ QColor(85, 0, 0, 255), QColor(255, 0, 0, 255), QColor(85, 85, 0, 255), QColor(255, 85, 0, 255),
QColor(85, 170, 0, 255), QColor(255, 170, 0, 255), QColor(85, 255, 0, 255), QColor(255, 255, 0, 255) },
{ QColor(85, 0, 127, 255), QColor(255, 0, 127, 255), QColor(85, 85, 127, 255), QColor(255, 85, 127, 255),
QColor(85, 170, 127, 255), QColor(255, 170, 127, 255), QColor(85, 255, 127, 255), QColor(255, 255, 127, 255) },
{ QColor(85, 0, 255, 255), QColor(255, 0, 255, 255), QColor(85, 85, 255, 255), QColor(255, 85, 255, 255),
QColor(85, 170, 255, 255), QColor(255, 170, 255, 255), QColor(85, 255, 255, 255), QColor(255, 255, 255, 255) }
};
ColorMenu::ColorMenu(QWidget *parent)
: QMenu(parent)
{
init();
}
void ColorMenu::init()
{
m_colorAction = new QWidgetAction(this);
m_colorWidget = new QWidget(this);
QGridLayout *gLayout = new QGridLayout(m_colorWidget);
gLayout->setMargin(0);
gLayout->setSpacing(2);
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 8; j++)
{
QPixmap pix(20, 20);
pix.fill(colors[i][j]);
QToolButton *btn = new QToolButton(m_colorWidget);
btn->setFixedSize(20, 20);
QAction *act = new QAction(QIcon(pix), QString(), btn);
act->setData(QVariant(colors[i][j]));
connect(act, &QAction::triggered, this, &ColorMenu::OnColorSelected);
btn->setDefaultAction(act);
gLayout->addWidget(btn, i, j);
}
}
m_colorAction->setDefaultWidget(m_colorWidget);
addAction(m_colorAction);
addSeparator();
QPixmap pix(20, 20);
pix.fill(QColor(Qt::transparent));
addAction(QIcon(pix), "Transparent", this, &ColorMenu::OnColorSelected)->setData(QColor(Qt::transparent));
}
void ColorMenu::OnColorSelected()
{
QAction *act = qobject_cast<QAction *>(sender());
if (act)
{
QColor color = qvariant_cast<QColor>(act->data());
emit colorSelected(color);
}
}
QIcon ColorMenu::ColorToolButtonIcon(QColor color)
{
QPixmap pixmap(18, 18);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
QFont font;
font.setBold(true);
font.setFamily("Microsoft YaHei");
font.setPointSize(12);
painter.setFont(font);
QRect target(0, 0, 18, 13);
painter.drawText(target, "A", QTextOption(Qt::AlignCenter));
painter.fillRect(QRect(0, 14, 18, 4), color);
return QIcon(pixmap);
}