Qt实现自定义下拉框

实现如下所示的下拉框," + ", " - "一直显示在·最后两个。

利用代理实现

class TextWgt : public QWidget
{
    Q_OBJECT
public:
    explicit TextWgt (QWidget *parent = nullptr);

    //添加模型
    void addItem(const QString &text);


private:
    void init();


private:
    QListView*             m_pView;
    ItemDelegate*          m_pItemDelegate;              //代理
    QStandardItemModel*    m_pModel;
    QComboBox*             m_pCbx;
};

TextWgt::TextWgt(QWidget *parent)
    : QWidget(parent)
{
    init();
}

void TextWgt::addItem(const QString &text)
{
    QStandardItem * item = new QStandardItem(text);
    m_pModel->insertRow(m_pModel->rowCount() - 2, item);
    m_pCbx->setCurrentText(text);
}

void TextWgt::init()
{

    m_pCbx = new QComboBox(this);
    m_pModel = new QStandardItemModel(this);
    m_pCbx->setModel(m_pModel);
    m_pView = new QListView(this);
    m_pCbx->setView(m_pView);

    //代理
    m_pItemDelegate = new ItemDelegate;
    m_pView->setItemDelegate(m_pItemDelegate);

    //添加项---“+”
    QStandardItem * addItem = new QStandardItem();
    m_pModel->blockSignals(true);
    m_pModel->appendRow(addItem);
    m_pModel->blockSignals(false);

    //删除项---“-”
    QStandardItem * deleteItem = new QStandardItem();
    m_pModel->blockSignals(true);
    m_pModel->appendRow(deleteItem);
    m_pModel->blockSignals(false);

    QHBoxLayout * layout = new QHBoxLayout(this);
    layout->setContentsMargins(0,0,0,0);
    layout->addWidget(m_pCbx);
}

代理代码如下:

class ItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    ItemDelegate(QObject *parent = nullptr);

protected:
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

};
ItemDelegate::ItemDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}

void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.isValid())
    {
        //文本
        QString text = index.data(Qt::EditRole).toString();
        QRect rect = option.rect.adjusted(-1, 0, -1, 0);

        //最后一列才画 +
        if(index.row() == (index.model()->rowCount() - 1))
        {

            painter->save();
            painter->setPen(Qt::NoPen);
            painter->setBrush(QColor("#1A3C5F"));
            painter->drawRect(rect);
            painter->restore();

            if(option.state.testFlag(QStyle::State_MouseOver))
            {
                //绘制背景色
                painter->save();
                painter->setPen(Qt::NoPen);
                painter->setBrush(QColor("#3786DA"));
                painter->drawRect(rect);
                painter->restore();
            }

            painter->save();
            int x = option.rect.width() / 2;
            int y = option.rect.top() + option.rect.height() / 2;
            QPoint startX(x - 9, y);
            QPoint startY(x + 9, y);
            painter->drwLine(startX, startY);

            QPoint endX(x, y - 9);
            QPoint endY(x, y + 9);
            painter->drwLine(endX, endY);
            painter->restore();
        }
        //"-号"
        else if(index.row() == (index.model()->rowCount() - 2))
        {
            painter->save();
            painter->setPen(Qt::NoPen);
            painter->setBrush(QColor("#1A3C5F"));
            painter->drawRect(rect);
            painter->restore();

            if(option.state.testFlag(QStyle::State_MouseOver))
            {
                painter->save();
                painter->setPen(Qt::NoPen);
                painter->setBrush(QColor("#3786DA"));
                painter->drawRect(rect);
                painter->restore();
            }

            painter->save();
            painter->setPen(QPen(Qt::white, 2));
            int x = option.rect.width() / 2 - 9;
            int y = option.rect.top() + option.rect.height() / 2;
            QPoint start(x, y);
            QPoint end(x + 18, y);
            painter->drawLine(start, end);
            painter->restore();
        }
        else
        {
            //选中和未选中背景色不同
            painter->save();
            painter->setPen(Qt::NoPen);
            if (option.state.testFlag(QStyle::State_Selected))
                painter->setBrush(QColor("#3786DA"));
            else
                painter->setBrush(QColor("#1A3C5F"));
            painter->drawRect(rect);
            painter->restore();


            //选中和未选中字体颜色都是白色
            painter->save();
            painter->setPen(Qt::white);
            QFont font;
            font.setPixelSize(13);
            font.setFamily("Miscrosoft YaHei");
            painter->setFont(font);
            painter->drawText(rect, Qt::AlignLeft | Qt::AlignCenter, text);
            painter->restore();
        }
    }

//    QStyledItemDelegate::paint(painter, option, index);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值