Important: Please read the Qt Code of Conduct -

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");

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

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

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


    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