基于QAbstractTableModel 实现自定义model ,实现数据的增加,删除,修改并更新表格 C++代码
时间: 2024-05-11 09:20:04 浏览: 184
QT 自定义混合控件——基于View/Model/Delegate的QTableView/QTreewidget/Combobox实现
3星 · 编辑精心推荐
以下是一个基于QAbstractTableModel的自定义model的示例代码,实现了数据的增加、删除、修改和更新表格。
```cpp
// mytablemodel.h
#ifndef MYTABLEMODEL_H
#define MYTABLEMODEL_H
#include <QAbstractTableModel>
#include <QVector>
class MyTableModel : public QAbstractTableModel
{
public:
MyTableModel(QObject *parent = nullptr);
// 重载QAbstractTableModel的方法
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
// 自定义方法
bool insertRow(int row, const QModelIndex &parent = QModelIndex());
bool removeRow(int row, const QModelIndex &parent = QModelIndex());
void setTableData(const QVector<QVector<QString>> &data);
private:
QVector<QVector<QString>> m_tableData;
};
#endif // MYTABLEMODEL_H
// mytablemodel.cpp
#include "mytablemodel.h"
MyTableModel::MyTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
int MyTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_tableData.size();
}
int MyTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
if (m_tableData.isEmpty())
return 0;
return m_tableData[0].size();
}
QVariant MyTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
int row = index.row();
int column = index.column();
return m_tableData[row][column];
}
return QVariant();
}
bool MyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
int row = index.row();
int column = index.column();
m_tableData[row][column] = value.toString();
emit dataChanged(index, index, { role });
return true;
}
return false;
}
QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return QString("Column %1").arg(section);
}
if (orientation == Qt::Vertical && role == Qt::DisplayRole) {
return QString("Row %1").arg(section);
}
return QVariant();
}
Qt::ItemFlags MyTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}
bool MyTableModel::insertRow(int row, const QModelIndex &parent)
{
beginInsertRows(parent, row, row);
QVector<QString> rowData(columnCount());
m_tableData.insert(row, rowData);
endInsertRows();
return true;
}
bool MyTableModel::removeRow(int row, const QModelIndex &parent)
{
beginRemoveRows(parent, row, row);
m_tableData.remove(row);
endRemoveRows();
return true;
}
void MyTableModel::setTableData(const QVector<QVector<QString>> &data)
{
m_tableData = data;
emit layoutChanged();
}
```
使用示例:
```cpp
// main.cpp
#include <QApplication>
#include <QTableView>
#include "mytablemodel.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QVector<QVector<QString>> data = {
{ "1", "Tom", "20" },
{ "2", "Jack", "25" },
{ "3", "Lucy", "18" }
};
MyTableModel model;
model.setTableData(data);
QTableView tableView;
tableView.setModel(&model);
tableView.show();
return a.exec();
}
```
以上代码实现了一个简单的表格,包括三列:编号、姓名和年龄。可以通过调用`insertRow()`、`removeRow()`和`setData()`方法来增加、删除和修改表格中的数据,表格会自动更新。
阅读全文