Qt之Tableview之定义数据结构
介绍
QT中的表格控件有Tableview和Tablewidget两中,Tablewidget在一些数据量比较小的情景下非常适用,能够很快的建立一张表格,但某些情况数据量比较大,结构比较复杂时,就需要使用tableview来显示用户自定义数据结构了。
Model需要继承自QAbstractItemModel、QAbstractTableModel、QAbstractListModel…其中之一。QAbstractItemModel能够实现很复杂的显示,treeview就是使用这种model,QAbstractTableModel,QAbstractListModel…也是继承至它。
tablewidgetmodel.h
#ifndef TABLEWDIGETMODEL_H
#define TABLEWDIGETMODEL_H
#include <QAbstractTableModel>
#include <QStringList>
struct mydata{
int number;
QString name;
int age;
};
class TableWdigetmodel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit TableWdigetmodel(QObject *parent = nullptr);
// Header:
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
// Basic functionality:
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;
Qt::ItemFlags flags(const QModelIndex& index) const override;
void RefreshModel();
void SetSourceData(QList<mydata> *data);
private:
QList<mydata> *sourcedata=nullptr;
QStringList headlist={
QStringLiteral("序号"),
QStringLiteral("姓名"),
QStringLiteral("年龄")
};
};
#endif // TABLEWDIGETMODEL_H
tablewidgetmodel.cpp
#include "tablewdigetmodel.h"
TableWdigetmodel::TableWdigetmodel(QObject *parent)
: QAbstractTableModel(parent)
{
}
QVariant TableWdigetmodel::headerData(int section, Qt::Orientation orientation, int role) const
{
// FIXME: Implement me!
if(role == Qt::DisplayRole)
{
if(orientation == Qt::Horizontal)
{
if(section>=0 && section<headlist.count())
{
return headlist[section];
}
}
}
return QAbstractTableModel::headerData(section,orientation,role);
}
int TableWdigetmodel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
if(sourcedata)
return sourcedata->length();
// FIXME: Implement me!
}
int TableWdigetmodel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return headlist.count();
// FIXME: Implement me!
}
QVariant TableWdigetmodel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if(sourcedata)
{
if(role == Qt::TextAlignmentRole)
return Qt::AlignCenter;
if(role == Qt::DisplayRole || role == Qt::EditRole)
{
switch (index.column())
{
case 0:
return sourcedata->at(index.row()).number;
case 1:
return sourcedata->at(index.row()).name;
case 2:
return sourcedata->at(index.row()).age;
default:
break;
}
}
}
// FIXME: Implement me!
return QVariant();
}
bool TableWdigetmodel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(!index.isValid())
return false;
if(sourcedata)
{
if(role==Qt::DisplayRole && index.column()==0)
(*sourcedata)[index.column()].number=value.toInt();
else if(role==Qt::DisplayRole && index.column()==1)
(*sourcedata)[index.column()].name=value.toString();
else if(role==Qt::DisplayRole && index.column()==2)
(*sourcedata)[index.column()].age=value.toInt();
}
}
//表格需要编辑的时候就需要实现此函数
Qt::ItemFlags TableWdigetmodel::flags(const QModelIndex &index) const
{
if(!index.isValid())
return QAbstractTableModel::flags(index);
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
// if(index.column() == COLUMN_NUMBER)
// flags = flags | Qt::ItemIsUserCheckable;
// else
flags = flags | Qt::ItemIsEditable;
return flags;
}
void TableWdigetmodel::RefreshModel()
{
beginResetModel();
endResetModel();
}
void TableWdigetmodel::SetSourceData(QList<mydata> *data)
{
this->sourcedata=data;
}
还能在表格里面加上按钮进度条等,以后有时间更新。
资源连接