QSqlQueryModel in a QML view causes items being shown twice



  • I am in the process of learning both QtSql and QML, so there is some room for errors.
    Pretty much my whole problem is in the title of my question.
    I tried to make a short, self-contained code to reproduce it :

    C++ code :
    @
    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    // uncomment after first launch, deleted test to make code short
    // QFile::remove("my.db.sqlite");

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("my.db.sqlite");
    db.open();

    QSqlQuery create("create table List (sample_text char(200))");
    QSqlQuery create2("insert into List values ("message !")");
    create.exec();
    create2.exec();

    QSqlQueryModel *someSqlModel = new QSqlQueryModel();
    someSqlModel->setQuery("SELECT * FROM List");

    QQmlContext *context = viewer.rootContext();
    context->setContextProperty("datamodel", someSqlModel);

    viewer.setMainQmlFile(QStringLiteral("qml/test/main.qml"));
    viewer.showExpanded();

    return app.exec();
    }
    @

    QML Code :

    @ Rectangle {
    ListView {
    width: 200; height: 200
    model: datamodel
    delegate: Row {
    Rectangle {
    width: 100; height: 40
    Text {
    anchors.fill: parent
    text: display
    }
    }
    }
    }
    }

    @
    I should get "message !" as output in my list, but I get :

    message !

    message !

    Any help ? Thanks!


Log in to reply
 

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