在项目开发中需要定制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::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 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
.... 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)); ...