Qt QTableView

article/2025/10/23 10:02:39

TableView

表格视图控件QTableView,需要和QStandardItemModel, 配套使用,这套框架是基于MVC设计模式设计的,M(Model)是QStandardItemModel数据模型,

不能单独显示出来。V(view)是指QTableView视图,要来显示数据模型,C(controllor)控制在Qt中被弱化,与View合并到一起。

文章最后为大家准备了Qt资料
↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡↡

1 QTableView简单应用

QStandardItmeModel表格的数据模型,那么这个模型需要填上每一行每一列的数据,就像execl表格一样。

widget.h

#ifndef WIDGET_H
#define WIDGET_H​#include <QTableView>
class Widget : public QTableView //继承至QTableView
{Q_OBJECT
public:Widget(QWidget *parent = 0);~Widget();
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QStandardItemModel>
#include <QDebug>
Widget::Widget(QWidget *parent): QTableView(parent)
{QStandardItemModel* model = new QStandardItemModel(this);model->setItem(0, 0, new QStandardItem("张三"));model->setItem(0, 1, new QStandardItem("3"));model->setItem(0, 2, new QStandardItem("男"));this->setModel(model);
}Widget::~Widget()
{}

以上代码实现了在model中添加一条数据,然后通过setModel函数设置view的数据模型为model,显示出来,如图:

2 修改行列字段名

修改字段名可以使用QStandardItemModel::setHeaderData,但是在这之前你需要调用QStandardItemModel::setColumnCount和QStandardItemModel::setRowCount,例如:

Widget::Widget(QWidget *parent): QTableView(parent)
{QStandardItemModel* model = new QStandardItemModel(this);/*设置列字段名*/model->setColumnCount(3);model->setHeaderData(0,Qt::Horizontal, "姓名");model->setHeaderData(1,Qt::Horizontal, "年龄");model->setHeaderData(2,Qt::Horizontal, "性别");/*设置行字段名*/model->setRowCount(3);model->setHeaderData(0,Qt::Vertical, "记录一");model->setHeaderData(1,Qt::Vertical, "记录二");model->setHeaderData(2,Qt::Vertical, "记录三");/*设置一条数据*/model->setItem(0, 0, new QStandardItem("张三"));model->setItem(0, 1, new QStandardItem("3"));model->setItem(0, 2, new QStandardItem("男"));this->setModel(model);
}

把1中的构造函数换为以上代码,运行即可得出下图:

3 移除数据

移除数据的常用函数有:

/*移除某行数据*/
bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex()) 
/*移除某列数据*/
bool QAbstractItemModel::removeColumn(int column, const QModelIndex &parent = QModelIndex())

例如:

model->removeRow(0);//移除第0行数据
model->removeColumn(0);//移除第0列数据

4 插入数据

插入一行数据:

void QStandardItemModel::insertRow(int row, const QList<QStandardItem *> &items)
/*
* row 表示从第几行插入数据
* items 表示要插入的数据QStandardItem对象
*/

例如:

QList<QStandardItem*> list;
list << new QStandardItem("王五") << new QStandardItem("22") << new QStandardItem("男");
model->insertRow(0, list); //在第0行插入一条记录

5 数据变更信号处理

当QStandardModel中的数据被更改的时候,会发射出dataChanged信号:

[signal] void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ());/*
*topLeft bottomRight这两索引指的是表格中被更改数据的区域,如果只有一个数据被更改,那么topLeft等于bottomRight
*/

例如:

widget.h

#ifndef WIDGET_H
#define WIDGET_H​
#include <QTableView>
class QStandardItemModel;
class Widget : public QTableView
{Q_OBJECT
public slots:void dataChangedSlot(const QModelIndex &topLeft, const QModelIndex &bottomRight,const QVector<int> &roles = QVector<int> ());
public:Widget(QWidget *parent = 0);QStandardItemModel* model;~Widget();
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QStandardItemModel>
#include <QDebug>
Widget::Widget(QWidget *parent): QTableView(parent)
{model = new QStandardItemModel(this);model->setColumnCount(3);model->setHeaderData(0,Qt::Horizontal, "姓名");model->setHeaderData(1,Qt::Horizontal, "年龄");model->setHeaderData(2,Qt::Horizontal, "性别");model->setRowCount(3);model->setHeaderData(0,Qt::Vertical, "记录一");model->setHeaderData(1,Qt::Vertical, "记录二");model->setHeaderData(2,Qt::Vertical, "记录三");model->setItem(0, 0, new QStandardItem("张三"));model->setItem(0, 1, new QStandardItem("3"));model->setItem(0, 2, new QStandardItem("男"));connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this, SLOT(dataChangedSlot(QModelIndex,QModelIndex,QVector<int>)));this->setModel(model);
}
void Widget::dataChangedSlot(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{qDebug() << model->data(topLeft).toString() << endl;
}
Widget::~Widget()
{
}

需要知道的是函数data可以获取想要的QStandardItem对象的索引:

[pure virtual] QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const

返回的QVariant对象是一种泛型变量,可以转换成QString、int、double等数据类型。

6 常用函数

 //默认显示行头,如果你觉得不美观的话,我们可以将隐藏        
tableview->verticalHeader()->hide();      //设置选中时为整行选中        
tableview->setSelectionBehavior(QAbstractItemView::SelectRows);         //设置表格的单元为只读属性,即不能编辑        
tableview->setEditTriggers(QAbstractItemView::NoEditTriggers);  //返回一个被选中的所有Item的索引,一般是去遍历这个链表进行处理
[virtual protected] QModelIndexList QTableView::selectedIndexes() const

7 QStandardItem被点选信号

当QStandardItemModel中的某个QStandardItem被点选后,QStandardItemModel对象会发出一个信号:

void QAbstractItemView::clicked(const QModelIndex &index);
/*
*返回被点选的Item的索引
*/

8 QItemDelegate代理

QTableView在处理信息显示编辑的时候比较单调,类似行编辑器,为了获得更多的灵性性,交互通过QItemDelegate执行。

​ 下面通过派生一个SpinDelegate来实现一个整数旋转框的代理器。

​ 一般我们要重写函数createEditor:

[virtual] QWidget *QItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
/*
*QWidget *parent一般是指哪个窗口使用了这个代理,一般用来托管内存
* QStyleOptionViewItem &option 样式风格
* const QModelIndex &index 需要更改的Item索引
*/

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include <QTableView>class QStandardItemModel;class Widget : public QTableView
{Q_OBJECT
public slots:void dataChangedSlot(const QModelIndex &topLeft, const QModelIndex &bottomRight,const QVector<int> &roles = QVector<int> ());
public:Widget(QWidget *parent = 0);QStandardItemModel* model;~Widget();
};#endif

widget.cpp

#include "widget.h"
#include "spindelegate.h"
#include <QStandardItemModel>
#include <QDebug>Widget::Widget(QWidget  *parent): QTableView(parent)
{model = new QStandardItemModel(this);//设置列字段名model->setColumnCount(3);model->setHeaderData(0,Qt::Horizontal,"姓名");model->setHeaderData(1,Qt::Horizontal,"年龄");model->setHeaderData(2,Qt::Horizontal,"性别");//设置行字段名model->setRowCount(3);model->setHeaderData(0,Qt::Vertical, "记录一");model->setHeaderData(1,Qt::Vertical, "记录二");model->setHeaderData(2,Qt::Vertical, "记录三");//设置一条数据model->setItem(0, 0, new QStandardItem("张三"));model->setItem(0, 1, new QStandardItem("3"));model->setItem(0, 2, new QStandardItem("男"));this->setModel(model);//设置代理this->setItemDelegate(new SpinDelegate(this));connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),this,SLOT(dataChangedSlot(QModelIndex,QModelIndex,QVector<int>)));this->setModel(model);}void Widget::dataChangedSlot(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{qDebug() << model->data(topLeft).toString() << endl;
}
Widget::~Widget()
{
}

spinDelegate.h

#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H
#include <QItemDelegate>class SpinDelegate : public QItemDelegate
{
public:SpinDelegate(QObject *parent = Q_NULLPTR);QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};#endif // SPINDELEGATE_H

spinDelegate.cpp

#include "spindelegate.h"
#include <QSpinBox>
SpinDelegate::SpinDelegate(QObject *parent): QItemDelegate(parent)
{}
QWidget* SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const
{if(index.column() == 1) // 返回下拉框{QSpinBox* box = new QSpinBox(parent);box->setMinimum(1);box->setMaximum(99);return box;}return QItemDelegate::createEditor(parent, option, index);
}


http://chatgpt.dhexx.cn/article/3B83k4ON.shtml

相关文章

QT QtableView操作详解

本文实现了使用QtableView控件来显示数据&#xff0c;数据源使用txt文本作为数据源&#xff0c;使用了QStandardItemModel作为数据模型来实现了对TableView空间的初始化&#xff0c;和对txt数据源的增删改查功能。 QT QtableView操作详解目录 1. 项目结构&软件界面 2. m…

QTableView使用方法小结

本文总结了QTableView常用方法&#xff0c;包括常规的数据显示、表头设置、字体和颜色设置、行列的高宽&#xff0c;显示格式设置等&#xff0c;还有一套分页算法&#xff0c;在QTableView表格里插入QLineEdit、QPushButton、QCheckBox等控件&#xff0c;常用的qss设置&#xf…

Qt之QTableView的简单使用(含源码+注释)

文章目录 一、QTableView操作示例图二、QTableView&#xff08;个人理解&#xff09;三、源码CMainWindow.hCMainWindow.cpp 四、拓展&#xff1a;代理的使用总结相关文章 一、QTableView操作示例图 下图为QTableView简单使用示例图&#xff0c;其中包含设置、获取、新建item等…

QTableView 基本使用

一、简介 表格视图控件 QTableView&#xff0c;需要和 QStandardItemModel 配套使用&#xff0c;这套框架是基于 MVC 设计模式设计的&#xff0c;M(Model) 是 QStandardItemModel 数据模型不能单独显示出来。V(view) 是指 QTableView 视图&#xff0c;要来显示数据模型&#x…

QTableView详细使用说明

QTableView详细使用说明 创建QTableView表格标题设置表格的标题获取表格的标题 数据操作插入数据删除数据 属性设置设置表格的对齐方式设置表格的宽高设置表格的线属性 自定义菜单eventFilter方法重载eventFilter()启用事件监听实现事件过滤函数 设置菜单属性方法设置属性关联信…

web前端新手面试指南:自我介绍

面试时注意&#xff1a;自我介绍不能太长&#xff0c;也不能过短&#xff0c;3分钟左右最合适&#xff0c;尤其做为web前端技术方面的面试&#xff0c;更加要说到点上&#xff0c;我依次从学习方面、项目实践、未来规划这三个方面写下web前端面试的自我介绍。 您好&#xff0c;…

Web前端面试自我介绍对话技巧注意事项

大家在学会了web前端技术后&#xff0c;当然是要准备找一个适合自己的web前端工作了&#xff0c;那么面试环节是必不可少的&#xff0c;有一个良好的自我介绍表述&#xff0c;在HR心中也能加分不少&#xff0c;接下来小编就为大家介绍一下Web前端面试自我介绍对话技巧注意事项。…

前端面试自我介绍的技巧都有哪些?

前端面试自我介绍的技巧都有哪些? 在前端面试自我介绍的时候&#xff0c;可能会遇到两种情况&#xff1a;允许主动和不允许主动&#xff01; 允许主动的情况下&#xff0c;前端程序员应该详细的介绍自己技能树&#xff0c;结合工作经历&#xff0c;展示给面试官自己对项目和…

JVM coredump

JVM crash分析 Java程序运行的时候&#xff0c;遇到了coredump的现象。最后定位到时JIT导致的bug。 http://www.oracle.com/technetwork/java/javase/crashes-137240.html#gbyzu 这里记录下JVMcrash产生的文件怎么去定位问题&#xff0c;但是一般不会遇到crash&#xff0c;h…

coredump 使用总结

1. core dump简介&#xff1a; A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of program state are usually dumped…

【linux】coredump问题排查

序言 记录coredump问题的一些定位技巧 1. coredump简介 coredump称为核心转储&#xff0c;就是在进程异常时的一个快照&#xff0c;保存了异常时的内存、寄存器、堆栈等数据当进程接收到某些 信号 而导致异常退出时&#xff0c;就会生成 coredump 文件core文件是ELF文件格式…

Core Dump核心转储

核心转储&#xff08;core dump&#xff09;&#xff0c;在汉语中有时戏称为吐核&#xff0c;是操作系统在进程收到某些信号而终止运行时&#xff0c;将此时进程地址空间的内容以及有关进程状态的其他信息写出的一个磁盘文件。这种信息往往用于调试。 概述 编辑 在UNIX系统中…

coredump文件的生成以及如何调试coredump文件?

生成coredump文件的相关配置 1. 首先通过 ulimit -a 命令&#xff0c;查看是否允许coredump文件生成; 如下图所示&#xff0c;core文件大小限制为0&#xff0c;即&#xff1a;不允许core文件生成 2. 使用 ulimit -c [kbytes] 命令更改core文件大小的限制 或者 通过 ulimit -c…

android生成coredump,详解coredump--全面

From: http://blog.csdn.net/tenfyguo/article/details/8159176 一,什么是coredump 我们经常听到大家说到程序core掉了,需要定位解决,这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程中异常退出或者中止,并且在满足一定条件下(这里为什么说需要满足一定的条…

coredump文件是如何生成的

目录 一、coredump 文件生成过程 二、coredump文件生成原理 1. 信号处理 do_signal() 2. 生成 coredump 文件 三、生产环境应该打开 coredump 功能吗&#xff1f; 人都会犯错&#xff0c;所以在编写程序时难免会出现 BUG。 有些 BUG 是业务逻辑上的错误导致的&#xff0c…

Coredump 详解

引言 当程序运行的过程中异常终止或崩溃&#xff0c;操作系统会将程序当时的内存状态记录下来&#xff0c;保存在一个文件中&#xff08;core文件&#xff09;&#xff0c;这种行为就叫做 Core Dump 或者叫做 ‘核心转储’&#xff0c;利用 coredump 可以帮助我们快速定位程序…

CoreDump追踪

当程序运行的过程中异常终止或崩溃&#xff0c;操作系统会将程序当时的内存状态记录下来&#xff0c;保存在一个文件中&#xff0c;这种行为就叫做Core Dump&#xff08;中文有的翻译成“核心转储”)。我们可以认为 core dump是“内存快照”&#xff0c;但实际上&#xff0c;除…

coredump详解

原文地址&#xff1a;https://blog.51cto.com/u_15471709/4868198 一&#xff0c;什么是coredump 我们经常听到大家说到程序core掉了&#xff0c;需要定位解决&#xff0c;这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程中异常退出或者中止&#xff0c;并且在满…

linux之fping命令

fping检测主机是否存在,fping命令 fping类似于ping&#xff0c;但比ping强大。与ping要等待某一主机连接超时或发回反馈信息不同&#xff0c;fping给一个主机发送完数据包后&#xff0c;马上给下一个主机发送数据包&#xff0c;实现多主机同时ping&#xff0c;fping还可以在命令…

linux fping参数,linux下,fping命令与ping命令解析

ping Linux系统的ping命令是常用的网络命令&#xff0c;它通常用来测试与目标主机的连通性。 1.命令格式&#xff1a; ping [参数] [主机名或IP地址] 2.命令功能&#xff1a; ping命令用于&#xff1a;确定网络和各外部主机的状态&#xff1b;跟踪和隔离硬件和软件问题&#xf…