Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Cannot display qsqlquerymodel to a qml list view

$
0
0
    int main(int argc, char *argv[])     {     QGuiApplication app(argc, argv);                 QFile f("productList.sqlite");           QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");     db.setHostName("localhost");     db.setDatabaseName("productdb");     db.setUserName("user");     db.open();     QTextStream in(&f);     QString line;     if (f.open(QFile::ReadOnly | QFile::Text)) {     QTextStream in(&f);     line = in.readLine();     while(!line.isNull()) {     QSqlQuery query = db.exec(line);     qDebug() << line;     line = in.readLine();     }     }           f.close();           QSqlQuery query1 = db.exec("SELECT * FROM productList");     qDebug() << query1.size();     QSqlQueryModel *model = new QSqlQueryModel;           model->setQuery("SELECT * FROM productList", db);     qDebug() << model->record(0);     model->setHeaderData(0, Qt::Horizontal, QObject::tr("Categorylv1"));     model->setHeaderData(1, Qt::Horizontal, QObject::tr("Categorylv2"));           model->setHeaderData(2, Qt::Horizontal, QObject::tr("Categorylv3"));     model->setHeaderData(3, Qt::Horizontal, QObject::tr("productCode"));     model->setHeaderData(4, Qt::Horizontal, QObject::tr("productDescription"));     model->setHeaderData(5, Qt::Horizontal, QObject::tr("productPrice"));           QtQuick2ApplicationViewer viewer;     viewer.rootContext()->setContextProperty("myModel", model);     viewer.setMainQmlFile(QStringLiteral("qml/basket/main.qml"));     viewer.showExpanded();           return app.exec();     }     import QtQuick 2.0           Rectangle {     width: 360     height: 360     // color: "black"           Grid {     id: grid1     anchors.rightMargin: 0     anchors.bottomMargin: 0     anchors.leftMargin: 0     anchors.topMargin: 0     anchors.fill: parent           Text {     id: text1     text: qsTr("Text")     anchors.right: parent.right     anchors.left: parent.left     anchors.top: parent.top     font.pixelSize: 12     }           ListView {     id: listView1     model:myModel     x: 0     y: 0     anchors.topMargin: 27     anchors.fill: parent     delegate: Item {           Row {     anchors.margins: 4     anchors.fill: parent     spacing: 4;           Text {     text: productDescription     width: 150     }     }     }     }     }     } Hello everyone I try some code with qml and i want to see to a listview some items with productsDescription I tried the example i found to web but the result is an empty list any ideas what I do wrong?

Viewing all articles
Browse latest Browse all 4972

Trending Articles