首页 > 其他分享 >QCombobox应用QStyledItemDelegate小Demo

QCombobox应用QStyledItemDelegate小Demo

时间:2022-10-31 21:04:04浏览次数:41  
标签:index Widget QStyledItemDelegate const Demo QCombobox ui ItemDelegate include


效果是这样的:

QCombobox应用QStyledItemDelegate小Demo_#define

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());
}
}

标签:index,Widget,QStyledItemDelegate,const,Demo,QCombobox,ui,ItemDelegate,include
From: https://blog.51cto.com/u_15854865/5811264

相关文章

  • QCamera同时打开多个USB摄像头小Demo
    先看一下效果:(最大传2M图片,所以我把图片缩小并降低了DPI) 注意:一定不要把usb摄像头接到同一个USB集线器上,我这里是一个接到电脑外置接口,一个接到PCI-usb接口上了。如果要接更......
  • Jenkins Pipeline 流水线-Demo
    JenkinsPipeline流水线流水线既能作为任务的本身,也能作为Jenkinsfile使用流水线可以让我们的任务从UI手动操作,转换为代码化,像dockerfile一样。从shell命令到配置文......
  • istio部署demoapp应用 (十四)sidecar
    创建client~#kubectlrunclient--image=ikubernetes/admin-box-it--rm--restart=Never--command--/bin/shIfyoudon'tseeacommandprompt,trypressingen......
  • grpc demo python客户端 c++服务端
    项目需啊将网站上传的图片传入c++推理引擎,网站使用flask架构,python编写,图片推理引擎是一个单独的server,c++编写,因此用grpc来传输比较合适。理论上来说只要规定好proto文件,......
  • Demo1
    publicclassHelloWorld{//public目前是起限制作用,限制文件名和类名保持一致,否则不会运行。//class:定义的一个类.后面定义该类的名称类就如一栋大楼。......
  • 今天重新复习了一下cloud-demo
    主要有这些知识点:nacos,feign,gataway。这三个都是涉及配置的知识点比较多。比如说对于每个服务,要配置它的NACOS注册中心地址,如果某个服务需要使用NACOS配置中心的动态......
  • Demo52_关于封装
    //关于封装packagecom.oop.demo2;//private:私有属性,只能通过方法赋值publicclassFZ_4{//属性privateStringname;privateintage;......
  • Demo51_关于构造器有参构造的使用
    //构造器:有参构造的使用packagecom.oop.demo2;publicclassperson_3_2{Stringname;intage;publicperson_3_2(){}publicperson_3_2(Stringna......
  • Demo51_关于构造器_偏复杂
    //关于构造器packagecom.oop.demo2;//空的类中有默认的方法,默认的构造器//一个类即使什么都不写,它也会存在一个方法(构造器)publicclassPerson_3{//1.使用new关键......
  • 华为ME60/Eudemon8000E-X8镜像配置
    应用场景描述:        把核心交换机(BAS)与出口网关之间的所有流量全部镜像给安全态势感知设备,对所有进出的数据进行分析。思路:1、设置solt板卡观察序号2、配置镜像端......