here is my setup:
First is the model class, this stores a pointer to the data of type CompetitionsList. We can see that it implements the necessary basic functions when deriving from QAbstractListModel, and it uses the QML_ELEMENT macro to expose the model to the QML type system:
class CompetitionsListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(CompetitionsList* list READ list WRITE setList)
QML_ELEMENT
public:
explicit CompetitionsListModel(QObject *parent = nullptr);
enum {
NameRole = Qt::ItemDataRole(),
IdRole
};
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
CompetitionsList *list() const;
void setList(CompetitionsList *list);
virtual QHash<int, QByteArray> roleNames() const;
private:
CompetitionsList* m_list; \\ <--- ptr to data
};
Then we have the data class. This class is also a QML_ELEMENT, and it has a userid property. This is used to populate the QVector<listItem> by requesting data from a RESTful http server.
The two signals preItemAppended() and postItemAppended() are emitted when data is added to the QVector<listItem>. These signals are then connected to the Model as a way of notifying the model it must create a new row in the list. The important function here is populateCompetitions, which I explain below.
struct listItem {
QString competitionName;
QString competitionId;
};
class CompetitionsList : public QObject
{
Q_OBJECT
Q_PROPERTY(QString userId READ getUserId WRITE setUserId)
QML_ELEMENT
public:
explicit CompetitionsList(QObject *parent = nullptr);
void populateCompetitions(const QString& userId);
QVector<listItem> items();
QString getUserId() const;
void setUserId(const QString &value);
signals:
void preItemAppended();
void postItemAppended();
void errorPopulatingData();
private:
QVector<listItem> m_competitions;
QString userId;
};
The populateCompetitions function looks as so:
void CompetitionsList::populateCompetitions(const QString &userId)
{
qDebug() << "initialising data";
HttpClient* client = new HttpClient();
client->getUserCompetitions(userId);
connect(client, &HttpClient::getUserCompetitionsResult, [=](const QByteArray& reply){
QJsonDocument _reply = QJsonDocument::fromJson(reply);
if(_reply["data"].isNull()) {
emit errorPopulatingData();
}
else {
const QJsonArray competitions = _reply["data"]["competitions"].toArray();
for(const QJsonValue& competition : competitions) {
emit preItemAppended();
listItem item{competition["competition-name"].toString(), competition["competition-id"].toString()};
m_competitions.append(std::move(item));
emit postItemAppended();
}
}
});
}
It requests data from the database, and then stores it locally.
Then finally, the QML model which ties it all together:
ListView {
id: view
implicitHeight: 1920
implicitWidth: 1080
clip: true
model: CompetitionsListModel {
list: CompetitionsList {
id: compList
}
Component.onCompleted: { compList.userId = "6033f377257e8630ed13299e" } //<-- calls the populateCompetitions function
}
delegate: RowLayout {
Text {
text: qsTr(model.name + ":::" + model.id)
}
}
}
We can see that it has a ListView element, with a model of type CompetitionsListModel and a list of type CompetitionsList. Once the model component is created, we set the userId of the list, this in turn calls the populateCompetitions function which sets up the data for the model to use as seen above.
Unfortunately this doesnt anything when I run the code. Blank screen. Nada. I was wondering if anyone has an insight as to what might be causing this based on the code provided. Ive been at it for so long and it just inst being nice.
I think you need to call qmlRegisterType or UncreatableType on your CompetitionsListModel :
https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterType
Related
i have a cpp struct which have so much fields
struct CloudMusicSongList{ ... };
and i want use it in qml in order input some infomation to it's instance, but i don't want to create a qobject derived class,and create a lot of qproperty ...
i seache in google and get this code snippt from this blog
class ListItem : public QObject
{
Q_OBJECT
public :
ListItem(QObject *parent = 0) : QObject(parent) {}
virtual ~ListItem() {}
virtual int id() const = 0;
virtual QVariant data(int role) const = 0;
virtual QHash<int, QByteArray> roleNames() const = 0;
virtual void triggerItemUpdate() {emit dataChanged();}
signals:
void dataChanged();
};
i just try following code but even not print called data method
defined.h
template <typename T>
class bindingProxy : public bindingProxyBase
{
public:
bindingProxy(QObject* parent = 0)
: bindingProxyBase(parent)
{
}
T tm;
virtual ~bindingProxy()
{
}
virtual QVariant data(int role)
{
qDebug() << "called data method";
QVariant qv;
auto fucs = Prism::Reflection::get_md<T>();
if (role == Qt::DisplayRole)
fucs[0].getFunc(tm, 0, qv);
else
fucs[role].getFunc(tm, role, qv);
return qv;
}
QHash<int, QByteArray> roleNames() const
{
static QHash<int, QByteArray> roles{};
if (roles.count() == 0)
{
for (Prism::Reflection::PrismMetaData<T>& item : Prism::Reflection::get_md<T>())
{
roles[item.offset] = item.name;
}
}
return roles;
}
bool setData(QVariant& value, int role = Qt::EditRole)
{
Prism::Reflection::get_md<T>()[role].setFunc(tm, role, value);
triggerItemUpdate();
return true;
}
};
bodyViewModel.cpp
#include ....
...
..
Q_DECLARE_METATYPE(bindingProxy<CloudMusicSongList>*);
class BodyViewModel : public QObject
{
Q_PROPERTY(bindingProxy<CloudMusicSongList>* editSongListModel READ editSongListModel WRITE setEditSongListModel NOTIFY editSongListModelChanged)
...
...
...
uing.qml
Button {
id:btn_tst
text: BodyViewModelCpp.editSongListModel.name + "hbb"
onClicked: BodyViewModelCpp.editSongListModel.name = "3232"
}
button look like this image after run app rung
i don't know whether it is the X Y problem.
somebody can help? thanks
i want create a bindingProxy template class which twoway bindable in qml
i think it should equivalence dynamicObject in wpf
Based on CloudMusicSongList I'm assuming the thing that you have is a list/array. There are quite a number of ways of exposing this to QML.
QQmlListProperty - https://doc.qt.io/qt-6/qqmllistproperty.html
QAbstractListModel - https://doc-snapshots.qt.io/qt6-dev/qabstractlistmodel.html
QVariant, QVariantList, and/or QVariantMap - https://doc-snapshots.qt.io/qt6-dev/qvariant.html
QObject / Q_OBJECT - create a new QML component and register it
Q_GADGET - register your C++ structure (good for access the class in a signal, but, QML/JS cannot copying it into a Javascript object, only good for the duration of the signal)
QQmlListProperty is a list of QObjects. It requires the underlying QObject be registered, but, allows you to create/manage and walk through a list of the results. The list is owned, usually by another QObject. e.g. we could have a QmlFile object, then implement a QmlFileSystem object that can return a list of QmlFiles.
QAbstactListModel can be used in places where a model is required, e.g. ListView.
QVariant, QVariantList and QVariantMap is the simplest way of creating a Javascript object in QML. The advantage is you do not have to register anything, but, the disadvantage is you do not get intellisense.
QVariant getBooks() {
QVariantList books;
QVariantMap book1;
book1["title"] = QString("Lord of the Rings");
QVariantMap book2;
book2["title"] = QString("The Hobbit");
books.append(book2);
return books;
// results in QML/JS:
// [ { "title": "Lord of the Rings" },
// { "title" : "The Hobbit" } ]
}
I'm trying to achieve a filter model that has a source model inside QML. Both of my models are c++ based and registered as modules.
Assuming I have:
ListView {
id: autocomplete
anchors.top: field.bottom
model: MyTreeModelCompleter {
separator: "."
model: MyTreeModel{}
}
}
The c++ of MyTreeModelCompleter:
class TreeModelCompleter : public QCompleter
{
Q_OBJECT
Q_PROPERTY(QString separator READ separator WRITE setSeparator)
Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel)
public:
explicit TreeModelCompleter(QObject *parent = Q_NULLPTR);
explicit TreeModelCompleter(QAbstractItemModel *model, QObject *parent = Q_NULLPTR);
QString separator() const;
QAbstractItemModel* model();
public slots:
void setSeparator(const QString&);
void setModel(QAbstractItemModel*);
protected:
QStringList splitPath(const QString &path) const override;
QString pathFromIndex(const QModelIndex &index) const override;
private:
QString m_sep;
QAbstractItemModel *m_model;
};
MyTreeModel c++:
class MyTreeModel : public QAbstractItemModel
{
Q_OBJECT
...
};
MyTreeModel QML:
MyTreeElement {
property bool check
property string name: "name1"
property string description: "desc of name1"
MyTreeElement {
property bool check
property string name: "name2"
property string description: "desc of name2"
}
MyTreeElement {
property bool check
property string name: "name3"
property string description: "desc of name3"
MyTreeElement {
property bool check
property string name: "name 4"
property string description: "desc of name4"
MyTreeElement {
property bool check
property string name: "name 5"
property string description: "desc of name5"
}
}
}
}
MyTreeelement:
class MyTreeNode : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QQmlListProperty<MyTreeNode> nodes READ nodes)
Q_CLASSINFO("DefaultProperty", "nodes")
MyTreeNode(QObject *parent = Q_NULLPTR);
void setParentNode(MyTreeNode *parent);
Q_INVOKABLE MyTreeNode *parentNode() const;
bool insertNode(MyTreeNode *node, int pos = (-1));
QQmlListProperty<MyTreeNode> nodes();
MyTreeNode *childNode(int index) const;
void clear();
Q_INVOKABLE int pos() const;
Q_INVOKABLE int count() const;
private:
QList<MyTreeNode *> m_nodes;
MyTreeNode *m_parentNode;
};
My main issue is including MyTreeModel inside the completer model MyTreeModelCompleter. The issue is generated when I try to bind them, the compiler complains that the values don't match in types as 1 is QAbstractItemModel* and the other is QAbstractItemModel.
Is there a way to get this model binding to work?
This is by no means a working code, because I think the issue is in the embedding of the Models in one another and not in the actual code.
The error:
Cannot assign object of type "MyTreeModel" to property of type
"QAbstractItemModel*" as the former is neither the same as the latter
nor a sub-class of it.
A way to handle the problem is to use QObject * instead of QAbstractItemModel * for the model property. Here is an example :
class TreeModelCompleter : public QCompleter
{
Q_OBJECT
Q_PROPERTY(QString separator READ separator WRITE setSeparator)
Q_PROPERTY(QObject* model READ model WRITE setModel)
public:
explicit TreeModelCompleter(QObject *parent = Q_NULLPTR);
explicit TreeModelCompleter(QAbstractItemModel *model, QObject *parent = Q_NULLPTR);
QString separator() const;
QObject* model();
public slots:
void setSeparator(const QString&);
void setModel(QObject*);
protected:
QStringList splitPath(const QString &path) const override;
QString pathFromIndex(const QModelIndex &index) const override;
private:
QString m_sep;
QAbstractItemModel *m_model;
};
I would like to extend the QFileSystemModel. I would like to have a custom role IsSelectedRole where I can store whether a bool value which stores whether a file got selected in a QML TreeView. More precisely, I don't know how to set the setData and data function of my QFileSystemModel derived class, like where to store the data. I guess making my own tree of bool variables should work, but I hope that there is an easier way.
Qt already has a model that stores item selections: QItemSelectionModel. This model is used by the widget views as well as the QML views. All that's left for you to do is to overlay the data from that model on top of the data from QFileSystemModel, in a viewmodel.
You definitely shouldn't be deriving from QFileSystemModel. The viewmodel maintains the state for a single view only, and should be a proxy that overlays your role on top of the underlying model.
The proxy doesn't have to assume anything about the underlying model, and can work on any model, not just the QFileSystemModel.
For example:
// https://github.com/KubaO/stackoverflown/tree/master/questions/filesystem-model-select-50132273
#include <QtWidgets>
#include <algorithm>
class SelectionProxy : public QIdentityProxyModel {
Q_OBJECT
Q_PROPERTY(QItemSelectionModel* selectionModel
READ selectionModel WRITE setSelectionModel NOTIFY selectionModelChanged)
Q_PROPERTY(QVector<int> roles READ roles WRITE setRoles)
using self_t = SelectionProxy;
using base_t = QIdentityProxyModel;
using model_t = QItemSelectionModel;
QPointer<QItemSelectionModel> m_model;
QVector<QMetaObject::Connection> m_modelConnections;
QVector<int> m_roles{IsSelectedRole};
QModelIndex topLeft() const {
return sourceModel()->index(0, 0);
}
QModelIndex bottomRight() const {
return sourceModel()->index(sourceModel()->rowCount()-1, sourceModel()->columnCount()-1);
}
void onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
auto sel = selected;
sel.merge(deselected, QItemSelectionModel::Select);
for (auto &range : qAsConst(sel)) {
auto topLeft = mapFromSource(range.topLeft());
auto bottomRight = mapFromSource(range.bottomRight());
emit dataChanged(topLeft, bottomRight, m_roles);
}
}
void onModelChanged(QAbstractItemModel *model) {
setSourceModel(model);
}
bool check(const QModelIndex &index, int role) const {
return index.isValid() && m_model && m_roles.contains(role);
}
public:
static constexpr int IsSelectedRole = Qt::UserRole + 44;
SelectionProxy(QObject *parent = {}) : QIdentityProxyModel(parent) {}
QItemSelectionModel *selectionModel() const { return m_model; }
virtual void setSelectionModel(QItemSelectionModel *model) {
if (model == m_model) return;
for (auto &conn : m_modelConnections)
disconnect(conn);
m_model = model;
m_modelConnections = {
connect(m_model, &model_t::selectionChanged, this, &self_t::onSelectionChanged),
connect(m_model, &model_t::modelChanged, this, &self_t::onModelChanged) };
setSourceModel(model->model());
emit selectionModelChanged(m_model);
}
Q_SIGNAL void selectionModelChanged(QItemSelectionModel *);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
if (!check(index, role))
return base_t::data(index, role);
return m_model->isSelected(mapToSource(index));
}
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override {
if (!check(index, role))
return base_t::setData(index, value, role);
using Sel = QItemSelectionModel;
m_model->select(mapToSource(index), value.toBool() ? Sel::SelectCurrent : (Sel::Deselect | Sel::Current));
return true;
}
QVector<int> roles() const { return m_roles; }
void setRoles(QVector<int> roles) {
std::sort(roles.begin(), roles.end());
if (roles == m_roles)
return;
std::swap(roles, m_roles);
if (!m_model)
return;
QVector<int> allRoles;
std::merge(roles.begin(), roles.end(), m_roles.begin(), m_roles.end(), std::back_inserter(allRoles));
emit dataChanged(topLeft(), bottomRight(), allRoles);
}
void setRole(int role) {
setRoles({role});
}
};
#include "main.moc"
A simple demonstrator sets up two views of the filesystem model; the bottom view shows true/false indicating the selection status of the first view.
int main(int argc, char **argv) {
QApplication app{argc, argv};
QWidget win;
QVBoxLayout layout{&win};
QTreeView left, right;
layout.addWidget(&left);
layout.addWidget(&right);
QFileSystemModel model;
SelectionProxy selProxy;
model.setRootPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
auto rootPathIndex = model.index(model.rootPath());
left.setModel(&model);
left.setSelectionMode(QAbstractItemView::MultiSelection);
left.setRootIndex(rootPathIndex);
selProxy.setRole(Qt::DisplayRole);
selProxy.setSelectionModel(left.selectionModel());
right.setModel(&selProxy);
right.setRootIndex(selProxy.mapFromSource(rootPathIndex));
for (int col : {1,2,3})
right.hideColumn(col);
win.show();
return app.exec();
}
Qt 5.8, Windows 10.
Quick Controls 2 application. In QML I have a ListView with a model derived from QAbstractListModel.
In the model I have the following code:
void MediaPlaylistModel::update()
{
beginResetModel();
{
std::unique_lock<std::mutex> lock(m_mutex);
m_ids = m_playlist->itemsIds();
}
endResetModel();
}
Nothing happens in QML view after calling this method: list is not updated.
If I go back and then forward (to the page with the ListView) - it'll contain updated data. Model object instance is the same always.
Am I doing something wrong?
Update #1:
The only methods I override are:
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
Update #2:
C++ model code:
MediaPlaylistModel::MediaPlaylistModel(
QSharedPointer<AbstractMediaPlaylist> playlist,
QObject *parent) :
base_t(parent),
m_playlist(playlist)
{
Q_ASSERT(m_playlist);
connect(playlist.data(), &AbstractMediaPlaylist::changed,
this, &MediaPlaylistModel::update);
update();
}
QHash<int, QByteArray> MediaPlaylistModel::roleNames() const
{
QHash<int, QByteArray> result;
result[IdRole] = "id";
result[TitleRole] = "title";
result[DurationRole] = "duration";
return result;
}
void MediaPlaylistModel::update()
{
Q_ASSERT_SAME_THREAD;
beginResetModel();
{
std::unique_lock<std::mutex> lock(m_mutex);
m_ids = m_playlist->itemsIds();
}
endResetModel();
}
int MediaPlaylistModel::rowCount(
const QModelIndex &parent) const
{
Q_UNUSED(parent);
std::unique_lock<std::mutex> lock(m_mutex);
return static_cast<int>(m_ids.size());
}
QVariant MediaPlaylistModel::data(
const QModelIndex &index,
int role) const
{
auto row = static_cast<size_t>(index.row());
int id = 0;
{
std::unique_lock<std::mutex> lock(m_mutex);
if (row >= m_ids.size())
return QVariant();
id = m_ids[row];
}
if (role == IdRole)
return id;
QVariant result;
auto item = m_playlist->item(id);
switch(role)
{
case Qt::DisplayRole:
case TitleRole:
result = item.title;
break;
case DurationRole:
result = item.duration;
break;
}
return result;
}
QML code:
import QtQuick 2.7
import QtQuick.Controls 2.0
import com.company.application 1.0
Page
{
id : root
property int playlistId
property var playlistApi: App.playlists.playlist(playlistId)
ListView
{
id : playlist
anchors.fill: parent
model: playlistApi.model
delegate: ItemDelegate
{
text: model.title
width: parent.width
onClicked: App.player.play(playlistId, model.id)
}
ScrollIndicator.vertical: ScrollIndicator {}
}
}
Update #3:
When the model is updated (one item added), something strange happens with QML ListView: in addition to fact that it's not updated (and it does not call
MediaPlaylistModel::data to retrieve new items), existing items got damaged. When I click on existed item, it's model.id property is always 0. E.g. at app start its model.id was 24, after one item added its model.id became 0.
Update #4:
App.playlists.playlist(playlistId) returns pointer to this class instance:
class CppQmlPlaylistApi :
public QObject
{
Q_OBJECT
Q_PROPERTY(QObject* model READ model NOTIFY modelChanged)
Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
public:
explicit CppQmlPlaylistApi(
int playlistId,
QWeakPointer<CorePlaylistsManager> playlistsMgr,
QObject *parent = 0);
QObject* model() const;
QString title() const;
void setTitle(const QString &title);
signals:
void modelChanged();
void titleChanged();
void loadPlaylistRequested(int id);
protected slots:
void onPlaylistLoaded(int id);
void onPlaylistRemoved(int id);
protected:
int m_playlistId = 0;
QWeakPointer<CorePlaylistsManager> m_playlistsMgr;
QSharedPointer<QAbstractItemModel> m_model;
};
The model was in non-GUI thread.
I was getting these debug messages (thanks to AlexanderVX for pointing me out):
QObject::connect: Cannot queue arguments of type 'QQmlChangeSet'
(Make sure 'QQmlChangeSet' is registered using qRegisterMetaType().)
Moving the model object to GUI thread fixed the problem.
Your code, as provided, is good. On the QML side, as long as your model is bound, and not dynamically re-created in JS, you should be good too.
ListView {
model: mediaPlaylistModel
}
Problems can arise if you overloaded beginResetModel or endResetModel by accident. For tests purposes, you can try to emit the QAbstractItemModel::modelReset() signal, and see if it changes anything.
It's quite easy to miss something with QAbstractItemModel, resulting in nothing working anymore !
I want to summarize What to do. I have an DataObject class which have members:
QString first;QString last;QList<SubObject*> m_sublist;
I am using QAbstractListModel to this. I can refer first and last to listview but I can't refer to like m_sublist[0].lesson. It gives me error like:
Cannot read property 'lesson' of undefined.
My code:
dataobject.h
class SubObject :public QObject
{
Q_OBJECT
public:
SubObject(const QString &lesson,QObject *parent = 0);
const QString lesson;
private:
// bool operator==(const SubObject* &other) const {
// return other->lesson == lesson;
// }
};
class DataObject :public QObject{
Q_OBJECT
public:
DataObject(const QString &firstName,
const QString &lastName,
const QList<SubObject*> &sublist);
QString first;
QString last;
QList<SubObject*> m_sublist;
};
simplelistmodel.h
class SimpleListModel : public QAbstractListModel {
Q_OBJECT
public:
SimpleListModel(QObject *parent=0);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QHash<int,QByteArray> roleNames() const { return m_roleNames; }
private:
// Q_DISABLE_COPY(SimpleListModel);
QList<DataObject*> m_items;
static const int FirstNameRole;
static const int LastNameRole;
static const int SubListRole;
QHash<int, QByteArray> m_roleNames;
};
simplelistmodel.cpp
const int SimpleListModel::FirstNameRole = Qt::UserRole + 1;
const int SimpleListModel::LastNameRole = Qt::UserRole + 2;
const int SimpleListModel::SubListRole = Qt::UserRole + 3;
SimpleListModel::SimpleListModel(QObject *parent) :
QAbstractListModel(parent) {
// Create dummy data for the list
QList<SubObject*> mysublist;
mysublist.append(new SubObject("MAT"));
mysublist.append(new SubObject("FEN"));
DataObject *first = new DataObject(QString("Arthur"), QString("Dent"),mysublist);
DataObject *second = new DataObject(QString("Ford"), QString("Prefect"),mysublist);
DataObject *third = new DataObject(QString("Zaphod"), QString("Beeblebrox"),mysublist);
m_items.append(first);
m_items.append(second);
m_items.append(third);
// m_roleNames = SimpleListModel::roleNames();
m_roleNames.insert(FirstNameRole, QByteArray("firstName"));
m_roleNames.insert(LastNameRole, QByteArray("lastName"));
m_roleNames.insert(SubListRole, QByteArray("subList"));
}
int SimpleListModel::rowCount(const QModelIndex &) const {
return m_items.size();
}
QVariant SimpleListModel::data(const QModelIndex &index,
int role) const {
if (!index.isValid())
return QVariant(); // Return Null variant if index is invalid
if (index.row() > (m_items.size()-1) )
return QVariant();
DataObject *dobj = m_items.at(index.row());
switch (role) {
case Qt::DisplayRole: // The default display role now displays the first name as well
case FirstNameRole:
return QVariant::fromValue(dobj->first);
case LastNameRole:
return QVariant::fromValue(dobj->last);
case SubListRole:
return QVariant::fromValue(dobj->m_sublist);
default:
return QVariant();
}
}
main.cpp
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
SimpleListModel model;
QQmlContext *classContext = engine.rootContext();
classContext->setContextProperty("absmodel",&model);
engine.load(QUrl(QStringLiteral("qrc:/myuiscript.qml")));
return app.exec(); }
myuiscript.qml
import QtQuick 2.0
import QtQuick.Window 2.0
Window {
id: bgRect
width: 200
height: 200
color: "black"
visible: true
ListView {
id: myListView
anchors.fill: parent
delegate: myDelegate
model: absmodel
}
Component {
id: myDelegate
Item {
width: 200
height: 40
Rectangle {
anchors.fill: parent
anchors.margins: 2
radius: 5
color: "lightsteelblue"
Row {
anchors.verticalCenter: parent.verticalCenter
Text {
text: firstName
color: "black"
font.bold: true
}
Text {
text: subList[0].lesson
color: "black"
}
}
}
}
}
}
I can't find any solution. Virtual data model returns single type of objects. FirsName is a string. I cant refer listview delegate like firstName(rolename). Also LastName is refered like lastName(rolename). But I can't refer subList(roleNames) like sublist[0].lesson.
My target is very simple. I want to refer single type (int,QString ....) to text in delegate by using rolename. I can't refer collection type(QList<SubObject*>) to text in delegate using rolename(subList[0].lesson). How to achive them?
Let's fix it step by step. This line text: subList[0].lesson in QML produce the error message
TypeError: Cannot read property 'lesson' of undefined
That means subList[0] is an undefined object and QML engine cannot read any property, including lesson, from this object. In fact, subList returned from model is a well-defined QList<SubObject*> object, but not subList[0] since QList<SubObject*> is not a QML list. To correctly pass a list from C++ to QML, return a QVariantList instead of QList.
//class DataObject
DataObject(const QString &firstName,
const QString &lastName,
const QVariantList &sublist);
QVariantList m_sublist; //use QVariantList instead of QList<SubObject*>
//---
//SimpleListModel::SimpleListModel
QVariantList mysublist; //use QVariantList instead of QList<SubObject*>
mysublist.append(QVariant::fromValue(new SubObject("MAT", this))); //remember parent
mysublist.append(QVariant::fromValue(new SubObject("FEN", this)));
//...
Now, subList[0] can be accessed in QML, but not subList[0].lesson. To access properties in a C++ class, explicitly define the property with Q_PROPERTY macro.
class SubObject :public QObject
{
Q_OBJECT
Q_PROPERTY(QString lesson READ getLesson NOTIFY lessonChanged)
public:
SubObject(const QString &lesson,QObject *parent = 0):
QObject(parent), m_lesson(lesson){;}
QString getLesson() const {return m_lesson;}
signals:
void lessonChanged();
private:
QString m_lesson;
};
And the QML code works now.