Cannot display qsqlquerymodel to a qml list view



  • @ int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QFile f("productList.sqlite");
     
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("localhost");
    db.setDatabaseName("productdb");
    db.setUserName("user");
    db.open();
    QTextStream in(&f);
    QString line;
    if (f.open(QFile::ReadOnly | QFile::Text)) {
    QTextStream in(&f);
    line = in.readLine();
    while(!line.isNull()) {
    QSqlQuery query = db.exec(line);
    qDebug() << line;
    line = in.readLine();
    }
    }
     
    f.close();
     
    QSqlQuery query1 = db.exec&#40;"SELECT * FROM productList"&#41;;
    qDebug() << query1.size();
    QSqlQueryModel *model = new QSqlQueryModel;
     
    model->setQuery("SELECT * FROM productList", db);
    qDebug() << model->record(0);
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("Categorylv1"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Categorylv2"));
     
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Categorylv3"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("productCode"));
    model->setHeaderData(4, Qt::Horizontal, QObject::tr("productDescription"));
    model->setHeaderData(5, Qt::Horizontal, QObject::tr("productPrice"));
     
    QtQuick2ApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("myModel", model);
    viewer.setMainQmlFile&#40;QStringLiteral("qml/basket/main.qml"&#41;);
    viewer.showExpanded();
     
    return app.exec();
    }@
    

    @ import QtQuick 2.0

    Rectangle {
    width: 360
    height: 360
    // color: "black"
     
    Grid {
    id: grid1
    anchors.rightMargin: 0
    anchors.bottomMargin: 0
    anchors.leftMargin: 0
    anchors.topMargin: 0
    anchors.fill: parent
     
    Text {
    id: text1
    text: qsTr("Text")
    anchors.right: parent.right
    anchors.left: parent.left
    anchors.top: parent.top
    font.pixelSize: 12
    }
     
    ListView {
    id: listView1
    model:myModel
    x: 0
    y: 0
    anchors.topMargin: 27
    anchors.fill: parent
    delegate: Item {
     
    Row {
    anchors.margins: 4
    anchors.fill: parent
    spacing: 4;
     
    Text {
    text: productDescription
    width: 150
    }
    }
    }
    }
    }
    }
    

    @

    Hello everyone I try some code with qml and i want to see to a listview some items with productsDescription I tried the example i found to web but the result is an empty list any ideas what I do wrong?


  • Moderators

    Hi,

    I think there are multiple issues here.
    Please have a look at following examples:
    "integrating-sqlite-with-qt-quick":http://stackoverflow.com/questions/16658360/integrating-sqlite-with-qt-quick
    "How_to_use_a_QSqlQueryModel_in_QML":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML
    "qtquickcontrols-calendar-example":https://qt-project.org/doc/qt-5/qtquickcontrols-calendar-example.html

    The best way would be to subclass QSqlQueryModel as explained in the above examples.



  • Consider problem SOLVED i solved it with implementing @class SqlQueryModel: public QSqlQueryModel
    {
    Q_OBJECT
    QHash<int,QByteArray> *hash;
    public:
    explicit SqlQueryModel(QObject * parent) : QSqlQueryModel(parent)
    {
    hash = new QHash<int,QByteArray>;
    hash->insert(Qt::UserRole, QByteArray("someRoleName"));
    hash->insert(Qt::UserRole + 1, QByteArray("otherRoleName"));
    }
    QVariant data(const QModelIndex &index, int role) const
    {
    if(role < Qt::UserRole) {
    return QSqlQueryModel::data(index, role);
    }
    QSqlRecord r = record(index.row());
    return r.value(QString(hash->value(role))).toString();
    }
    inline RoleNameHash roleNames() const { return *hash; }
    };@


  • Moderators

    Wonderful :)
    You can mark post as solved by editing this post title and prepend [solved] so that others know it has been solved.


Log in to reply
 

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