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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

oracle中exists和not exists用法举例详解

《oracle中exists和notexists用法举例详解》:本文主要介绍oracle中exists和notexists用法的相关资料,EXISTS用于检测子查询是否返回任何行,而NOTE... 目录基本概念:举例语法pub_name总结 exists (sql 返回结果集为真)not exists (s

Springboot中Jackson用法详解

《Springboot中Jackson用法详解》Springboot自带默认json解析Jackson,可以在不引入其他json解析包情况下,解析json字段,下面我们就来聊聊Springboot中J... 目录前言Jackson用法将对象解析为json字符串将json解析为对象将json文件转换为json

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa

UVM:callback机制的意义和用法

1. 作用         Callback机制在UVM验证平台,最大用处就是为了提高验证平台的可重用性。在不创建复杂的OOP层次结构前提下,针对组件中的某些行为,在其之前后之后,内置一些函数,增加或者修改UVM组件的操作,增加新的功能,从而实现一个环境多个用例。此外还可以通过Callback机制构建异常的测试用例。 2. 使用步骤         (1)在UVM组件中内嵌callback函

这些ES6用法你都会吗?

一 关于取值 取值在程序中非常常见,比如从对象obj中取值 const obj = {a:1b:2c:3d:4} 吐槽: const a = obj.a;const b = obj.b;const c = obj.c;//或者const f = obj.a + obj.b;const g = obj.c + obj.d; 改进:用ES6解构赋值