Qt--Qcombox代理初探

前言:工程有时候会需要在Qcombox中显示字体加图片,并且点击图片进行删除或登录的一些操作。
目标:实现点击Qcombox区域中的图片区域,提示删除该行,点击删除按钮删除

.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public:
    void init();
private:
    Ui::MainWindow *ui;
 private  slots:
    void deleteItem(const QModelIndex &index);
};

#endif // MAINWINDOW_H




#ifndef ITEMDELEGATETEST_H
#define ITEMDELEGATETEST_H

#include <QObject>
#include <QStyledItemDelegate>

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

    ~ItemdelegateTest(){}
    void paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const;
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);

signals:
    void deleteItem(const QModelIndex &index);

public slots:
};

#endif // ITEMDELEGATETEST_H

.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "itemdelegatetest.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    ui->comboBox->setStyleSheet("QComboBox{border:2px solid gray;}"
                                              "QComboBox QAbstractItemView::item{height:30px;}" //下拉选项高度
     );

    //ui->comboBox->setEditable(true);
    QStringList ls;
    ls<<"A"<<"B"<<"C";
    ui->comboBox->addItems(ls);
    ItemdelegateTest *pDelegate = new ItemdelegateTest(this);
    ui->comboBox->setItemDelegate(pDelegate);

    connect(pDelegate, SIGNAL(deleteItem(QModelIndex)), this, SLOT(deleteItem(QModelIndex)));

}

void MainWindow::deleteItem(const QModelIndex &index)
{
    if (QMessageBox::information(NULL, "Title", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
    {
        ui->comboBox->removeItem(index.row());
    }
}



#include "itemdelegatetest.h"
#include <QStyledItemDelegate>
#include <QPainter>
#include <QMouseEvent>
#include <QEvent>
#include <QApplication>
#include <QToolTip>
#include "mainwindow.h"
#include <QPixmap>
#include <QtGui>

ItemdelegateTest::ItemdelegateTest(QObject *parent) : QStyledItemDelegate(parent)
{

}

void ItemdelegateTest::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()) / 2;
    QPixmap pixmap = QPixmap(":/check.png");
    //QPixmap pixmap = QPixmap("E:/qt/QtDelegateTestT/zhj.png");
    QRect DrawRect = QRect(viewOption.rect.left() + viewOption.rect.width() - 30, viewOption.rect.top() + height, 9, 9);
    painter->drawPixmap(DrawRect, pixmap);
}

bool ItemdelegateTest::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    int height = (option.rect.height()) / 2;
    QRect DrawRect = QRect(option.rect.left() + option.rect.width() - 30, option.rect.top() + height, 9, 9);

    //QMouseEvent *mouseEvent = static_cast(event);
    QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
    if (event->type() == QEvent::MouseButtonPress && DrawRect.contains(mouseEvent->pos()))
    {
       emit deleteItem(index);
    }

    if (event->type() == QEvent::MouseMove && DrawRect.contains(mouseEvent->pos()))
    {
        QCursor cursor(Qt::PointingHandCursor);
        QApplication::setOverrideCursor(cursor);
        QToolTip::showText(mouseEvent->globalPos(), "删除");
    }
    else
    {
        QCursor cursor(Qt::ArrowCursor);
        QApplication::setOverrideCursor(cursor);
    }

    return QStyledItemDelegate::editorEvent(event, model, option, index);
}

效果:

这里写图片描述

这里写图片描述

这里写图片描述

备注:新添加的图片资源有可能显示不出来,需要重新清理后Qmake一下

猜你喜欢

转载自blog.csdn.net/yifanmoon/article/details/80492396
今日推荐