qt - QSqlQueryModel in a QML view causes items being shown twice -


thanks jay, problem seems not qml in database.

so minimal erroneous code :

qfile::remove("my.db.sqlite");  qsqldatabase db = qsqldatabase::adddatabase("qsqlite"); db.setdatabasename("my.db.sqlite"); db.open();  qsqlquery drop("drop table list;"); qsqlquery create("create table list (sample_text char(200));"); qsqlquery insert("insert list values (\"some message!\");");  drop.exec(); create.exec(); insert.exec();  db.close(); 

when inspecting my.db.sqlite, :

sqlite> select * list; message! message! 

thanks!

------ old question -------

i in process of learning both qtsql , qml, there room errors. pretty whole problem in title of question. tried make short, self-contained code reproduce :

c++ code :

int main(int argc, char *argv[]) { qguiapplication app(argc, argv); qtquick2applicationviewer viewer;     // uncomment after first launch, deleted test 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 list values (\"message !\")"); create.exec(); create2.exec();  qsqlquerymodel *somesqlmodel = new qsqlquerymodel(); somesqlmodel->setquery("select * 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 "message !" output in list, :

message !

message !

any ? thanks!

okay, found error.

according bug report :

https://bugreports.qt-project.org/browse/qtbug-30205

you should not call exec() if put query in constructor of qsqlquery.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -