Let me answer my question!
It seems the usage of ListElement in the ListModel is a special language feature.
As the ListElement Manual says:
List elements are defined like other QML elements except that they
contain a collection of role definitions instead of properties. Using
the same syntax as property definitions...
According to this, it turns out, the syntax ListElement{roleName:roleValue} in ListModel would not define a instance of type ListElement, but a List element or list item of the ListModel.
I have run the following codes to prove my point.
QML:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel {
objectName: "model"
ListElement {
objectName: "element"
name: "abc"
}
}
}
CPP:
#include <QAbstractListModel>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtDebug>
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
for(const auto &rootObject:engine.rootObjects())
{
auto model = rootObject->findChild<QAbstractListModel*>("model");
qDebug() << model;
qDebug() << rootObject->findChild<QObject*>("element");
if(model)
{
qDebug() << model->roleNames();
}
}
return app.exec();
}
The output messages:
QML debugging is enabled. Only use this in a safe environment.
QQmlListModel(0x26c4e7c1500, name = "model")
QObject(0x0)
QHash((1, "objectName")(0, "name"))
Seen? The ListModel instantiated an object named model. Whereas the ListElement didn't create an object named element, but made two roles for model object.