Qt Qtableview 自定义model继承QAbstractTableModel头文件和cpp
时间: 2024-01-15 12:05:14 浏览: 35
下面是一个简单的自定义QAbstractTableModel的头文件和cpp文件:
MyTableModel.h:
```c++
#ifndef MYTABLEMODEL_H
#define MYTABLEMODEL_H
#include <QAbstractTableModel>
#include <QList>
class MyTableModel : public QAbstractTableModel
{
public:
MyTableModel(QObject *parent = nullptr);
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;
private:
QList<QList<QVariant>> m_data;
};
#endif // MYTABLEMODEL_H
```
MyTableModel.cpp:
```c++
#include "MyTableModel.h"
MyTableModel::MyTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
// 初始化数据
QList<QVariant> row1;
row1 << 1 << "John" << "Doe";
QList<QVariant> row2;
row2 << 2 << "Jane" << "Doe";
QList<QVariant> row3;
row3 << 3 << "Bob" << "Smith";
m_data.append(row1);
m_data.append(row2);
m_data.append(row3);
}
int MyTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_data.count();
}
int MyTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_data.first().count();
}
QVariant MyTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= m_data.count() || index.row() < 0)
return QVariant();
if (index.column() >= m_data[index.row()].count() || index.column() < 0)
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
return m_data[index.row()][index.column()];
return QVariant();
}
bool MyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
m_data[index.row()][index.column()] = value;
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return QString("ID");
case 1:
return QString("First Name");
case 2:
return QString("Last Name");
default:
return QVariant();
}
}
return QVariant();
}
Qt::ItemFlags MyTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}
```
在这个例子中,MyTableModel类实现了一个简单的表格数据模型。在构造函数中,初始化了表格数据。在data()方法中,根据index返回对应的数据。在setData()方法中,根据index和value设置对应的数据。在headerData()方法中,返回表头的标签。在flags()方法中,指定表格项可编辑。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)