4

I was playing around with QSqlQueryModel but I am completely stuck right now. I've searched for a solution the entire day but no luck so far.

What I do got working is that it pulls data from my sqlite database BUT for some reason I can not show it in my listview. It looks like my rolenames do not exist.
I get messages like....ReferenceError: id is not defined..... for each row that I pull out of the database.

I've used an example from: http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML

I've tried both examples but I always get the same problem.
My ccp file looks like this...

#include "artistssqlmodel.h"

const char* ArtistsSqlModel::COLUMN_NAMES[] = {
    "id",
    "word",
    NULL
};

const char* ArtistsSqlModel::SQL_SELECT = "SELECT id, word FROM dictionary LIMIT 5";

ArtistsSqlModel::ArtistsSqlModel(QObject *parent) :
    QSqlQueryModel(parent)
{
    mydb=QSqlDatabase::addDatabase("QSQLITE");
    QString dbPath = "E://mydb.db";
    mydb.setDatabaseName(dbPath);
    connectToDb();

    int idx = 0;
    QHash<int, QByteArray> roleNames;
    while( COLUMN_NAMES[idx]) {
        roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx];
        idx++;
    }
    //roleNames(roleNames);
    refresh();
}

void ArtistsSqlModel::connectToDb()
{
    //QString bla = "Default";

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

void ArtistsSqlModel::refresh()
{
    this->setQuery(SQL_SELECT);
}

QVariant ArtistsSqlModel::data(const QModelIndex &index, int role) const
{
    QVariant value = QSqlQueryModel::data(index, role);
    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;
}

and my qml file looks like this

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
    visible: true
    width: 800
    height: 800

    ListView{
        anchors.fill:parent
        model:artistsModel
        delegate: Item{
            width:parent.width
            height: width/10
            Text {
                id: name
                text: word
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                anchors.fill:parent
            }
        }
    }
}

I hope I provided enough info and I hope somebody knows what I am doing wrong. I'm new with qt and c++ but normally I can solve the issues with some research.
This time I am completely stuck and I need to figure out a way to get the data from my sqlite db into my listsviews.

EDIT: Thanks for the reply here is all the rest of the code (these are the files I've got) The artistssqlmodel.h file

#ifndef ARTISTSSQLMODEL_H
#define ARTISTSSQLMODEL_H

#include <QObject>
#include <QSqlQueryModel>
#include <QDebug>

class ArtistsSqlModel : public QSqlQueryModel
{
    Q_OBJECT
public:
    explicit ArtistsSqlModel(QObject *parent);
    void refresh();
    QVariant data(const QModelIndex &index, int role) const;
    void connectToDb();
signals:
public slots:
private:
    const static char* COLUMN_NAMES[];
    const static char* SQL_SELECT;
    QSqlDatabase mydb;
};


#endif // ARTISTSSQLMODEL_H

and the main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "artistssqlmodel.h"

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

    QQmlContext *context = engine.rootContext();
    ArtistsSqlModel *artistsSqlModel = new ArtistsSqlModel( qApp);

    context->setContextProperty("artistsModel", artistsSqlModel);
    engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));

    return app.exec();
}
2
  • 2
    Please put all of the C++ code into one main.cpp, and the QML code in one main.qml file. This makes it much easier for people to run your code without having to go searching for the pieces that are missing. Commented Jun 24, 2014 at 20:56
  • thanks for your reply i just added all the remaining code i have in my project the .h file and the main.cpp. the main.qml is already posted. Commented Jun 25, 2014 at 0:57

1 Answer 1

2

It looks like my rolenames do not exist

Exactly. You doesn't define the rolenames where QML knows to look for. The wiki where you got the example is out dated. The "new" way to expose the rolenames for a model is reimplementing the QAbstractItemModel::rolenames() method. Move the rolenames definition to it:

QHash<int, QByteArray> ArtistsSqlModel::rolenames() const
{
  int idx = 0;
  QHash<int, QByteArray> roleNames;
  while( COLUMN_NAMES[idx]) {
    roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx];
    idx++;
  }  
  return roleNames;
}

By the way, I suggest you to use the new Qt documentation, if you are not using yet. I think is much clear and organized than the old one.

I hope this help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.