wordpress加授權(quán)網(wǎng)絡(luò)優(yōu)化工程師證書
1.簡介
QAbstractListModel是Qt框架中的一個抽象類,用于實現(xiàn)數(shù)據(jù)模型,用于在Qt的視圖組件中展示和編輯列表數(shù)據(jù)。與QAbstractTableModel類似,它也是一個抽象類,提供了一些基本的接口和默認(rèn)實現(xiàn),可以方便地創(chuàng)建自定義的列表數(shù)據(jù)模型。
QAbstractListModel的主要功能包括以下幾點:
- 數(shù)據(jù)的獲取和設(shè)置:通過實現(xiàn)data()和setData()接口,可以用于獲取和設(shè)置列表中的數(shù)據(jù)??梢愿鶕?jù)自己的數(shù)據(jù)結(jié)構(gòu)和邏輯,在這兩個接口中進(jìn)行相關(guān)的操作。data()方法用于獲取指定索引位置的數(shù)據(jù),setData()方法用于設(shè)置指定索引位置的數(shù)據(jù)。
- 列表項的管理:可以通過rowCount()方法獲取列表中的項數(shù)。也可以通過insertRows()和removeRows()方法動態(tài)地增加或刪除列表項。
- 列表的顯示和編輯:可以通過實現(xiàn)displayRole和editRole相關(guān)方法來確定列表數(shù)據(jù)在視圖中的顯示和編輯方式。也可以通過實現(xiàn)flags()方法來指定每個列表項的編輯屬性。
- 數(shù)據(jù)的排序:可以通過實現(xiàn)sort()方法來對列表中的數(shù)據(jù)進(jìn)行排序。
由于該模型提供了比QAbstractItemModel更專業(yè)的接口,因此不適合與樹視圖一起使用;如果您想提供一個用于此目的的模型,則需要對QAbstractItemModel進(jìn)行子類化。如果您需要使用多個列表模型來管理數(shù)據(jù),則可能更適合使用子類QAbstractTableModel。
繼承QAbstractListModel,需要重寫rowCount()、data()、insertRows()、removeRows()等函數(shù)。
- rowCount()函數(shù)返回模型的行數(shù)。
- data()函數(shù)返回指定索引處的數(shù)據(jù)。
- insertRows()插入行
- removeRows()刪除行
2.示例
聲明數(shù)據(jù)結(jié)構(gòu)體:
typedef struct _student
{QString name;int age;double score;
}Student;
重寫rowCount()、data()、insertRows()和removeRows()等函數(shù)。?
#ifndef MYLISTMODEL_H
#define MYLISTMODEL_H#include <QAbstractListModel>
#include <QObject>
#include <QList>typedef struct _student
{QString name;int age;double score;
}Student;class MyListModel : public QAbstractListModel
{Q_OBJECT
public:MyListModel(QObject *parent = nullptr);enum RoleNames{Name,Age,Score};public://更新void update(QList<Student> students);// 返回列表中行的數(shù)量virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;// 返回指定索引處的數(shù)據(jù)virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;//插入行virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());//刪除行virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());private:QList<Student> m_lstStu;
};#endif // MYLISTMODEL_H#include "MyListModel.h"MyListModel::MyListModel(QObject *parent): QAbstractListModel(parent)
{}void MyListModel::update(QList<Student> students)
{m_lstStu = students;for(int i=0;i<m_lstStu.size();i++){beginInsertRows(QModelIndex(),i,i);endInsertRows();}
}int MyListModel::rowCount(const QModelIndex &parent) const
{Q_UNUSED(parent);return m_lstStu.size();
}QVariant MyListModel::data(const QModelIndex &index, int role) const
{if(!index.isValid())return QVariant();int nRow = index.row();Student stu = m_lstStu.at(nRow);if (role == Qt::DisplayRole || role == Qt::EditRole){QString ret = QString("%1_%2_%3").arg(stu.name).arg(stu.age).arg(stu.score);return ret;}return QVariant();
}bool MyListModel::insertRows(int row, int count, const QModelIndex &parent)
{if (row >= 0 && row <= m_lstStu.size()){beginInsertRows(parent, row, row + count - 1);for (int i = 0; i < count; ++i){//插入一個空的數(shù)據(jù)Student stu;stu.name = QString();stu.age = 0;stu.score = 0;m_lstStu.insert(row, stu);}endInsertRows();return true;}return false;
}bool MyListModel::removeRows(int row, int count, const QModelIndex &parent)
{if (row >= 0 && row + count <= m_lstStu.size()){beginRemoveRows(parent, row, row + count - 1);for (int i = 0; i < count; ++i){m_lstStu.removeAt(row);}endRemoveRows();return true;}return false;
}
使用示例:
#include "ListForm.h"
#include "ui_ListForm.h"
#include "MyListModel.h"MyListModel *pModel = nullptr;ListForm::ListForm(QWidget *parent) :QWidget(parent),ui(new Ui::ListForm)
{ui->setupUi(this);//去除選中虛線框ui->listView->setFocusPolicy(Qt::NoFocus);//設(shè)置整行選中ui->listView->setSelectionBehavior(QAbstractItemView::SelectRows);pModel = new MyListModel(this);// 構(gòu)造數(shù)據(jù),更新界面QList<Student> students;QList<QString> nameList;nameList<<"張三"<<"李四"<<"王二"<<"趙五"<<"劉六";for (int i = 0; i < 5; ++i){Student student;student.name = nameList.at(i);student.age = qrand()%6 + 13;//隨機生成13到19的隨機數(shù)student.score = qrand()%20 + 80;//隨機生成0到100的隨機數(shù);students.append(student);}pModel->update(students);ui->listView->setModel(pModel);
}ListForm::~ListForm()
{delete ui;
}void ListForm::on_btnInsert_clicked()
{if(!pModel)return;int row = ui->listView->currentIndex().row();if(row < 0)return;pModel->insertRows(row+1,1);
}void ListForm::on_btnDel_clicked()
{if(!pModel)return;int row = ui->listView->currentIndex().row();if(row < 0)return;pModel->removeRows(row,1);
}
3.推薦
Qt 繼承QAbstractTableModel實現(xiàn)自定義TableModel-CSDN博客
Qt 插件開發(fā)詳解_qt插件化開發(fā)-CSDN博客
Qt 繼承QAbstractTableModel實現(xiàn)自定義TableModel-CSDN博客
?