Qt | QTableView的使用方法
Qt | QTableView的使用方法
QTableView的数据是基于model显示的,需要先建一个model,然后tableView通过setModel进行模型绑定,后续操作model就可以实现表格数据的读写。
1、基础使用
1、设置模型显示
# 创建3X3模型,如果不指定表格大小,那么他会根据标签的数量自行定义表格大小model = QStandardItemModel(3, 3, self.__MainWindow)# 设置行标签model.setHorizontalHeaderLabels(['编号', '姓名', '年龄'])# 设置列标签model.setVerticalHeaderLabels(['一班', '二班', '三班'])# 设置模型到TabelView上self.tableView.setModel(model)
2、设置内容显示
data = ['1', '2', '3']for i in range(len(data)):item = QStandardItem(data[i])# 设置内容对齐方式为:水平中心对齐+垂直中心对齐item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)model.setItem(i, i, item)
3、获取表格内容
QString data = model->data(model->index(0, 0)).toString(); // 获取第[0,0]格的数据
4、设置表格之间根据间距调整宽度方式
一共5种显示模式:
- Custom
- Fixed
- Interactive
- ResizeToContents
- Stretch
常用的是根据内容自动调整大小和拉伸
设置方法:
# 设置水平标签重载大小模式为拉伸,也就是铺满整个框,每一列间距是一样的,这样比较好看
self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
5、隐藏标题栏
tableView->verticalHeader()->hide();
2、表格设置成下拉框形式
QTableView的表格默认显示的是ItemDelegate,双击表格的时候可以实现编辑,如果想要实现双机表格出现一个下拉框,那么就需要将Delegate设置为QComboBox,实现办法是自定义继承自QItemDelegate的ComboDelegate,覆写updateEditorGeometry、setModelData、setEditorData、createEditor这四个函数即可。代码如下:
头文件:
#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H#include <QItemDelegate>class ComboDelegate : public QItemDelegate
{Q_OBJECTpublic:ComboDelegate() = delete;virtual ~ComboDelegate();ComboDelegate(const QStringList &items, 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;signals:void currentIndexChange(int row, int col) const; //定义消息必须有constprivate slots :private:QStringList items;
};#endif // COMBODELEGATE_H
源文件:
#include "ComboDelegate.h"
#include <QtWidgets/QComboBox>
#include <QDebug>ComboDelegate::ComboDelegate(const QStringList &items, QObject *parent) : QItemDelegate(parent)
{this->items = items;
}ComboDelegate::~ComboDelegate()
{}QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{QComboBox *editor = new QComboBox(parent);editor->addItems(items);editor->setEditable(false); // 不可编辑return editor;
}void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QString value = index.model()->data(index, Qt::EditRole).toString();QComboBox *comboBox = static_cast<QComboBox*>(editor);int currIndex = comboBox->findText(value);comboBox->setCurrentIndex(currIndex);
}void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QComboBox *comboBox = static_cast<QComboBox*>(editor);QString value = comboBox->currentText();model->setData(index, value, Qt::EditRole);emit currentIndexChange(index.row(), index.column()); // 发送信号,实现其它联动更新
}void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const
{(void)index; // 防止编译警告editor->setGeometry(option.rect);QComboBox *comboBox = static_cast<QComboBox*>(editor);comboBox->showPopup(); // 直接弹出下拉框,没有这一句双击后需要再点一次Combox才会弹出下拉框
}
使用方法:
QStringList sensorTypes;sensorTypes << ""<< "颗粒物"<< "火灾"<< "烟感"<< "水浸"<< "温度"<< "湿度"<< "温湿度"<< "自定义1"<< "自定义2"<< "自定义3"<< "自定义4"<< "自定义5"<< "自定义6"<< "自定义7"<< "自定义8"<< "自定义9"<< "自定义10";ComboDelegate* delegateSensorTypes = new ComboDelegate(sensorTypes, this);ui->tableView->setItemDelegateForColumn(0, delegateSensorTypes); // 第0列表格设置为下拉框形式
效果图:
ends…
Qt | QTableView的使用方法相关推荐
- Qt QTableView表格排序
关于Qt QTableView表格排序的问题 本人用到的方法是setSortEnable,发下针对某一列排序失败. 后来发现该列值虽然显示为数字,但是实际存储的是字符串,所以table会按照字符串来排 ...
- python表格控件_python GUI库图形界面开发之PyQt5表格控件QTableView详细使用方法与实例...
PyQt5表格控件QTableView简介 在通常情况下,一个应用需要和一批数据进行交互,然后以表格的形式输出这些信息,这时就需要用到QTableView类了,在QTableView中可以使用自定义的 ...
- qt qtableview 刷新列表_qt qtablewidget 刷新
QTableWidget单击.双击表头进行排序_互联网_IT/计算机_专业资料 暂无评价|0人阅读|0次下载|举报文档 QTableWidget单击.双击表头进行排序_互联网_IT/计算机_专业... ...
- Qt Creator用户互动方法
Qt Creator用户互动方法 用户互动方法 基本互动方法 鼠标区域 重点范围 可轻弹 常规控制属性 按钮控制 按钮 延迟按钮 复选框 单选按钮 转变 圆形按钮 显示文字和图标 检查按钮 按钮信号 ...
- Qt程序打包发布方法(使用官方提供的windeployqt工具)
Qt程序打包发布方法(使用官方提供的windeployqt工具) 转自:http://tieba.baidu.com/p/3730103947?qq-pf-to=pcqq.group Qt 官方开发环 ...
- QT QtableView操作详解
本文实现了使用QtableView控件来显示数据,数据源使用txt文本作为数据源,使用了QStandardItemModel作为数据模型来实现了对TableView空间的初始化,和对txt数据源的增删 ...
- Qt程序打包发布方法(使用官方提供的windeployqt工具) 转自:http://tieba.baidu.com/p/3730103947?qq-pf-to=pcqq.group Qt 官方
Qt程序打包发布方法(使用官方提供的windeployqt工具) 转自:http://tieba.baidu.com/p/3730103947?qq-pf-to=pcqq.group Qt 官方开发环 ...
- python gui 显示表格_python GUI库图形界面开发之PyQt5表格控件QTableView详细使用方法与实例...
PyQt5表格控件QTableView简介 在通常情况下,一个应用需要和一批数据进行交互,然后以表格的形式输出这些信息,这时就需要用到QTableView类了,在QTableView中可以使用自定义的 ...
- Qt控件使用方法技巧合集
Qt控件使用方法技巧合集 ~~~~~~~~ 本篇博客代码示例包含C++版本和Python版本,不管是哪个版本,Qt的API都是一样的,使用方式大同小异. 文章目录 Qt控件使用方法技巧合 ...
最新文章
- mybatis的一些基础问题
- Deep Learning and Shallow Learning
- 机器学习理论梳理2 : KNN K近邻分类模型
- 施一公:带好学生,是特别要紧的事
- 55 - I. 二叉树的深度
- 【Elasticsearch】使用Elasticsearch中的copy_to来提高搜索效率
- [译][Tkinter 教程15] event 事件绑定
- matlab 画非线性曲线,matlab 非线性曲线拟合, nlinfit lsqcurvefit lsqnonlin
- 从 0 开始了解 Docker(ubuntu )
- kali wifi密码 破解
- 数据库系统概论第五版(王珊)-系统篇(十二)
- 【源码】采用PI反馈控制的DC-DC降压变换器仿真
- 华硕 小布 类似机器人_盘点足球赛事背后的高科技 华硕机器人小布化身观赛助理...
- 西瓜书读书笔记(一)
- 美国未来计算机人才需求,对话美国帕森斯前院长:“未来人才需求趋势”
- 2019 年终总结,168 篇,已归类!
- 万恶之源的hello world
- ROC、PR曲线、AUC值
- 基于Lae的ffmpeg播放器
- mysql 5.7 在线ddl