Qt combo box QComboBox custom color selection box

In project development need to customize Qt Combobox, each item is displayed for the user to select an RGB color to achieve the effect of

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("border: none");
}


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::number(color.red()), QString::number(color.green()), QString::number(color.blue()));
    QString strText = QString("%1,%2,%3").arg(QString::number(color.red()), QString::number(color.green()), QString::number(color.blue()));
    m_pLabel->setStyleSheet(strstyle);
    m_pRgbLabel->setText(strText);
}

void QColorWidget::mousePressEvent(QMouseEvent *event)
{ 
    EMIT the Click (m_color); 
} 




QColorCombobox :: QColorCombobox (the QWidget * parent / * = Q_NULLPTR * / ) 
    : a QComboBox (parent) 
{ 
    m_pLineEdit = new new QColorWidget ( the this ); 
    m_pListWidget = new new QListWidget ( the this ); 
    setContextMenuPolicy (the Qt :: NoContextMenu); // disable menu 
    m_pListWidget-> setVerticalScrollBarPolicy (the Qt :: ScrollBarAlwaysOff); // disable the vertical scroll bar 
    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

....

    ui.comboBox->appendItem(QColor(255, 0, 0));
    ui.comboBox->appendItem(QColor(255, 255, 0));
    ui.comboBox->appendItem(QColor(255, 0, 255));
    ui.comboBox->appendItem(QColor(0, 255, 255));
    ui.comboBox->appendItem(QColor(0, 255, 0));

...

 

Guess you like

Origin www.cnblogs.com/chechen/p/12219644.html