borderboxQt组合框QComboBox定制颜⾊选择框在项⽬开发中需要定制Qt Combobox,每⼀项是显⽰⼀个RGB颜⾊供⽤户选择,实现的效果
QColorCombobox.h
#pragma once
#include <QLineEdit>
#include <QCombobox>
class QLabel;
class QListWidget;
class QColorWidget : public QLineEdit
{
Q_OBJECT
public:
QColorWidget(QWidget *parent = Q_NULLPTR);
~
QColorWidget();
void updateColor(const QColor& color);
void mousePressEvent(QMouseEvent *event);
signals:
void click(const QColor& color);
private:
QLabel* m_pLabel;
QLabel* m_pRgbLabel;
QColor m_color;
};
class QColorCombobox :public QComboBox
{
Q_OBJECT
public:
QColorCombobox(QWidget *parent = Q_NULLPTR);
~QColorCombobox();
void appendItem(const QColor& color);
private slots:
void onClickColorWidget(const QColor& color);
private:
QColorWidget* m_pLineEdit;
QListWidget* m_pListWidget;
};
QColorCombobox.cpp
#include "QColorCombobox.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QListWidget>
QColorWidget::QColorWidget(QWidget *parent /*= Q_NULLPTR*/)
:QLineEdit(parent)
,m_color(255,0,0)
{
m_pLabel = new QLabel(this);
m_pRgbLabel = new QLabel(this);
m_pLabel->setFixedSize(15, 15);
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(m_pLabel);
layout->addWidget(m_pRgbLabel);
layout->setContentsMargins(5, 0, 0, 2);
setLayout(layout);
setReadOnly(true);
setStyleSheet("QLineEdit{border: none;}QLineEdit:hover{background-color:rgb(0,125,255);}");
}
QColorWidget::~QColorWidget()
{
}
void QColorWidget::updateColor(const QColor& color)
{
m_color = color;
QString strstyle = QString("border:1px solid black;background-color:rgb(%1,%2,%3);").arg(QString::d()), QString::()), QString::number(color.blue())); QString strText = QString("%1,%2,%3").arg(QString::d()), QString::()), QString::number(color.blue()));
m_pLabel->setStyleSheet(strstyle);
m_pRgbLabel->setText(strText);
}
void QColorWidget::mousePressEvent(QMouseEvent *event)
{
emit click(m_color);
}
QColorCombobox::QColorCombobox(QWidget *parent /*= Q_NULLPTR*/)
:QComboBox(parent)
{
m_pLineEdit = new QColorWidget(this);
m_pListWidget = new QListWidget(this);
m_pLineEdit->setStyleSheet("");
setContextMenuPolicy(Qt::NoContextMenu);//禁⽤菜单
m_pListWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//禁⽤垂直滚动条
m_pListWidget->setStyleSheet("QListWidget::Item:hover{background-color:rgb(0,125,255);}");
setLineEdit(m_pLineEdit);
setModel(m_pListWidget->model());
setView(m_pListWidget);
}
QColorCombobox::~QColorCombobox()
{
}
void QColorCombobox::appendItem(const QColor& color)
{
QColorWidget* pWid = new QColorWidget(this);
pWid->updateColor(color);
connect(pWid, SIGNAL(click(const QColor&)),this,SLOT(onClickColorWidget(const QColor&)));
QListWidgetItem* pItem = new QListWidgetItem(m_pListWidget);
m_pListWidget->addItem(pItem);
m_pListWidget->setItemWidget(pItem, pWid);
}
void QColorCombobox::onClickColorWidget(const QColor& color)
{
m_pLineEdit->updateColor(color);
hidePopup();
}
MainWindow.cpp
....
uiboBox->appendItem(QColor(255, 0, 0));
uiboBox->appendItem(QColor(255, 255, 0));
uiboBox->appendItem(QColor(255, 0, 255));
uiboBox->appendItem(QColor(0, 255, 255));
uiboBox->appendItem(QColor(0, 255, 0));
...
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论