QTableView 委任用法QStyledItemDelegate

2023-12-17 20:58

本文主要是介绍QTableView 委任用法QStyledItemDelegate,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

The QStyledItemDelegate class provides display and editing facilities for data items from a model.

委任是提供展示编辑模型数据的一种工具,比如在表格中,你想让表格具有下拉框,spinbox等组件的功能。就可以用委任来实现。

The QStyledItemDelegate class is one of the Model/View Classes and is part of Qt's model/view framework. The delegate allows the display and editing of items to be developed independently from the model and view

委任类是模型/视图,以及模型试图框架的一部分,模型/试图允许委任独立的展示编辑以及开发。

实现委任的方法有两大类:来自于他自身的虚函数

virtual QWidget *

createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const

virtual void

paint(QPainter *painter, 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 QSize

sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

virtual void

updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

1:qt 自身没有自带的组件

If the delegate does not support painting of the data types you need or you want to customize the drawing of items, you need to subclass QStyledItemDelegate, and reimplement paint() and possibly sizeHint(). The paint() function is called individually for each item, and with sizeHint(), you can specify the hint for each of them

如果委任不支持对数据类型的绘画,那末你就需要自己去绘画这些项目。你需要实现paint()可能还有sizehint().

2: qt自身拥有的组件

It is possible for a custom delegate to provide editors without the use of an editor item factory. In this case, the following virtual functions must be reimplemented:

一个定制的委托可以在不使用编辑器项目工厂的情况下提供编辑器。在这种情况下,必须重新实现以下虚拟功能;就是这四种函数。

  • createEditor()
  • setEditorData()
  • updateEditorGeometry()
  • setModelData()

本章节主要讲解第二部分:先看图:


main.cpp

#include <QApplication>

#include <QHeaderView>
#include <QStandardItemModel>
#include <QTableView>
#include<QHBoxLayout>
#include"delegate.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    [1]
    QStandardItemModel *model=new QStandardItemModel;
    model->setColumnCount(2);
    model->setRowCount(4);
    [2]
    QTableView *view=new QTableView;
    view->verticalHeader()->hide();
    view->setAlternatingRowColors(true);
    view->setStyleSheet("QTableView{background-color: #4682B4;"
            "alternate-background-color: #D8BFD8;}");
    view->setModel(model);
    spinboxdelegate delegate;
    view->setItemDelegateForColumn(1,&delegate);//将表格的第一列进行委任。
    view->horizontalHeader()->setStretchLastSection(true);
/[3]
    QWidget *widget=new QWidget;
    QPalette pal=widget->window()->palette();
    pal.setColor(QPalette::Window, "#B0C4DE");
    widget->setFixedSize(400,500);
    QHBoxLayout *hb=new QHBoxLayout;
    hb->setContentsMargins(50,100,50,200);
    hb->addWidget(view);
    widget->setLayout(hb);
    widget->setPalette(pal);
    widget->show();
    return a.exec();
}
delegate.cpp

#include"delegate.h"

#include<QSpinBox>
#include<QComboBox>
spinboxdelegate::spinboxdelegate(QObject *parent):QStyledItemDelegate(parent)
{
}
QWidget *spinboxdelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *box=new  QComboBox(parent);
    box->addItem("男");
    box->addItem("女");
    box->setFrame(false);
    return box;
}
void spinboxdelegate::setEditorData(QWidget *editor, const QModelIndex &index)const
{
    QString  value=index.model()->data(index,Qt::EditRole).toString();
    QComboBox *box=static_cast<QComboBox*>(editor);
    box->setCurrentText(value);
}
void spinboxdelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const
{
    QComboBox *box=static_cast<QComboBox*>(editor);
          box->internalWinId();
          QString  value = box->currentText();
          model->setData(index, value, Qt::EditRole);
}
void spinboxdelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}
delegate.h

#ifndef DELEGATE_H

#define DELEGATE_H
#include<QObject>
#include<QStyledItemDelegate>
 
class spinboxdelegate:public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit spinboxdelegate(QObject *parent=0);
 
      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 // DELEGATE_H
 



这篇关于QTableView 委任用法QStyledItemDelegate的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/505821

相关文章

#error用法

/* *检查编译此源文件的编译器是不是C++编译器 *如果使用的是C语言编译器则执行#error命令 *如果使用的是 C++ 编译器则跳过#error命令 */ #ifndef __cplusplus #error 亲,您当前使用的不是C++编译器噢! #endif #include <stdio.h> int main() {

SQL Server中,isnull()函数以及null的用法

SQL Serve中的isnull()函数:          isnull(value1,value2)         1、value1与value2的数据类型必须一致。         2、如果value1的值不为null,结果返回value1。         3、如果value1为null,结果返回vaule2的值。vaule2是你设定的值。        如

tensorboard-----summary用法总结

Tensorflow学习笔记——Summary用法         最近在研究tensorflow自带的例程speech_command,顺便学习tensorflow的一些基本用法。 其中tensorboard 作为一款可视化神器,可以说是学习tensorflow时模型训练以及参数可视化的法宝。 而在训练过程中,主要用到了tf.summary()的各类方法,能够保存训练过程以及参数分布图并在

vscode-创建vue3项目-修改暗黑主题-常见错误-element插件标签-用法涉及问题

文章目录 1.vscode创建运行编译vue3项目2.添加项目资源3.添加element-plus元素4.修改为暗黑主题4.1.在main.js主文件中引入暗黑样式4.2.添加自定义样式文件4.3.html页面html标签添加样式 5.常见错误5.1.未使用变量5.2.关闭typescript检查5.3.调试器支持5.4.允许未到达代码和未定义代码 6.element常用标签6.1.下拉列表

YTKKeyValueStore用法

iOS端的尝试 后来我从后台转做iOS端的开发,我就尝试了在iOS端直接使用Key-Value式的存储。经过在粉笔网、猿题库、小猿搜题三个客户端中的尝试后,我发现Key-Value式的存储不但完全能够满足大多数移动端开发的需求,而且非常适合移动端采用。主要原因是:移动端存储的数据量不会很大: 如果是单机的应用(例如效率工具Clear),用户自己一个人创建的数据最多也就上万条。 如果

redis高级用法

redis 慢日志查询 配置参数 slowlog-log-slower-than 10000 #单位微秒 slowlog-max-len 选项指定服务器最多保存多少条慢查询日志 redis-cli slowlog get #获取慢日志1) 1) (integer) 4 # 日志的唯一标识符(uid)2) (integer) 1378781447 # 命令执

Log4j用法

日志是应用软件中不可缺少的部分,Apache的开源项目Log4j是一个功能强大的日志组件,提供方便的日志记录,具体请参考Log4j文档指南。 Log4j下载 在apache网站,可以免费下载到Log4j最新版本的软件包 Apache log4j  (推荐) Apache log4j 2 Log4j的包下载完成后,解压,将其中打包好的的log4j-1.x.x.jar导入你的工程

sql之top用法

TOP 子句 TOP 子句用于规定要返回的记录的数目。 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的。 注释: 并非所有的数据库系统都支持 TOP 子句。 SQL Server 的语法: SELECT TOP number|percent column_name(s)FROM table_name MySQL 和 Oracle 中的 SQL SELECT TOP 是等价的 M

Android自定义系列——9.Path详细用法

rXxx方法 rXxx方法的坐标使用的是相对位置(基于当前点的位移),而之前方法的坐标是绝对位置(基于当前坐标系的坐标)。 Path path = new Path();path.moveTo(100,100);path.lineTo(100,200);canvas.drawPath(path,mDeafultPaint); 在这个例子中,先移动点到坐标(100,100)处,之后再连接

JACKSON框架用法基本

JACKSON框架用法基本 之前参与的一个手机项目中和服务器进行交互的数据格式就是JSON,在手机端用JACKSON进行数据和java bean之间的转换。这次专业课的结课作业需要手机应用和服务器交互,我就想用JSON传递数据,用JACKSON来进行解析,特此总结一下。 简介 JACKSON是一个多用途的java库,来解析JSON格式的数据,用途很广。 解析JSON的三种方式 St