Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QSqlQueryModel in a QML view causes items being shown twice

QSqlQueryModel in a QML view causes items being shown twice

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 792 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    doom_Oo7
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved