首页 > 其他分享 >Qt中QStyledItemDelegate的使用(二)

Qt中QStyledItemDelegate的使用(二)

时间:2023-12-03 11:44:06浏览次数:29  
标签:index const Qt QWidget 使用 model QStyledItemDelegate painter QModelIndex

延续上一篇文章。本文给出了一个QStyledItemDelegate类自定义绘制加自定义编辑框的例子。为方便读者理清思路,我已经尽量简化本文附加的代码了。此程序模拟用户给出星级评价的效果,在编辑状态下用户可以设置0~5个星星的评价,在普通状态下界面显示对应数量的金黄色星星。本文代码在VS2017和Qt5.9环境下测试通过。程序运行界面如下:

头文件。Qt也建议在自绘时同时重写sizeHint()函数给出一个合适的显示空间大小,这里为了简化就不重写了,影响不大:

class MStarDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    explicit MStarDelegate(QObject *parent = 0);
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};

CPP文件。在paint(...)函数中首先生成五角星的五个顶点坐标,然后在for循环中从左到右以一定间隔绘制指定数量的星星:

MStarDelegate::MStarDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

void MStarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QPointF vertex[5]; /* 五角星 Size=20,20 */
    for (int i = 0; i < 5; i++)
    {
        vertex[i].setX(+10 * sinf(i * 144.0f * (3.1415926535f / 180.0f)));
        vertex[i].setY(-10 * cosf(i * 144.0f * (3.1415926535f / 180.0f)));
    }

    int many = index.data(Qt::DisplayRole).toInt();
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(255, 170, 0));
    QSize iconSize = option.decorationSize;
    QRect thisRect = option.rect;
    int y = thisRect.y() + thisRect.height() / 2;
    for (int i = 0; i < many; i++)
    {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        qreal left = 2 + iconSize.width() * 0.5 + (2 + iconSize.width()) * i;
        painter->translate(left, y);
        painter->scale(iconSize.width() / 20.0, iconSize.height() / 20.0);
        painter->drawPolygon(vertex, 5, Qt::WindingFill);
        painter->restore();
    }
}

QWidget *MStarDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSpinBox* cbBox = new QSpinBox(parent);
    cbBox->setRange(0, 5);
    cbBox->setSuffix(u8"星");
    return cbBox;
}

void MStarDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QSpinBox* cbWidget = dynamic_cast<QSpinBox*>(editor);
    cbWidget->setValue(index.data(Qt::DisplayRole).toInt());
}

void MStarDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    int level = dynamic_cast<QSpinBox*>(editor)->value();
    model->setData(index, level, Qt::DisplayRole);
}

void MStarDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

主窗口的构造函数代码如下。其中QtTest是主窗口类,ui.tvHost是QTableView控件。

QtTest::QtTest(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    ui.tvHost->setItemDelegateForColumn(0, new MStarDelegate(ui.tvHost));
    QStandardItemModel* model = new QStandardItemModel(ui.tvHost);
    model->setHorizontalHeaderLabels({ u8"状态", u8"说明" });
    model->setVerticalHeaderLabels({ u8"设备1", u8"设备2", u8"设备3", u8"设备4" });
    model->setColumnCount(2);
    model->setRowCount(4);
    for (int i = 0; i < 4; i++)
    {
        model->setItem(i, 0, new QStandardItem(u8"3"));
        model->setItem(i, 1, new QStandardItem(u8"示例文字"));
    }
    ui.tvHost->setModel(model);
}

 

标签:index,const,Qt,QWidget,使用,model,QStyledItemDelegate,painter,QModelIndex
From: https://www.cnblogs.com/mengxiangdu/p/17831787.html

相关文章

  • Windows驱动中数字签名认证(使用 ci.dll)
    1.背景  对于常规应用程序来说,在应用层可以使用WinVerifyTrust,在驱动层使用常规的API无法使用,自己分析数据又太麻烦。  但在内核中ci.dll包装了数据签名验证相关的功能,我们可以使用该dll来实现我们的数字签名验证。  详细的分析见《内核中的代码完整性:深入分析ci......
  • 前端学习笔记202310学习笔记第一百壹拾四天-webpack配置和使用技巧9
    ......
  • 前端学习笔记202310学习笔记第一百壹拾四天-webpack配置和使用技巧7
    ......
  • AI作画使用指南
    AI作画API使用文档请前往:https://blog.csdn.net/qq_48896417/article/details/132438967?spm=1001.2014.3001.5501一、定义AI作画使用指南二、形式「公式」=图片主体,细节词,修饰词细节词可以任意组合,修饰词可以限定一种风格,也可以限定多种风格,遵循的基本原则是符合正常的中文......
  • 图数据库Neo4j概念、应用场景、安装及CQL的使用
    一、图数据库概念引用SethGodin的说法,企业需要摒弃仅仅收集数据点的做法,开始着手建立数据之间的关联关系。数据点之间的关系甚至比单个点本身更为重要。传统的**关系数据库管理系统(RDBMS)**并不擅长处理数据之间的关系,那些表状数据模式和呆板的结构难以添加新连接或不同类型连接......
  • sap.suite.ui.generic.template.ListReport.extensionAPI.ExtensionAPI 的使用场合介
    首先让我们了解一下什么是sap.suite.ui.generic.template.ListReport.extensionAPI.ExtensionAPI。这是一个在SAPFioriElements中用于扩展ListReport应用的API。SAPFioriElements旨在提供一种简洁,高效且一致的用户体验,而不需要开发人员编写大量的前端代码。然而,有些情......
  • Jupyter Notebook的使用
    什么是Jupyter NotebookJupyterNotebook是一个基于Web的交互式计算环境,支持多种编程语言,包括Python、R、Julia等。它的主要功能是将代码、文本、数学方程式、可视化和其他相关元素组合在一起,创建一个动态文档,用于数据分析、机器学习、科学计算和数据可视化等方面。JupyterN......
  • 使用JookDB将Oracle数据库迁移到Mysql
    JookDB是多平台的数据库开发管理工具,如Sql输入提示、导入导出、表设计、数据编辑等功能强大,而且是C++开发的界面非常丝滑流畅。可以免费支持Oracle、Mysql、SqlServer数据库管理。要通过JookDB将Oracle数据库迁移到Mysql需要先添加连接到这两个数据库的数据源。选择菜单【文件/新建......
  • 你真的了解HashSet 和HashMap的区别、优缺点、使用场景吗?
     HashSet和HashMap是Java集合框架中的两个常用类,它们都用于存储和管理数据,但在使用方式、功能和性能上有很大的区别。HashSet和HashMap的区别区别一:用途不同HashSet: HashSet是一个基于哈希表的集合,用于存储不重复的元素,它不存储键值对。它实际上是基于HashMap......
  • SMTP操作使用详解并通过python进行smtp邮件发送示例
    转载请注明出处:1.SMTP     SMTP 的全称是“SimpleMailTransferProtocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP协议属于TCP/IP协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP服......