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

相关文章

java解析jwt中的payload的用法

《java解析jwt中的payload的用法》:本文主要介绍java解析jwt中的payload的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解析jwt中的payload1. 使用 jjwt 库步骤 1:添加依赖步骤 2:解析 JWT2. 使用 N

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

SQL BETWEEN 的常见用法小结

《SQLBETWEEN的常见用法小结》BETWEEN操作符是SQL中非常有用的工具,它允许你快速选取某个范围内的值,本文给大家介绍SQLBETWEEN的常见用法,感兴趣的朋友一起看看吧... 在SQL中,BETWEEN是一个操作符,用于选取介于两个值之间的数据。它包含这两个边界值。BETWEEN操作符常用

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

python3 gunicorn配置文件的用法解读

《python3gunicorn配置文件的用法解读》:本文主要介绍python3gunicorn配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python3 gunicorn配置文件配置文件服务启动、重启、关闭启动重启关闭总结python3 gun

MySQL 中的 LIMIT 语句及基本用法

《MySQL中的LIMIT语句及基本用法》LIMIT语句用于限制查询返回的行数,常用于分页查询或取部分数据,提高查询效率,:本文主要介绍MySQL中的LIMIT语句,需要的朋友可以参考下... 目录mysql 中的 LIMIT 语句1. LIMIT 语法2. LIMIT 基本用法(1) 获取前 N 行数据(

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分