QML using C ++ implementation of the TreeView Model

QML is implemented using C ++ TreeView Model (a)
Classification: C / C ++ 2016-04-13 22:27:24
data access components such as QML ListView, TableView, GridView ListModel generally used as a data provider, such application has considerable limitations, if you can not access the local file system, can not connect to traditional SQL database, it is usually in use for data are accessed through C ++, data display and editing by QML, common data model components have QAbstractItemModel , QAbstractTableModel, QSQLTableModel and so on. All superior Model components inherit from QAbstractItemModel, if we can understand the interface function and operation mechanism QAbstractItemModel, you can understand the Model / View QT mechanism of implementation.
QAbstractItemModel is an abstract class, instantiates QAbstractItemModel must inherit and implement at least the following five methods:

int rowCount(const QModelIndex &parent=QModelIndex()) const;
int columnCount(const QModelIndex &parent=QModelIndex()) const;
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const;
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
QModelIndex parent(const QModelIndex &child) const;

QWidget component and is different in the data model in QML not be accessed by the column data (the Column), but the data access through Role, for example:
click (here) folded or unfolded

TableView{
id:tableView1
anchors.fill: parent
TableViewColumn{
width:50
title:""
role:“tagging”
}

        TableViewColumn{
            width:80
            title:"操作"
            role:"name"
        }

}
TableViewColumn is TableView the column definition, TableViewColumn acquired data to the model defined by the role attribute, TableView will be acquired by the role model is available roleNames () method calls the model. Therefore, in addition to the need to achieve five virtual function, you must also re-implement roleNames () View tell what role is available, roleNames () has the following prototype:

Click (here) folded or unfolded

QHash<int,QByteArray> roleNames() const;

The following is a complete Model class definition:

Click (here) folded or unfolded

class SqlMenuEntry:public QAbstractItemModel,public QQmlParserStatus
{
Q_OBJECT
public:
explicit SqlMenuEntry(QObject *parent=0);
~SqlMenuEntry();
enum MenuEntryRoles{idRole=Qt::UserRole+1,nameRole,defaultEntryRole,customEntryRole,iconRole,iconHoverRole};
int rowCount(const QModelIndex &parent=QModelIndex()) const;
int columnCount(const QModelIndex &parent=QModelIndex()) const;
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const;
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
QModelIndex parent(const QModelIndex &child) const;
QHash<int,QByteArray>roleNames() const;
private:
QHash <int, QByteArray> mRoleNames;
QList <QHash <int, QVariant >> mRecords; // actual data stored here, QList can only save two-dimensional data can not save a tree node, here is just an example
};

Achieve roleNames () is quite simple:

Click (here) folded or unfolded

QHash <int, the QByteArray> :: SqlMenuEntry roleNames () const
{
return mRoleNames;
}
mRoleNames can be initialized in the class constructor:

Click (here) folded or unfolded

:: SqlMenuEntry SqlMenuEntry (QObject * parent)
: the QAbstractItemModel (parent)
{
mRoleNames [nameRole] = "name";
mRoleNames [idRole] = "menuid";
mRoleNames [iconRole] = "icon";
mRoleNames [defaultEntryRole] = "default" ;
mRoleNames [iconHoverRole] = "iconHover";
}
can, "menuid", "icon" data in QML by "name" access:
click (here) folded or unfolded

{The ListView
Model: {} MenuEntryModel
the delegate: Item {
the Column {
the Text text {: name}
the Text {text: icon}
}
}
}
If the two-dimensional table only provides data, then according to the name of the interface more than a few simple functions can be view to realize the data supplied, wherein:

Click (here) folded or unfolded

int SqlMenuEntry::rowCount(const QModelIndex &parent) const
{
return mRecords.size();

}
Int SqlMenuEntry :: the columnCount (const QModelIndex & parent) const
{
return. 1; // QML not use the column to obtain data returned by default one, does not return one case, then, View control considers the table is empty, without acquiring data
}
QModelIndex SqlMenuEntry index :: (Row int, int column, const QModelIndex parent &) const
{
IF ((Row> = 0) && (Row <mRecords.size ()))
{
return createIndex (Row, column);
}
return QModelIndex (); // return an invalid empty index
}
QModelIndex SqlMenuEntry :: parent (const QModelIndex Child &) const
{
return QModelIndex (); // not a two-dimensional table row parent node
}
the QVariant SqlMenuEntry :: Data (const & QModelIndex index, int Role ) const
{
IF (index.isValid)
{
return mRecords [index.row ()] [Role];
}
}

MRecords data on demand may be generated, as acquired from the database server QSqlQuery components, data gets added:

Click (here) folded or unfolded

QHash <int, the QVariant> Row;
Row [nameRole] = "NAME1";
Row [iconRole] = "icon1";
mRecords.append (Row);
the model implemented by class

Click (here) folded or unfolded

qmlRegisterType ( " com.limutech.tv ", 1,0, "MenuEntryModel");
to be registered, the registration may be generated after the class instances QML:

Click (here) folded or unfolded

Import com.limutech.tv 1.0
MenuEntryModel {
ID: menuEntryModel
}
the ListView {
Model: menuEntryModel
...
}
View data acquisition assembly process probably as follows:
. 1, View call rowCount (const QModelIndex & parent) transmission line and acquired root node of a parent empty number;
2, View call columnCount (const QModelIndex & parent) and pass an empty parent number of columns in the root node;
. 3, View enumeration call index (int row, int column, const QModelIndex & parent) Consecutive line number to each row , column number and obtaining the empty root parent QModelIndex;
. 4, continue to be returned to parent modelIndex rowCount acquisition and columnCount each row, it is greater than 0 if there is a child node of the node;
list role 5, return call roleNames available
6 to return the modelIndex and role parameters, data acquisition and data calls using the appropriate delegate display
to display one of the tree list, you need to perfect the two-dimensional table model, parent handle the situation is not empty, at the same time, the model should data can be stored in a tree, in the next section I will continue to share the hierarchical model and implementation And modify some of the data achieved.

Guess you like

Origin blog.csdn.net/qq_43248127/article/details/90897535