Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QSqlQueryModel-覆盖未调用的函数数据_C++_Qt_Model_Qml - Fatal编程技术网

C++ QSqlQueryModel-覆盖未调用的函数数据

C++ QSqlQueryModel-覆盖未调用的函数数据,c++,qt,model,qml,C++,Qt,Model,Qml,我试图用从MySQL数据库检索到的一些信息填充QML中的TableView 我可以使用QSqlQuery连接到数据库,但是当尝试使用QSqlQueryModel时,它不起作用(在最后的图像中获得的结果)。我一直在调试该应用程序,但从未调用该模型的重写函数data和重写函数roleNames 这就是我的模型文件的样子:tableModel.h #ifndef TABLEMODEL_H #define TABLEMODEL_H #include <QObject> #include &

我试图用从MySQL数据库检索到的一些信息填充QML中的TableView

我可以使用QSqlQuery连接到数据库,但是当尝试使用QSqlQueryModel时,它不起作用(在最后的图像中获得的结果)。我一直在调试该应用程序,但从未调用该模型的重写函数
data
和重写函数
roleNames

这就是我的模型文件的样子:tableModel.h

#ifndef TABLEMODEL_H
#define TABLEMODEL_H

#include <QObject>
#include <QtQml/qqml.h>
#include <QSqlQueryModel>
#include <source/database/mySQL/mySqlQueries.h>

class TableModel : public QSqlQueryModel {
    Q_OBJECT
    public:
        // List all the roles that will be used in the TableView
        enum Roles {
            CHROM_ROLE = Qt::UserRole + 1,
            POS_ROLE,
            ID_ROLE,
            REF_ROLE,
            ALT_ROLE,
            QUAL_ROLE
        };

        explicit TableModel(QObject *parent = 0);

        // Override the method that will return the data
        QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const override;

    protected:
        /* hashed table of roles for speakers.
         * The method used in the wilds of the base class QAbstractItemModel,
         * from which inherits the class QSqlQueryModel
         * */
        QHash<int, QByteArray> roleNames() const override;

};

#endif // TABLEMODEL_H
如您所见,每个TableViewColumns的角色与cpp文件中的角色相对应

pro文件包含以下行(此外,SQL查询正在运行):

在main.cpp文件中,我实例化了如下内容:

    QGuiApplication app(argc, argv);
    qmlRegisterType<TableModel>("TableModel", 1, 0, "TableModel");

    TableModel tableModel;

    MySqlConnector db;
    db.connectToDB("localhost", "dbname", "user", "Password");
    db.open();
    // I've tested that the query is working using just QSqlQuery and retrieves info from database 
    tableModel.setQuery(MySQLQueries::queryBodyOfVCF.arg(1), db.db);

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("TableModel", &tableModel);

    const QUrl url(QStringLiteral("qrc:/views/MasterView.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();

我的实现有什么问题吗?我想可能是我误解了某些概念,或者.h文件或.cpp文件中缺少某些内容。

问题是因为您使用了项的名称:,解决方法是更改上下文属性的名称:

engine.rootContext()->setContextProperty(“tableModel”和&tableModel);

另一方面,我建议使用在我概括逻辑的地方实现的模型。

remove
qmlRegisterType(“TableModel”,1,0,“TableModel”)。还提供了一个。还要添加
qDebug(),谢谢<代码>qDebug()嗯,我已经理解了错误。我认为错误是行数,因为在图像中显示为1,但在预期输出中显示为2。现在我明白了这个错误是因为它看起来像TABLEVIEW\u QMLTYPE\u…是我的错,我不清楚。我已经编辑了这个问题,所以很明显这是一个输出示例。我在数据库中插入了另一个条目,因此查询现在在MySQL Workbench中返回2行
qDebug()的工作方式就像一个符咒!非常感谢你。问题是它使用的是类型而不是对象。当然,我将检查您链接的帖子中实现的模型,并更改我的实现。事实上,要找到这样的例子有点困难,所以谢谢你的链接。
import QtQuick.Controls 2.4
import QtQuick.Controls 1.4 as Controls
import QtQuick.Window 2.11
import Qt.labs.qmlmodels 1.0

Window {
    id: root
    visible: true
    width: 640
    height: 480
    title: qsTr("Title")

        Controls.TableView {
            id: tableview
            width: root.width * 0.8
            height: root.height * 0.8
            anchors.centerIn: parent
            clip: true

            Controls.TableViewColumn {
                role: "CHROM_ROLE"    // These roles are roles names coincide with a C ++ model
                title: "#Chrom"
            }

            Controls.TableViewColumn {
                role: "POS_ROLE"    // These roles are roles names coincide with a C ++ model
                title: "Pos."
            }

            Controls.TableViewColumn {
                role: "ID_ROLE"  // These roles are roles names coincide with a C ++ model
                title: "ID"
            }

            Controls.TableViewColumn {
                role: "REF_ROLE" // These roles are roles names coincide with a C ++ model
                title: "Ref."
            }

            Controls.TableViewColumn {
                role: "ALT_ROLE" // These roles are roles names coincide with a C ++ model
                title: "Alt."
            }

            Controls.TableViewColumn {
                role: "QUAL_ROLE" // These roles are roles names coincide with a C ++ model
                title: "Qual."
            }

            // We set the model in the TableView
            model: TableModel
        }


}
QT += sql
    QGuiApplication app(argc, argv);
    qmlRegisterType<TableModel>("TableModel", 1, 0, "TableModel");

    TableModel tableModel;

    MySqlConnector db;
    db.connectToDB("localhost", "dbname", "user", "Password");
    db.open();
    // I've tested that the query is working using just QSqlQuery and retrieves info from database 
    tableModel.setQuery(MySQLQueries::queryBodyOfVCF.arg(1), db.db);

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("TableModel", &tableModel);

    const QUrl url(QStringLiteral("qrc:/views/MasterView.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
  chrom   pos   ref      alt   qual      id
   ctg1     9     A     C, G    100   rs001
   ctg3    12     C        T    100   rs002
model: tableModel