Answer the question
In order to leave comments, you need to log in
How to display a list of records?
Hello!
How to display a list of records from a database using QML?
There is nothing sensible in Google, errors all the time ...
Start with: ui = new QDeclarativeView;
Next, you need to get data from the database (for example, news):
QSqlQueryModel *sqlModel = new QSqlQueryModel(this);
sqlModel->setQuery("SELECT * FROM news");
QDeclarativeContext *context = ui->rootContext();
context->setContextProperty("sqlModel", sqlModel);
ListView {
anchors.fill: parent
model: sqlModel
delegate: Text {
text: title
}
}
ui->setSource(QUrl("qrc:/mainpage.qml"));
setCentralWidget(ui);
ui->setResizeMode(QDeclarativeView::SizeRootObjectToView);
qrc:/mainpage.qml:74: ReferenceError: Can't find variable: title
text: title
Answer the question
In order to leave comments, you need to log in
How to use a QSqlQueryModel in QML
5 step is deprecated, today instead of these lines in main.cpp we simply write:
ArtistsSqlModel *artistsSqlModel = new ArtistsSqlModel( qApp);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("artistsModel", artistsSqlModel);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question