QtQuick TableView和QSqlTableModel无法正常工作

时间:2016-05-07 17:46:41

标签: sql qt qml

我正在尝试实现一个QML程序来查看数据库中的数据。我使用QSqlTableModel作为模型,使用TableView进行可视化。关键是TableView根本不调用数据函数,所以我只看到一堆空行(它能够正确计算模型中的行数)。如下所示,与QTableView一起使用时,该模型可以正常工作。所以关键是TableView根本不调用数据。

这是代码。 QML:

import QtQuick 2.6
import QtQuick.Controls 1.5

ApplicationWindow {
    id: root
    visible: true
    width: 1024
    height: 640
    title: qsTr("myApp")

    TableView {
        sortIndicatorVisible: true
        model: sqlData
        anchors.fill: parent

        TableViewColumn {
            role: "Nome"
            title: "Nome"
            width: 200
        }

        TableViewColumn {
            role: "Residenza"
            title: "Residenza"
            width: 200
        }

        TableViewColumn {
            role: "Assicurazione"
            title: "Assicurazione"
            width: 200
        }
    }
}

C ++头文件

#ifndef LISTMODEL_H
#define LISTMODEL_H

#include <QtSql/QSqlTableModel>
#include <QHash>
#include <QByteArray>
#include <QVariant>
#include <QModelIndex>

class ListModel: public QSqlTableModel
{
    Q_OBJECT
    Q_PROPERTY ( QString sqlFilter READ getSQLFilter WRITE setSQLFilter )
private:
    QHash<int, QByteArray> roles;
    QString getSQLFilter() { return filter(); }
    void setSQLFilter(const QString &filter) { setFilter(filter); }

public:
    explicit ListModel(QObject *parent = 0);

public:
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole ) const;
    void generateRoleNames();

#ifdef HAVE_QT5
    virtual QHash<int, QByteArray> roleNames() const{return roles;}
#endif
};

#endif // LISTMODEL_H

实施

#include "listmodel.h"
#include <QtSql/QSqlRecord>
#include <QDebug>
#include <iostream>
#include <QModelIndex>

ListModel::ListModel(QObject *parent) : QSqlTableModel(parent) {

}

QVariant ListModel::data ( const QModelIndex & index, int role ) const
{
    if(index.row() >= rowCount())
        return QVariant();

    if(role < Qt::UserRole)
        return QSqlTableModel::data(index, role);

    QModelIndex modelIndex = this->index(index.row(), role - Qt::UserRole - 1);
    return QSqlTableModel::data(modelIndex, Qt::DisplayRole);
}

void ListModel::generateRoleNames()
{
    roles.clear();

    for (int i = 0; i < columnCount(); i++)
    {
        roles[Qt::UserRole + i + 1] = QVariant(headerData(i, Qt::Horizontal)).toByteArray();
    }
}

的main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
#include <QtSql/QSqlDatabase>
#include <QMessageBox>
#include <listmodel.h>
#include <QQmlContext>
#include <QTableView>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QString dbName(app.applicationDirPath());
    dbName.append("/mydb.sqlite");

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(dbName);
    if (!db.open()) {
        QMessageBox::critical(0, qApp->tr("Cannot open database"), "", QMessageBox::Cancel);
        return 0;
    }

    ListModel dataModel;
    dataModel.setTable("Clienti");
    dataModel.setEditStrategy(QSqlTableModel::OnFieldChange);
    dataModel.select();
    dataModel.generateRoleNames();

    while(dataModel.canFetchMore())
        dataModel.fetchMore();
/*
    QTableView *myView = new QTableView;
    myView->setModel( dataModel );
    myView->show();
*/
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("sqlData", &dataModel);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
    db.close();
}

0 个答案:

没有答案