zoukankan      html  css  js  c++  java
  • Qt 组合框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::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));
    
    ...
  • 相关阅读:
    联想IdeaPad品牌出炉 三款笔记本亮相
    [推荐]2008年必不可少的20个网络产品
    微软公开.NET Base Classes源代码
    [共享一下]Head.First.设计模式.中文版
    IT: 蓝牙十岁了
    祝贺“阿来之家”博客正式开通~
    NodeJS安全设计:好吃的草莓味糖果,只给好朋友小红
    NodeJS文件读取:感恩常在抓把糖果,愉悦客人
    NodeJS缓存机制:畅销货,就多囤一点呗
    安装pygame出现is not a supported wheel on this platform解决办法
  • 原文地址:https://www.cnblogs.com/chechen/p/12219644.html
Copyright © 2011-2022 走看看