c++ - QML TableView + PostgreSQL database error -
i have posrgresql database , need display data in tableview, error ".../qt5.5.1/5.5/gcc_64/qml/qtquick/controls/private/basictableview.qml:516: unable assign [undefined] int" every row.
it redirects basictableview.qml:
rowitem.rowindex = qt.binding( function() { return model.index }); the problem here.
as understand, in unknown reason can set indexes rows. thought, it's tableview problem, when try open sqlite database, it's ok. thought, it's postgres problem, when display data in listview, it's ok.
that's problem:
sqlite + tableview = ok;
postgres + listview = ok;
postgres + tableview = error.
i tried reinstalling qt , reinstall kubuntu, problem still exists.
here code:
main.cpp
#include <qguiapplication> #include <qqmlapplicationengine> #include <qsqldatabase> #include <qsqlquerymodel> #include <qsqlquery> #include <qdebug> #include <qquickview> #include <qqmlcontext> #include <qsqltablemodel> #include <qstring> #include "customsqlmodel.h" int main(int argc, char *argv[]) { qguiapplication app(argc, argv); qquickview view; qsqldatabase db = qsqldatabase::adddatabase("qpsql"); db.sethostname("127.0.0.1"); db.setdatabasename("stores"); db.setusername("postgres"); db.setpassword("11111111"); bool ok = db.open(); customsqlmodel *model = new customsqlmodel(); model->setquery("select * product"); view.rootcontext()->setcontextproperty("lolmodel", model); view.setsource(qurl(qstringliteral("qrc:/main.qml"))); view.show(); qsqlquery query; return app.exec(); } customsqlmodel.h
#pragma once #include <qsqlquerymodel> #include <qvariant> class customsqlmodel : public qsqlquerymodel { q_object public: explicit customsqlmodel(qobject *parent = 0); void setquery(const qstring &query, const qsqldatabase &db = qsqldatabase()); void setquery(const qsqlquery &query); qvariant data(const qmodelindex &index, int role) const; qhash<int, qbytearray> rolenames() const { return m_rolenames; } private: void generaterolenames(); qhash<int, qbytearray> m_rolenames; }; customsqlmodel.cpp
#include "customsqlmodel.h" #include <qsqlrecord> #include <qsqlquery> customsqlmodel::customsqlmodel(qobject *parent) : qsqlquerymodel(parent) { } void customsqlmodel::setquery(const qstring &query, const qsqldatabase &db) { qsqlquerymodel::setquery(query, db); generaterolenames(); } void customsqlmodel::setquery(const qsqlquery & query) { qsqlquerymodel::setquery(query); generaterolenames(); } void customsqlmodel::generaterolenames() { m_rolenames.clear(); for(int = 0; < record().count(); ++) { m_rolenames.insert(qt::userrole + + 1, record().fieldname(i).toutf8()); } } qvariant customsqlmodel::data(const qmodelindex &index, int role) const { qvariant value; if(role < qt::userrole) { value = qsqlquerymodel::data(index, role); } else { int columnidx = role - qt::userrole - 1; qmodelindex modelindex = this->index(index.row(), columnidx); value = qsqlquerymodel::data(modelindex, qt::displayrole); } return value; } main.qml
import qtquick 2.3 import qtquick.controls 1.4 rectangle { visible: true width: 800 height: 600 rectangle { id: root anchors.fill: parent tableview { id: studentview anchors { top: parent.top left: parent.left right: parent.right bottom: parent.bottom bottommargin: 100 } model: lolmodel tableviewcolumn { role: "manufacturer" title: "manufacturer" } tableviewcolumn { role: "model" title: "model" } tableviewcolumn { role: "guarantee" title: "guarantee" } } } }
it role "model" makes happen. have rename it.
i suppose mess somewhere real model of view...
Comments
Post a Comment