PDA

View Full Version : Cannot display qsqlquerymodel to a qml list view



ironexmaiden
3rd August 2014, 12:43
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);


QFile f("productList.sqlite");

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("localhost");
db.setDatabaseName("productdb");
db.setUserName("user");
db.open();
QTextStream in(&f);
QString line;
if (f.open(QFile::ReadOnly | QFile::Text)) {
QTextStream in(&f);
line = in.readLine();
while(!line.isNull()) {
QSqlQuery query = db.exec(line);
qDebug() << line;
line = in.readLine();
}
}

f.close();

QSqlQuery query1 = db.exec("SELECT * FROM productList");
qDebug() << query1.size();
QSqlQueryModel *model = new QSqlQueryModel;

model->setQuery("SELECT * FROM productList", db);
qDebug() << model->record(0);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Categorylv1"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Categorylv2"));

model->setHeaderData(2, Qt::Horizontal, QObject::tr("Categorylv3"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("productCode"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("productDescription"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("productPrice"));

QtQuick2ApplicationViewer viewer;
viewer.rootContext()->setContextProperty("myModel", model);
viewer.setMainQmlFile(QStringLiteral("qml/basket/main.qml"));
viewer.showExpanded();

return app.exec();
}



import QtQuick 2.0

Rectangle {
width: 360
height: 360
// color: "black"

Grid {
id: grid1
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
anchors.fill: parent

Text {
id: text1
text: qsTr("Text")
anchors.right: parent.right
anchors.left: parent.left
anchors.top: parent.top
font.pixelSize: 12
}

ListView {
id: listView1
model:myModel
x: 0
y: 0
anchors.topMargin: 27
anchors.fill: parent
delegate: Item {

Row {
anchors.margins: 4
anchors.fill: parent
spacing: 4;

Text {
text: productDescription
width: 150
}
}
}
}
}
}


Hello everyone I try some code with qml and i want to see to a listview some items with productsDescription I tried the example i found to web but the result is an empty list :( any ideas what I do wrong?

anda_skoa
3rd August 2014, 13:35
Do you get the delegate instantiated but without data or does not delegate get instantiated at all?

Cheers,
_

ironexmaiden
3rd August 2014, 19:19
Consider problem SOLVED i solved it with implementing


class SqlQueryModel: public QSqlQueryModel
{
Q_OBJECT
QHash<int,QByteArray> *hash;
public:
explicit SqlQueryModel(QObject * parent) : QSqlQueryModel(parent)
{
hash = new QHash<int,QByteArray>;
hash->insert(Qt::UserRole, QByteArray("someRoleName"));
hash->insert(Qt::UserRole + 1, QByteArray("otherRoleName"));
}
QVariant data(const QModelIndex &index, int role) const
{
if(role < Qt::UserRole) {
return QSqlQueryModel::data(index, role);
}
QSqlRecord r = record(index.row());
return r.value(QString(hash->value(role))).toString();
}
inline RoleNameHash roleNames() const { return *hash; }
};

grol
1st September 2014, 19:55
Hey there,

I am trying the same since some days now. Can you maybe post the whole project as a zip file?
Or post the main.cpp and how you implement it?

Would be awsome...

inline RoleNameHash roleNames() const { return *hash; }

does not name a type, is one of the errors i get...

anda_skoa
1st September 2014, 21:08
Just implement this method http://qt-project.org/doc/qt-5/qabstractitemmodel.html#roleNames

ironexmaiden probably had a typedef for the return type.

Cheers,
_

grol
1st September 2014, 21:34
solved like following. thanks!


#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H

#include <QtSql>
#include <QAbstractItemModel>

class SqlQueryModel: public QSqlQueryModel
{
Q_OBJECT
QHash<int,QByteArray> *hash;
public:
explicit SqlQueryModel() : QSqlQueryModel()
{
hash = new QHash<int,QByteArray>;
hash->insert(Qt::UserRole, QByteArray("name"));
hash->insert(Qt::UserRole + 1, QByteArray("title"));
}
QVariant data(const QModelIndex &index, int role) const
{
if(role < Qt::UserRole) {
return QSqlQueryModel::data(index, role);
}
QSqlRecord r = record(index.row());
return r.value(QString(hash->value(role))).toString();
}
inline QHash<int, QByteArray> roleNames() const { return *hash; }
};

#endif // SQLQUERYMODEL_H

anda_skoa
2nd September 2014, 06:16
You can just have the hash on the stack, no need for allocating it on the heap using new.


QHash<int,QByteArray> hash;


Currently you leak it.

Cheers,
_

grol
2nd September 2014, 10:20
you mean like following?


class SqlQueryModel: public QSqlQueryModel
{
Q_OBJECT
QHash<int,QByteArray> hash;
public:
explicit SqlQueryModel() : QSqlQueryModel()
{
//hash = new QHash<int,QByteArray>;
hash.insert(Qt::UserRole, QByteArray("name"));
hash.insert(Qt::UserRole + 1, QByteArray("title"));
}
QVariant data(const QModelIndex &index, int role) const
{
if(role < Qt::UserRole) {
return QSqlQueryModel::data(index, role);
}
QSqlRecord r = record(index.row());
return r.value(QString(hash.value(role))).toString();
}
inline QHash<int, QByteArray> roleNames() const { return hash; }
};

anda_skoa
2nd September 2014, 11:45
Exactly!

Cheers,
_