Qt 模型视图笔记1
模型
![](https://img-blog.csdnimg.cn/bd6f83299b8f4323bfc6ee9126cbaa73.png)
QAbstractItemModel
- 它是一个接口类,不能直接使用。
- 使用时必须子类化并实现index(), parent(), rowCount(), columnCount(), data()。
- 通过QModelIndex索引定位数据
- 支持move(移动行列) remove(删除行列) insert(插入行列) set(设置数据) 等操作
- itemFlags(影响item) Qt::ItemFlag
![](https://img-blog.csdnimg.cn/4fe46b671dd84a2cb3db32dbed9d39bc.png)
![](https://img-blog.csdnimg.cn/358fc8ab5c044f1a8897b545c7792613.png)
示例:自定义Modal
![](https://img-blog.csdnimg.cn/86fc9a56c01e4c65b535fa7e0d10e26c.png)
![](https://img-blog.csdnimg.cn/531c1ef94223451bacd0cee83c26c3c0.png)
#ifndef MYITEMMODAL_H
#define MYITEMMODAL_H
#include <QAbstractItemModel>
class MyItemModal : public QAbstractItemModel
{
Q_OBJECT
public:
explicit MyItemModal(QObject *parent = nullptr);
// Header:
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
// Basic functionality:
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
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;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
private:
QVector<QString> m_modelData;
};
#endif // MYITEMMODAL_H
#include <QtDebug>
#include "myitemmodal.h"
MyItemModal::MyItemModal(QObject *parent)
: QAbstractItemModel(parent)
{
m_modelData.push_back(QStringLiteral("湖北省"));
m_modelData.push_back(QStringLiteral("浙江省"));
m_modelData.push_back(QStringLiteral("河南省"));
m_modelData.push_back(QStringLiteral("四川省"));
}
QVariant MyItemModal::headerData(int section, Qt::Orientation orientation, int role) const
{
qDebug()<< "header";
return QStringLiteral("省份");
}
bool MyItemModal::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if (value != headerData(section, orientation, role)) {
// FIXME: Implement me!
emit headerDataChanged(orientation, section, section);
return true;
}
return false;
}
QModelIndex MyItemModal::index(int row, int column, const QModelIndex &parent) const
{
return createIndex(row, column);
}
QModelIndex MyItemModal::parent(const QModelIndex &index) const
{
return QModelIndex();
}
int MyItemModal::rowCount(const QModelIndex &parent) const
{
// if (!parent.isValid())
// return 0;
qDebug() << "row count:" << m_modelData.size();
return m_modelData.size();
}
int MyItemModal::columnCount(const QModelIndex &parent) const
{
// if (!parent.isValid())
// return 0;
return 1;
}
QVariant MyItemModal::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role== Qt::EditRole){
return m_modelData[index.row()];
}
return QVariant();
}
bool MyItemModal::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (data(index, role) != value) {
if (role == Qt::EditRole){
qDebug() << "set";
m_modelData[index.row()] = value.toString();
}
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags MyItemModal::flags(const QModelIndex &index) const
{
// if (!index.isValid())
// return Qt::NoItemFlags;
return Qt::ItemIsEditable |Qt::ItemIsEnabled;
}
this->ui->listView->setModel(&m_myItemModal);
示例2:自定义QStringListModal
#ifndef CUSTOMMODEL_H
#define CUSTOMMODEL_H
#include <QStringListModel>
class CustomModel : public QStringListModel
{
Q_OBJECT
public:
explicit CustomModel(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;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
private:
};
#endif // CUSTOMMODEL_H
#include "custommodel.h"
#include <QIcon>
CustomModel::CustomModel(QObject *parent)
: QStringListModel(parent)
{
}
//QVariant CustomModel::headerData(int section, Qt::Orientation orientation, int role) const
//{
// // FIXME: Implement me!
//}
//int CustomModel::rowCount(const QModelIndex &parent) const
//{
// // For list models only the root node (an invalid parent) should return the list's size. For all
// // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
// if (parent.isValid())
// return 0;
// // FIXME: Implement me!
//}
QVariant CustomModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
static QStringList list= {":/new/prefix1/image/categories-new-white (1).png", ":/new/prefix1/image/categories-new-white (2).png", ":/new/prefix1/image/categories-new-white3.png"};
if (role == Qt::DecorationRole){
QIcon icon(list[index.row()]);
assert(!icon.isNull());
return icon;
}
// FIXME: Implement me!
return QStringListModel::data(index, role);
}
示例3:treeview的使用
![](https://img-blog.csdnimg.cn/46137f67ee874cd6bc74085ab8c48fa5.png)
- 主要用到 QStandardItemModel QStandardItem
QList<QStandardItem*> list;
auto item = new QStandardItem("浙江省");
auto hz = new QStandardItem("杭州");
hz->appendRow(new QStandardItem("西湖区"));
hz->appendRow(new QStandardItem("上城区"));
item->appendRow(hz);
item->appendRow(new QStandardItem("金华"));
item->appendRow(new QStandardItem("衢州"));
list.push_back(item);
model.appendRow(list);
ui->treeView->setModel(&model);
TableView QAbstractTableModel
#ifndef CUSTOMABSTRACTMODEL_H
#define CUSTOMABSTRACTMODEL_H
#include <QAbstractTableModel>
class CustomTableModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit CustomTableModel(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;
private:
QList<QList<QString>> datas;
};
#endif // CUSTOMABSTRACTMODEL_H
#include "customtablemodel.h"
const int COLUMN_COUNT = 3;
const int ROW_COUNT= 10;
CustomTableModel::CustomTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
for(auto i = 0; i < ROW_COUNT; i++) {
QString prifix =QString("%1").arg(i);
int suffix = 1;
datas.push_back({prifix+ QString("-%1").arg(suffix++), prifix+ QString("-%1").arg(suffix++), prifix+ QString("-%1").arg(suffix++)});
}
}
QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
static QList<QString> headers = {"first", " second", "third"};
if (orientation == Qt::Vertical)
{
if (role == Qt::DisplayRole){
return QVariant(QString("%1").arg(section) );
}
} else{
if (role == Qt::DisplayRole){
return headers[section];
}
}
return QVariant();
}
int CustomTableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return ROW_COUNT;
}
int CustomTableModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return COLUMN_COUNT;
}
QVariant CustomTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole){
auto row = index.row();
auto col = index.column();
return datas[row][col];
}
return QVariant();
}