Text in ListView from QAbstractListModel
-
I have a subclass of QAbstractListModel:
int JobListModel::rowCount(const QModelIndex &parent) const{ return m_items.size(); } QVariant JobListModel::data(const QModelIndex &index, int role) const{ if(index.isValid()){ return QVariant(); } if(index.row() > (m_items.size() - 1)){ return QVariant(); } Job* jobObj = m_items.at(index.row()); switch (role) { case IdRole: return QVariant::fromValue(jobObj->jobId()); case TitleRole: return QVariant::fromValue(jobObj->jobTitle()); default: return QVariant(); } } QHash<int, QByteArray> JobListModel::roleNames() const{ QHash<int, QByteArray> roles; roles[IdRole] = "jobId"; roles[TitleRole] = "jobTitle"; return roles; }
Then I have a main.cpp:
QApplication app(argc, argv); JobListModel jobModel; jobModel.generateJobs(); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("jobModel", &jobModel); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec();
Jobs.qml:
ListView { id: jobs width: 600 height: 250 model: jobModel delegate: Item { x: 0 width: 80 height: 50 Row { id: row1 x: 0 y: 0 z: 100 width: 600 height: 50 spacing: 10 layoutDirection: Qt.LeftToRight Text { x: 59 text: <<What here?>> font.bold: false anchors.verticalCenter: parent.verticalCenter } } } }
What should I write into the
text:
in the Jobs.qml to show thejobId
andjobTitle
values? The list shows correct number of items but without text. I have tried a lot of combinations...