当前位置 博文首页 > Allen Roson:QAbstractListModel子类化

    Allen Roson:QAbstractListModel子类化

    作者:[db:作者] 时间:2021-07-09 09:37

    目录

    ?

    实现说明

    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);
    }
    

    ?

    ?

    ?

    cs
    下一篇:没有了