mardi 5 janvier 2016

QML TableView + PostgreSQL database error

I have posrgresql database and I need to display the data in TableView, but I get an error ".../Qt5.5.1/5.5/gcc_64/qml/QtQuick/Controls/Private/BasicTableView.qml:516: Unable to assign [undefined] to int" for every row.

It redirects to BasicTableView.qml:

rowItem.rowIndex = Qt.binding( function() { return model.index });

the problem is here.

As I understand, in some unknown reason it can set indexes for rows. I thought, it's TableView problem, but when I try to open sqlite database, it's ok. I thought, it's postgres problem, but when I display data in ListView, it's ok.

That's the problem:

Sqlite + TableView = ok;

Postgres + ListView = ok;

Postgres + TableView = error.

I tried reinstalling Qt and even reinstall Kubuntu, but problem still exists.

Here is my 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 * from 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 i = 0; i < record().count(); i ++) {
        m_roleNames.insert(Qt::UserRole + i + 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"
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire