Cannot display qsqlquerymodel to a qml list view

  • @ int main(int argc, char *argv[])
    QGuiApplication app(argc, argv);

    QFile f("productList.sqlite");
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    QTextStream in(&f);
    QString line;
    if ( | QFile::Text)) {
    QTextStream in(&f);
    line = in.readLine();
    while(!line.isNull()) {
    QSqlQuery query = db.exec(line);
    qDebug() << line;
    line = in.readLine();
    QSqlQuery query1 = db.exec&#40;"SELECT * FROM productList"&#41;;
    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);
    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
    font.pixelSize: 12
    ListView {
    id: listView1
    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?

  • Moderators


    I think there are multiple issues here.
    Please have a look at following examples:

    The best way would be to subclass QSqlQueryModel as explained in the above examples.

  • Consider problem SOLVED i solved it with implementing @class SqlQueryModel: public QSqlQueryModel
    QHash<int,QByteArray> *hash;
    explicit SqlQueryModel(QObject * parent) : QSqlQueryModel(parent)
    hash = new QHash<int,QByteArray>;
    hash->insert(Qt::UserRole, QByteArray("someRoleName"));
    hash->insert(Qt::UserRole + 1, QByteArray("otherRoleName"));
    QVariant data(const QModelIndex &index, int role) const
    if(role < Qt::UserRole) {
    return QSqlQueryModel::data(index, role);
    QSqlRecord r = record(index.row());
    return r.value(QString(hash->value(role))).toString();
    inline RoleNameHash roleNames() const { return *hash; }

  • Moderators

    Wonderful :)
    You can mark post as solved by editing this post title and prepend [solved] so that others know it has been solved.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.