2016-01-06 61 views
1

我有posrgresql數據庫,我需要在TableView中顯示數據,但我得到一個錯誤「.../Qt5.5.1/5.5/gcc_64/qml/QtQuick/Controls/Private/BasicTableView.qml:516:無法爲每行分配[undefined]爲int「」。QML TableView + PostgreSQL數據庫錯誤

它重定向到BasicTableView.qml:

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

的問題是在這裏。

據我所知,由於某種未知的原因,它可以設置行索引。我想,這是TableView的問題,但是當我嘗試打開sqlite數據庫時,沒關係。我想,這是postgres的問題,但是當我在ListView中顯示數據時,沒關係。

這就是問題所在:

Sqlite + TableView = ok;

Postgres + ListView = ok;

Postgres + TableView = error。

我試着重新安裝Qt,甚至重新安裝Kubuntu,但問題依然存在。

這裏是我的代碼:

的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" 
      } 
     } 
    } 
} 

回答

1

這是發生這種情況的角色「模型」。 你必須重命名它。

我認爲它與視圖的真實模型混在一起...