C++ SqlQueryModel通用模型-角色名称设置不正确
我一直在尝试使用qt文档中的SqlQueryModel通用模型,但似乎无法使它与qml正常工作。 非常类似于我在堆栈溢出上看到的另一个问题,但他们也还没有得到答案 我可以查询数据库,在我的listview中得到正确数量的结果,但是如果我试图通过RoleName访问属性,那么它会说该属性未定义C++ SqlQueryModel通用模型-角色名称设置不正确,c++,qt,sqlite,qml,C++,Qt,Sqlite,Qml,我一直在尝试使用qt文档中的SqlQueryModel通用模型,但似乎无法使它与qml正常工作。 非常类似于我在堆栈溢出上看到的另一个问题,但他们也还没有得到答案 我可以查询数据库,在我的listview中得到正确数量的结果,但是如果我试图通过RoleName访问属性,那么它会说该属性未定义 main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #incl
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)
}
}
}
}
}
main.cpp
#包括
#包括
#包括
#包括“wordmodel.h”
int main(int argc,char*argv[])
{
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
WordModel*wordListModel=新的WordModel(qApp);
engine.rootContext()->setContextProperty(“WLModel”,wordListModel);
发动机负荷(QUrl)(QStringletral(“qrc:///qml/main.qml")));
返回app.exec();
}
SqlQueryModel.h
#ifndef SQLQUERYMODEL_H
#定义SQLQUERYMODEL_H
#包括
#包括
#包括
类SqlQueryModel:公共QSqlQueryModel
{
Q_对象
QVariant数据(常量QModelIndex&index,int-role=Qt::DisplayRole)常量;
内联RoleNameHash roleNames()常量{return*roles;}
公众:
显式SqlQueryModel(QObject*parent=0);
Q_可调用的void setQuery(const-QString&query,const-QSqlDatabase&db=QSqlDatabase());
Q_可调用的void setQuery();
QHash generateRoleNames();
//内联RoleNameHash roleNames()常量{return*roles;}
QHash roleNames()常量{返回角色;}
信号:
void countChanged();
公众时段:
void connectToDb();
void closeDB();
私人:
//qsqlquerysql\u选择;
QHash角色;
qsqlmydb数据库;
};
#endif//SQLQUERYMODEL_H
SqlQueryModel.cpp
#包括“sqlquerymodel.h”
#包括
#包括
#包括
SqlQueryModel::SqlQueryModel(QObject*父对象):
QSqlQueryModel(父级)
{
mydb=QSqlDatabase::addDatabase(“QSQLITE”);
QString dbPath=“E://Qt//sqlite//dictionary.db”;
mydb.setDatabaseName(dbPath);
mydb.setUserName(“管理员”);
mydb.setPassword(“管理员”);
}
void SqlQueryModel::connectToDb()
{
如果(!mydb.open())
{
qDebug()您使用的是哪个版本的Qt
。方法generateRoleNames()
已被弃用,请重新实现roleNames()
而不是generateRoleNames()