QAbstractTableModel使用详解&数据单条更新&整体更新

这篇博客介绍了如何在Qt中实现一个最简单的QTableView模型,包括设置行数、列数、单元格数据以及表头数据。进阶部分讲解了数据的更新、插入和删除操作,提供了自定义模型类`CustomTableModel`的实现,包括数据结构和关键函数的详细代码。此外,还展示了如何设置QTableView的编辑触发、列宽调整等样式选项。

1、入门

实现一个最简单的model需要实现如下函数

//QTableView使用model的时候,根据这个函数获取表格有多少行。
virtual int    rowCount(const QModelIndex & parent = QModelIndex()) const;  
//QTableView使用model的时候,根据这个函数获取表格有多少列。
virtual int columnCount(const QModelIndex & parent = QModelIndex()) const;
//QTableView使用model的时候,根据这个函数获取单元格的数据。
virtual QVariant    data(const QModelIndex & index, int role = Qt::DisplayRole) const;
//QTableView使用model的时候,根据这个函数获取表头的数据,自己可以实现水平表头和垂直表头数据。
virtual QVariant    headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;

model实现完成之后,设置给QTableView,假设 QTableView *table = new QTableView;

CustomTableModel * model = New CustomTableModel(table);
table->setModel(model);

//想table好看点就需要做一些设置,列举一点,根据需要自己选用
table->verticalHeader()->setVisible(false);    //设置垂直表头不可见
table->setEditTriggers(QAbstractItemView::AllEditTriggers);    //任意动作都可出发编辑
table->resizeColumnsToContents();    //设置表格的列宽以内容宽度为准
table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);    //设置表头可拉伸,这样的话会填充整个画面可填充宽度。

2、进阶

数据的整体更新,单条更新,单条插入,单条删除等,参考代码如下。

想更新的时候,就直接调用model接口就行

QList<CustomTableItem> list = ;    //你需要更新的 items
model->updateAllItems(list);

CustomTableModel.h中的实现如下

写一个最基本的tablemodel给Qtableview用,需要重写如下几个函数。

//自定义数据存储格式,如果数据比较简单,就不需要这个数据结构。
//这里支持四个字段,属性名(strPropertyName),属性值(strProp
使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you&#39;re still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you&#39;re almost done, but not quite. What&#39;s the use of putting a JButton in a JTable if you can&#39;t press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值