QAbstractListModel子类化

235 篇文章 182 订阅

目录

 

实现说明

mylistmodel.h 

mylistmodel.cpp 

listmodeltest.h 

listmodeltest.cpp 


 

实现说明

当子类化QAbstractListModel时,必须提供rowCount()和data()函数的实现。行为良好的模型还提供了headerData()实现。


如果您的模型在QML中使用,并且需要roleNames()函数提供的默认角色以外的角色,则必须重写它。


对于可编辑列表模型,还必须提供setData()的实现,并实现flags()函数,以便它返回一个包含Qt::ItemIsEditable的值


请注意,QAbstractListModel提供了columnCount()的默认实现,它通知视图此模型中只有一列项。


为可调整大小的列表式数据结构提供接口的模型可以提供insertRows()和removeRows()的实现。在实现这些函数时,调用适当的函数非常重要,以便所有连接的视图都能意识到任何更改:
insertRows()实现必须在将新行插入数据结构之前调用beginInsertRows(),然后必须立即调用endInsertRows()。
removeRows()实现必须在从数据结构中删除行之前调用beginRemoveRows(),然后必须立即调用endRemoveRows()。

 

划重点:看代码时主要看注释,注释写得很清楚

 

mylistmodel.h 

#ifndef MYLISTMODEL_H
#define MYLISTMODEL_H

#include <QAbstractListModel>

class MyListModel : public QAbstractListModel
{
	Q_OBJECT

public:
	MyListModel(QObject *parent = NULL);
	~MyListModel();

	//当子类化QAbstractListModel时,必须提供rowCount()和data()函数的实现
	virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
	virtual int	rowCount(const QModelIndex & parent = QModelIndex()) const;

	//如果希望QListView为可编辑列表,必须提供setData()和flags()函数的实现
	virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
	virtual Qt::ItemFlags flags(const QModelIndex & index) const;

	/*如果希望QListView可增加或者删减列表项,可以提供下面两个函数的实现:insertRows()和removeRows(),当然也可以
	自定义其它函数来实现增加或者删减功能,但是在实现这些函数时,必须在这些函数内部调用必要的其它函数,以便所有关联的
	视图都能响应数据的更改,需要调用的函数说明如下(这里假设你实现的函数是insertRows()和removeRows()):
	insertRows()实现必须在将新行插入数据结构之前调用beginInsertRows(),然后必须立即调用endInsertRows()。
	removeRows()实现必须在从数据结构中删除行之前调用beginRemoveRows(),然后必须立即调用endRemoveRows()。
	*/
	virtual bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex());
	virtual bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());

private:
	QStringList m_strList;
};

#endif // MYLISTMODEL_H

 

mylistmodel.cpp 

#include "mylistmodel.h"
#include <windows.h>

MyListModel::MyListModel(QObject *parent)
	: QAbstractListModel(parent)
{
	m_strList<<"11"<<"22"<<"33";
}

MyListModel::~MyListModel()
{

}

QVariant MyListModel::data( const QModelIndex & index, int role /*= Qt::DisplayRole*/ ) const
{
	if (!index.isValid())
	{
		return QVariant();
	}

	if (Qt::DisplayRole == role)
	{
		return m_strList.at(index.row());
	}

	return QVariant();
}

int MyListModel::rowCount( const QModelIndex & parent /*= QModelIndex()*/ ) const
{
	return m_strList.size();
}

bool MyListModel::setData( const QModelIndex & index, const QVariant & value, int role /*= Qt::EditRole*/ )
{
	if (!index.isValid())
	{
		return false;
	}

	if (Qt::EditRole == role)
	{
		m_strList[index.row()] = value.toString();
	}

	for (int i = 0;i < m_strList.size();i++)
	{
		OutputDebugString((m_strList[i].toStdWString().append(L"\n")).c_str());
	}
	
	return true;
}

Qt::ItemFlags MyListModel::flags( const QModelIndex & index ) const
{
	//这里可以设置视图的状态,比如要设置列表为可编辑状态,则要或上Qt::ItemIsEditable
	return Qt::ItemIsEditable|Qt::ItemIsSelectable|Qt::ItemIsDragEnabled
		|Qt::ItemIsDropEnabled|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled;
}

bool MyListModel::insertRows( int row, int count, const QModelIndex & parent /*= QModelIndex()*/ )
{
	beginInsertRows(parent,row,row + count);
	m_strList.append("44");
	m_strList.append("55");
	endInsertRows();
	return true;
}

bool MyListModel::removeRows( int row, int count, const QModelIndex & parent /*= QModelIndex()*/ )
{
	beginRemoveRows(parent,row,row + count);
	for (int i = 0;i < count;i++)
	{
		m_strList.removeAt(row + i);
	}

	endRemoveRows();
	return true;
}

 

listmodeltest.h 

#ifndef LISTMODELTEST_H
#define LISTMODELTEST_H

#include <QtWidgets/QMainWindow>
#include "ui_listmodeltest.h"
#include "mylistmodel.h"

class ListModelTest : public QMainWindow
{
	Q_OBJECT

public:
	ListModelTest(QWidget *parent = 0);
	~ListModelTest();

public slots:
	void Insert();
	void Remove();

private:
	Ui::ListModelTestClass ui;

	MyListModel m_listModel;
};

#endif // LISTMODELTEST_H

 

listmodeltest.cpp 

#include "listmodeltest.h"

ListModelTest::ListModelTest(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	ui.listView->setModel(&m_listModel);
	connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(Insert()));
	connect(ui.pushButton_2,SIGNAL(clicked()),this,SLOT(Remove()));
}

ListModelTest::~ListModelTest()
{

}

void ListModelTest::Insert()
{
	m_listModel.insertRows(1,2);
}

void ListModelTest::Remove()
{
	m_listModel.removeRows(2,2);
}

 

 

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能网络管理,促进个性学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息水平,而且优了教学和管理流程,为学生、教师和家长提供了更加便捷、个性的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allen Roson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值