我有posrgresql数据库,我需要在TableView中显示数据,但是我收到错误“... / Qt5.5.1 / 5.5 / gcc_64 / qml / QtQuick / Controls / Private / BasicTableView.qml: 516:无法为每行将[undefined]分配给int“。
重定向到BasicTableView.qml:
rowItem.rowIndex = Qt.binding( function() { return model.index });
问题出在这里。
据我所知,由于某些未知原因,它可以为行设置索引。我想,这是TableView问题,但是当我尝试打开sqlite数据库时,没关系。我想,这是postgres问题,但是当我在ListView中显示数据时,没关系。
这就是问题所在:
Sqlite + TableView = ok;
Postgres + ListView = ok;
Postgres + TableView =错误。
我尝试重新安装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"
}
}
}
}
答案 0 :(得分:1)
这是角色&#34;模型&#34;这让这件事发生了。 你必须重命名它。
我认为它与视图的真实模型混淆了......