gpt4 book ai didi

c++ - SqlQueryModel 通用模型 - 角色名称设置不正确

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:53:19 26 4
gpt4 key购买 nike

我一直在尝试使用 qt 文档中的 SqlQueryModel 通用模型,但我似乎无法让它与 qml 一起正常工作。与我在 Stack OverFlow 上看到的另一个问题非常相似,但他们也没有得到答案 My QSqlQueryModel doesn't show data in the listview

我可以查询数据库并在我的 ListView 中获得正确数量的结果,但是如果我尝试通过 RoleName 访问一个属性,那么它会说该属性未定义

    main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "wordmodel.h"

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;

WordModel *wordListModel = new WordModel( qApp);

engine.rootContext()->setContextProperty("WLModel", wordListModel);
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));

return app.exec();
}

SqlQueryModel.h

#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H

#include <QSqlQueryModel>
#include <QHash>
#include <QByteArray>

class SqlQueryModel : public QSqlQueryModel
{
Q_OBJECT
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole ) const;
inline RoleNameHash roleNames() const { return *roles; }

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

Q_INVOKABLE void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
Q_INVOKABLE void setQuery();
QHash<int, QByteArray> generateRoleNames();
//inline RoleNameHash roleNames() const { return *roles; }
QHash<int, QByteArray> roleNames() const{return roles;}

signals:
void countChanged();

public slots:
void connectToDb();
void closeDB();

private:
//QSqlQuery SQL_SELECT;
QHash<int, QByteArray> roles;
QSqlDatabase mydb;

};


#endif // SQLQUERYMODEL_H

SqlQueryModel.cpp

#include "sqlquerymodel.h"
#include <QSqlRecord>
#include <QSqlField>
#include <QDebug>

SqlQueryModel::SqlQueryModel(QObject *parent) :
QSqlQueryModel(parent)
{
mydb=QSqlDatabase::addDatabase("QSQLITE");
QString dbPath = "E://Qt//sqlite//dictionary.db";
mydb.setDatabaseName(dbPath);
mydb.setUserName("admin");
mydb.setPassword("admin");
}

void SqlQueryModel::connectToDb()
{
if(!mydb.open())
{
qDebug() << "Database didnt open";
}
else
{
qDebug() << "Your database is open";
}
}

void SqlQueryModel::closeDB()
{
mydb.close();
}

void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)
{
connectToDb();
QSqlQueryModel::setQuery("SELECT * FROM words_en WHERE word LIKE 'young%' LIMIT 10",mydb);
generateRoleNames();
closeDB();
}

void SqlQueryModel::setQuery()
{
connectToDb();
QSqlQueryModel::setQuery("SELECT * FROM words_en WHERE word LIKE 'young%' LIMIT 10");
generateRoleNames();
closeDB();
}


QHash<int, QByteArray> SqlQueryModel::generateRoleNames()
{
roles.clear();
for( int i = 0; i < record().count(); i++) {
roles[Qt::UserRole + i + 1] = record().fieldName(i).toLatin1();
qDebug() << roles[Qt::UserRole + i + 1];

}
return roles;
}

QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
{
qDebug() << "ALALLALALALALALA";
QVariant value = QSqlQueryModel::data(index, role);
qDebug() << 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.2
import QtQuick.Window 2.1

Window {
visible: true
width: 800
height: 800

// here i tried putting the class into a Qml type with the qmlRegisterType()
// but also to no avail

// SqlQueryModel{
// id:something
// Component.onCompleted: {
// something.setQuery()
// }
// }

ListView{
width:parent.width
height:parent.height
model:wordListModel
delegate: Item{
width:parent.width
height: width/10
Text {
id: name
text: word
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
anchors.fill:parent
Component.onCompleted: {
console.log(id)
console.log(word)
}
}
}
}
}

最佳答案

您使用的是哪个版本的Qt。方法 generateRoleNames() 已被弃用,重新实现 roleNames() 而不是 generateRoleNames()

关于c++ - SqlQueryModel 通用模型 - 角色名称设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24427491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com