时间:2021-07-01 10:21:17 帮助过:6人阅读
TableViewColumn是TableView的列定义,TableViewColumn通过role属性定义来向model获取数据,TableView会通过调用model的roleNames()方法来获取model可用的role。所以,除了必须要实现的5个虚函数外,还必须重新实现roleNames()来告诉View有哪些role是可用的,roleNames()的原型如下:
- TableView{
- id:tableView1
- anchors.fill: parent
- TableViewColumn{
- width:50
- title:""
- role:"tagging"
- }
- TableViewColumn{
- width:80
- title:"操作"
- role:"name"
- }
- }
- QHash
roleNames() const;
roleNames()的实现相当简单:
- classSqlMenuEntry: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
roleNames() const; - private:
- QHash
mRoleNames; - QList
> mRecords; //真正的数据保存在这里,QList只能保存二维数据没办法保存树状节点,这里仅仅是例子 - };
mRoleNames可以在类构造函数中进行初始化:
- QHash
SqlMenuEntry::roleNames() const
- {
- return mRoleNames;
- }
在QML中就可以通过"name"、"menuid"、"icon"对数据进行访问:
- SqlMenuEntry::SqlMenuEntry(QObject *parent)
- :QAbstractItemModel(parent)
- {
- mRoleNames[nameRole] = "name";
- mRoleNames[idRole] = "menuid";
- mRoleNames[iconRole] = "icon";
- mRoleNames[defaultEntryRole] = "default";
- mRoleNames[iconHoverRole] = "iconHover";
- }
如果仅为二维表提供数据,那么根据以上几个接口函数的名称就可以简单的实现数据供给View视图,其中:
- ListView{
- model:MenuEntryModel{ }
- delegate:Item{
- Column{
- Text{text:name}
- Text{text:icon}
- }
- }
- }
- int SqlMenuEntry::rowCount(const QModelIndex &parent) const
- {
- return mRecords.size();
- }
- int SqlMenuEntry::columnCount(const QModelIndex &parent) const
- {
- return 1; //QML不使用列获取数据,默认返回一列,不返回1例的话,View控件会认为表是空表,不获取数据
- }
- QModelIndex SqlMenuEntry::index(int row, int column, const QModelIndex &parent) const
- {
- if((row >= 0)&&(row < mRecords.size()))
- {
- return createIndex(row,column);
- }
- return QModelIndex(); //返回一个无效的空索引
- }
- QModelIndex SqlMenuEntry::parent(const QModelIndex &child) const
- {
- return QModelIndex(); //二维表中的行没有parent节点
- }
- QVariant SqlMenuEntry::data(const QModelIndex &index, int role) const
- {
- if(index.isValid)
- {
- return mRecords[index.row()][role];
- }
- }
实现后的model类可以通过
- QHash
row;
- row[nameRole] = "name1";
- row[iconRole] = "icon1";
- mRecords.append(row);
进行注册,注册后的类可以在QML生成实例:
- qmlRegisterType
("com.limutech.tv",1,0,"MenuEntryModel");
View组件获取数据的流程大概如下:
- import com.limutech.tv 1.0
- MenuEntryModel{
- id:menuEntryModel
- }
- ListView{
- model:menuEntryModel
- ...
- }
http://www.bkjia.com/PHPjc/1120299.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1120299.htmlTechArticle使用C++实现QML的TreeView Model (一) QML中的数据访问组件如ListView、TableView、GridView通常使用ListModel做为数据提供者,这种应用有相当大局限...