QCombobox应用QStyledItemDelegate小Demo

效果是这样的:


QComboBox是个很基础的控件,也是继承自QWidget。

①我们先建立代理类,继承自QStyledItemDelegate:

1、头文件:

#ifndef ITEMDELEGATE_H
#define ITEMDELEGATE_H

#include <QStyledItemDelegate>
class ItemDelegate : public QStyledItemDelegate
{
  Q_OBJECT
signals:
    void deleteItem(const QModelIndex &index);
public:
    ItemDelegate(QObject * parent=0);
    virtual ~ItemDelegate(){}
    void paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const;
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};

#endif // ITEMDELEGATE_H

2、源文件:

#include "ItemDelegate.h"
#include <QPainter>
#include <QMouseEvent>
#include <QStyledItemDelegate>
#include <QToolTip>
#include <QApplication>
ItemDelegate::ItemDelegate(QObject * parent)
    : QStyledItemDelegate(parent)
{
}

void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem  viewOption(option);
    if (viewOption.state & QStyle::State_HasFocus)
    {
        viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
    }
    QStyledItemDelegate::paint(painter, viewOption, index);
    int height = (viewOption.rect.height() - 9) / 2;
    QPixmap pixmap = QPixmap(":/delete");
    QRect decorationRect = QRect(viewOption.rect.left() + viewOption.rect.width() - 30, viewOption.rect.top() + height, 9, 9);
    painter->drawPixmap(decorationRect, pixmap);
}

bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    int height = (option.rect.height() - 9) / 2;
    QRect decorationRect = QRect(option.rect.left() + option.rect.width() - 30, option.rect.top() + height, 9, 9);
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    if (event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
    {
        emit deleteItem(index);
    }
    if (event->type() == QEvent::MouseMove && decorationRect.contains(mouseEvent->pos()))
    {
        QCursor cursor(Qt::PointingHandCursor);
        QApplication::setOverrideCursor(cursor);
        QString strText = QStringLiteral("删除账号信息");
        QToolTip::showText(mouseEvent->globalPos(), strText);
    }
    else
    {
        QCursor cursor(Qt::ArrowCursor);
        QApplication::setOverrideCursor(cursor);
    }
    return QStyledItemDelegate::editorEvent(event, model, option, index);
}

②在我们的程序中应用这个代理:

1、头文件:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private slots:
    void deleteItem(const QModelIndex &index);
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

2、源文件:

#include "widget.h"
#include "ui_Widget.h"
#include "ItemDelegate.h"
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ItemDelegate *pDelegate = new ItemDelegate(this);
    ui->comboBox->addItem(QPixmap(":/shasha.png"), QStringLiteral("沙师弟"));
    ui->comboBox->addItem(QPixmap(":/jiejie.png"), QStringLiteral("二师兄"));
    ui->comboBox->addItem(QPixmap(":/kongkong.png"), QStringLiteral("猴哥"));
    ui->comboBox->addItem(QPixmap(":/tangtang.png"), QStringLiteral("师傅"));
    ui->comboBox->setItemDelegate(pDelegate);
    connect(pDelegate, SIGNAL(deleteItem(QModelIndex)), this, SLOT(deleteItem(QModelIndex)));
}
Widget::~Widget()
{
    delete ui;
}
void Widget::deleteItem(const QModelIndex &index)
{
    if (QMessageBox::question(this, QStringLiteral("提示"), QStringLiteral("确认要删除所选账号吗?"),
                     QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
    {
        ui->comboBox->removeItem(index.row());
    }
}

©️2020 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值