Qt自定义委托

2023-11-12

Qt中的委托通常都是继承自QStyledItemDelegate或者QItemDelegate,二者的区别主要在于绘制方式,QStyledItemDelegate会使用当前样式绘制,并且能够使用qss,因此在在自定义委托时,一般使用 QStyledItemDelegate作为基类。除此之外,二者基本没有区别,写法和用法都一样。

继承 QStyledItemDelegate需要实现以下几个函数:

  • createEditor():returns the widget used to change data from the model and can be reimplemented to customize editing behavior.
  • setEditorData(): provides the widget with data to manipulate.
  •  updateEditorGeometry():ensures that the editor is displayed correctly with respect to the item view.
  • setModelData():returns updated data to the model.
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;

假设一个QTableView中,某一列的数据全是“选项一,选项二,选项三”中的任意一个选项,这种情况下就可以把这一列的代理设置成以QComboBox为基础的,每次修改数据时双击表格中的cell,出现下拉框来选择数据。详细代码如下:

nari_combodelegate.h

#ifndef NARI_COMBODELEGATE_H
#define NARI_COMBODELEGATE_H

class NARI_ComboDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    NARI_ComboDelegate(const QStringList &items, QObject *parent = NULL);
    ~NARI_ComboDelegate();

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor,	const QStyleOptionViewItem &option, const QModelIndex &index) const;

    QStringList getItemStr();

private:
    QStringList m_ls_itemtext;
};

#endif // NARI_COMBODELEGATE_H

nari_combodelegate.cpp

#include "nari_combodelegate.h"

NARI_ComboDelegate::NARI_ComboDelegate(const QStringList &items, QObject *parent) :
    QItemDelegate(parent)
{
    m_ls_itemtext = items;
}

NARI_ComboDelegate::~NARI_ComboDelegate(){ }

QWidget *NARI_ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *editor = new QComboBox(parent);
    editor->addItems(m_ls_itemtext);
    editor->setEditable(false);
    return editor;
}

void NARI_ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    int icurIndex = comboBox->findText(value);
    comboBox->setCurrentIndex(icurIndex);
}

void NARI_ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    model->setData(index, dynamic_cast<QComboBox*>(editor)->currentText(), Qt::EditRole);
}

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

QStringList NARI_ComboDelegate::getItemStr()
{
    return m_ls_itemtext;
}

mainwindow.cpp

// ...
ui->tableView->setItemDelegateForColumn(0, new NARI_ComboDelegate(QStringList() << "Item1" << "Item2" << "Item3"));
// ...

其他委托示例


限制输入大小

nari_doubledelegate.h

#ifndef NARI_DOUBLEDELEGATE_H
#define NARI_DOUBLEDELEGATE_H

class NARI_DoubleDelegate : public QItemDelegate
{
    Q_OBJECT
public :
    NARI_DoubleDelegate(double minval, double maxval, QObject *parent = NULL);
    ~NARI_DoubleDelegate();

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;

private:
    double m_minval, m_maxval;
};

#endif // NARI_DOUBLEDELEGATE_H

nari_doubledelegate.cpp

#include "nari_doubledelegate.h"

NARI_DoubleDelegate::NARI_DoubleDelegate(double minval, double maxval, QObject *parent)
    : QItemDelegate(parent)
{
    m_minval = minval;
    m_maxval = maxval;
}

NARI_DoubleDelegate::~NARI_DoubleDelegate(){  }

QWidget *NARI_DoubleDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
    editor->setToolTip(QString::number(m_minval) + " - " + QString::number(m_maxval));
    editor->setMinimum(m_minval);
    editor->setMaximum(m_maxval);
    return editor;
}

void NARI_DoubleDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    double value = index.model()->data(index, Qt::EditRole).toDouble();
    QDoubleSpinBox *spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
    spinBox->setValue(value);
}

void NARI_DoubleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QDoubleSpinBox *spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
    spinBox->interpretText();
    int value = spinBox->value();
    model->setData(index, value, Qt::EditRole);
}

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


限制输入IP地址

nari_Ipdelegate.h

#ifndef NARI_IPDELEGATE_H
#define NARI_IPDELEGATE_H

class NARI_IpDelegate : public QItemDelegate
{
public:
    NARI_IpDelegate(QObject *parent = NULL);
    ~NARI_IpDelegate();

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif // NARI_IPDELEGATE_H

nari_Ipdelegate.cpp

#include "nari_ipdelegate.h"


NARI_IpDelegate::NARI_IpDelegate(QObject *parent)
    : QItemDelegate(parent)
{

}

NARI_IpDelegate::~NARI_IpDelegate(){ }

QWidget *NARI_IpDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QLineEdit *editor = new QLineEdit(parent);
    editor->setValidator(new QRegExpValidator(QRegExp("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"), parent));
    editor->setInputMask("000.000.000.000;0");  //必须加;0否则前面的正则表达式失效,;0”表示删除时默认填充为0
    return editor;
}

void NARI_IpDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QLineEdit *le = dynamic_cast<QLineEdit*>(editor);
    le->setText(value);
}

void NARI_IpDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    model->setData(index, dynamic_cast<QLineEdit*>(editor)->text(), Qt::EditRole);
}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Qt自定义委托 的相关文章

  • 如何使用 qt 库中的调试符号为 qt 5.5 创建开发 shell

    我有一个开发外壳buildInputs条目包括qt55 qtbase 这很好用 今天 我在 qt 库中发生了段错误 我想要带有调试符号的 qt 库 我看了一下nixpkgs pkgs development libraries qt 5 5
  • 如何重写(重新实现)QFileSystemModel 中的成员函数

    我已经为此苦苦挣扎了一段时间 Qt s QFileSystemModel由于图标获取算法非常糟糕 在获取数百个文件时速度非常慢 我想完全禁用图标 它们被提取到QFileSystemModel data方法不是虚拟的 QFileSystemM
  • 在 Windows 上以 QML 播放 RTSP 视频

    我正在尝试将 QML 中的 RTSP 流播放到视频标签中 如下所示 Repeater model 8 Video Layout fillWidth true Layout fillHeight true fillMode VideoOutp
  • 运行最新版本时没有“最新”消息?

    我正在尝试使用Sparkle https sparkle project org与 Qt Go 的绑定 https github com therecipe qt app 闪光 m import
  • MapItemView 在 dataChanged 信号后不会更新

    我正在使用 QMLMapItemView使用 C 的组件QAbstractListModel基于模型 这MapItemView当模型重置时 或者每当添加新项目或删除现有项目时 工作正常 但是 那MapItemView不反映对已添加项目的更改
  • 仅当从 Qt 连接时网页返回 HTTP 406 错误

    我有一个测试页面设置http mlecturedownload com test qt php http mlecturedownload com test qt php有以下代码
  • 在 Windows 上静默安装 Qt55 Enterprise

    编辑 在 Qt 支持的帮助下 我已经解决了如何自动化 Qt 企业安装程序的这两个部分 下面是脚本调用 我正在尝试在 Windows 8 1 和 Windows 10 上静默安装 Qt 5 5 1 Enterprise 使用 script 开
  • 使用信号和槽更新指针

    我对 Qt 很陌生 请帮我解决这个问题 我正在使用线程在后台执行密集操作 同时我想更新 UI 所以我使用 SIGNALS 和 SLOTS 为了更新 UI 我发出一个信号并更新 UI 让我们考虑下面的示例代码 struct sample QS
  • 渲染具有透明度的纹理时,OpenGL 不需要的像素

    我已经为这个问题苦苦挣扎了一段时间了 当我使用 OpenGL 渲染 2D 纹理 在无透明度和部分透明度之间的过渡上具有透明度值 时 我得到了一些烦人的灰色像素 我认为这是像素值插值的产物 关于如何改进这一点有什么想法吗 I m attach
  • 在 Qt 中用像素图画笔画一条线?

    一段时间以来 我正在使用 Qt C 开发一个简单的绘图和绘画应用程序 目前我正在使用 QPainter drawLine 进行绘制 并且工作正常 我想做的是用像素图画笔绘图 这是我可以做到的 我可以使用 QPainterPath 和 QPa
  • PyQt QFileDialog exec_ 很慢

    我正在使用自定义QFileDialog因为我想选择多个目录 但是exec 功能非常慢 我不明白为什么 我正在使用最新版本的 PyQt 代码片段 from PyQt4 import QtGui QtCore QtNetwork uic cla
  • 在未安装 Qt VS Tools 的情况下以 Qt/MsBuild 格式编译 Qt 项目

    我在 Visual Studio 中有很多 Qt 项目 使用新的 Qt MsBuild 格式 https blog qt io blog 2018 02 16 qt visual studio improving performance 由
  • QGraphicsScene没有删除QWidget的功能

    QGraphicsScene 有一个addWidget QWidget 有函数 但是没有对应的removeWidget QWidget 它只有removeItem QGraphicsItem 如何删除 QWidget 这是一个基本示例 看看
  • 如何在 QT 安装程序框架中区分每用户安装与系统范围安装?

    我正在使用一些名为 pgModeler 的应用程序 它的当前版本提供了一个基于 QT 安装程序框架的安装程序 Windows 上该安装程序的问题是它安装每个用户的开始菜单条目 https github com pgmodeler pgmod
  • 加载 QPixmap 数据的更好方法

    更好的方法来做到这一点 没有QImage QImage image width height QImage Format RGB888 memcpy image bits m frameRGB gt data 0 height width
  • 在 Qt 5 中嵌入 Python

    我想将 Python 解释器嵌入到 Qt 5 应用程序中 我在 Qt 5 中有一个工作应用程序 但是当我把 include
  • 具有多个父项的 Qt 树模型

    我想构建一棵树 其中一个元素可以引用另一个元素 我想要构建的树是 像这样的东西 A B C D E F P this is a pointer to C D first child of C E second child of C I fo
  • SWI-Prolog 与 C++ 接口的问题

    我试图让 SWI Prolog 与 C 很好地配合 现在束手无策 现在 在我开始准确解释我的问题是什么之前 我想首先说明我的项目是关于什么的以及我选择了哪些工具来开发解决方案 我的教授分配给我的任务是开发一个 GUI 程序 作为 SWI p
  • 如何从浮点数组创建新的 QImage

    我有一个代表图像的浮点数数组 列在前 我想在 QGraphicsSecene 上将图像显示为 QPixmap 为了做到这一点 我尝试使用 QImage 构造函数 QImage const uchar data int width int h
  • 我应该使用 QCoreApplication::processEvents() 还是 QApplication::processEvents()?

    我有一个从两者调用的方法QThreads和主线程 这个方法有时可能需要很长时间才能在循环中进行计算 所以我把QCoreApplication processEvents 这可以防止 GUI 冻结 在某个时刻我已经改变了QCoreApplic

随机推荐

  • 远程桌面端口默认是什么?修改远程桌面端口号方法

    远程桌面连接是我们控制远程电脑的高效方法 远程桌面连接也是需要使用端口的 下面我们一起来学习一下远程桌面连接默认的端口号及服务器端远程端口号的修改方法 推荐 服务器远程桌面端口修改工具 远程桌面端口默认是什么 远程桌面连接的默认端口号是 3
  • C语言实现简易扫雷小游戏

    game h include
  • Linux下编译CEF源码及交叉编译

    Linux下编译CEF chromium源码及交叉编译 官方编译文档 https bitbucket org chromiumembedded cef wiki MasterBuildQuickStart markdown header l
  • 继承 c++

    1 类的继承概念的解释 2 函数隐藏 3 赋值兼容 4 多种继承方式 5 函数的使用 构造函数 析构函数 拷贝构造函数 赋值运算符重载函数 1 类的继承概念解释 假定有一个类A 要创建一个新类B 它是类A的一个特殊版本 类A就称为基类 类B
  • java前台请求quartz,spring整合java quartz实现动态定时任务的前台网页配置与管理

    实例简介 在实际项目应用中经常会用到定时任务 可以通过quartz和spring的简单配置即可完成 但如果要改变任务的执行时间 频率 废弃任务等就需要改变配置甚至代码需要重启服务器 这里介绍一下如何通过quartz与spring的组合实现动
  • 目标主机SSH服务存在RC4、CBC或None弱加密算法 修复方法

    近期进行服务器漏扫时发现了 目标主机SSH服务存在RC4 CBC或None弱加密算法 的漏洞 记录一下修复方法 如下 1 修改 ssh 配置文件 vim etc ssh sshd config 或 vi etc ssh sshd confi
  • java打印出1~100之内的所有素数

    素数 质数 是指在大于1的整数之中只能被1和它自身整除的数就称之为素数 例如 2 1 2 2 2 1 2就是一个素数 以此类推3 5 7都是素数 代码 public class Main public static void main St
  • 主板电源开关接口图解_组装电脑时主板跳线如何接?DIY装机主板接线教程

    如今装机不再像以前那么神秘 不用再去电脑城问东问西 只要上天猫或京东等网上商城即可放心买到各种电脑配件 那么 自己组装电脑最难的是什么 CPU 散热器 内存 显卡安装都很简单 很多小伙伴自己组装电脑的难点主要在于主板跳线或者说机箱接线 今天
  • 2022.06.26 华为od机试真题

    华为od机试真题 1 最长连续子串 2 正方形数量 3 二叉树层次遍历 不会做 1 最长连续子串 有N个正整数组成的一个序列 给定一个整数sum 求长度最长的的连续子序列使他们的和等于sum 返回次子序列的长度 如果没有满足要求的序列 返回
  • 2019年广东工业智能机器人产量约占全国29%

    日前发布的 广东省制造业高质量发展 十四五 规划 下称 规划 中 智能机器人是我省 十四五 谋划发展的十大战略性新兴产业之一 战略性新兴产业是科技创新和产业发展的深度融合 规划针对智能机器人的发展提出 重点发展机器人减速器 控制器等关键部件
  • 【计算机视觉

    文章目录 一 PROMISE12 二 BraTS 2015 三 LIP Look into Person 四 BigEarthNet 五 Stanford Background Standford Background Dataset 六
  • win10 powershell无法激活conda v4.9环境

    1 PATH环境变量 把condabin目录添加到环境变量中 2 初始化powershell 2 1 管理员身份运行powershell win x 弹出选项 选中 Windows PowerShell 管理员 2 2 conda init
  • python 替换_Python 实现将numpy中的nan和inf,nan替换成对应的均值

    nan not a number inf infinity 正无穷 numpy中的nan和inf都是float类型 t t 返回bool类型的数组 矩阵 np count nonzero 返回的是数组中的非0元素个数 true的个数 np
  • (python)Hex文件解析和校验

    目录 前言 Hex文件结构分析 1 利用notepad 打开hex文件 2 hex行格式 行开始 数据长度 地址 数据类型 数据 校验和 3 校验和 完整代码 总结 前言 Intel HEX文件是由一行行符合Intel HEX文件格式的文本
  • 数据赋能企服增长,构建“以客户为中心”的数字化经营体系

    目前 中国企服市场数字化需求爆发 主要表现在传统行业数字化转型加速 企服企业的服务能力得到认可 新冠疫情加速数字化进程等方面 神策数据作为大数据分析与营销科技优质服务商 结合自身数字化实践经验 总结出企服数据驱动增长的解决方案 从市场营销
  • mysql查询like多个值

    有个需求是要查询字段中code前缀是H M 81 82开头的 方法一 使用like和or select from zhy where code like H or code like M or code like 81 or code li
  • Java并发包中那些值得学习的并发工具类(空谈误国,实干兴邦,代码示范,抛砖引玉)

    首先我们通常说的并发包就是java util concurrent包及其子包 集中了Java并发的各种基础工具类 一 这个并发包在哪 上面的包就是传说中的并发包 为什么这个并发包就比较流弊呢 原因主要有以下几点 提供了几个比synchron
  • 什么叫临界资源和临界区?

    临界资源是指每次仅允许一个进程访问的资源 属于临界资源的硬件有打印机 磁带机等 软件有消息缓冲队列 变量 数组 缓冲区等 诸进程间应采取互斥方式 实现对这种资源的共享 每个进程中访问临界资源的那段代码称为临界区 显然 若能保证诸进程互斥地进
  • 机器学习源代码_机器学习中程序源代码的静态分析

    机器学习源代码 Machine learning has firmly entrenched in a variety of human fields from speech recognition to medical diagnosin
  • Qt自定义委托

    Qt中的委托通常都是继承自QStyledItemDelegate或者QItemDelegate 二者的区别主要在于绘制方式 QStyledItemDelegate会使用当前样式绘制 并且能够使用qss 因此在在自定义委托时 一般使用 QSt