3

What is the best way to return the result of a SQL query to QML? I have this function that executes a query:

bool Storage::setQuery(QString query)
{
    QSqlQuery qsqlQuery;
    bool success = qsqlQuery.exec(query);
    qDebug() << "Error setQuery" << m_sqlDatabase.lastError();

    // What can I do here for store and return my data
    while (success.next()) {
         // do something with data
    }

    return data;
}

Currently this function returns a boolean, but I would like to know how to send the results of a SELECT * for example. What is the best way?

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
simon
  • 1,180
  • 3
  • 12
  • 33
  • You can use [`.value`](http://doc.qt.io/qt-5/qsqlquery.html#value) to get the values one at a time and use them to fill an instance of a class that you are able to export towards the QML environment. If you have no constraints, use sqlite directly from within QML instead. – skypjack Nov 15 '15 at 20:08
  • OK but if I want return à object like a person, this object would have few attributs like age, name, etc. How can I return object list of person to qml ? I don't want use localstorage I need to access all my data to another pc. I don't found any example and I'am really beginer with qml – simon Nov 15 '15 at 22:03

1 Answers1

0

You can implement a Q_INVOKABLE function that returns a QString. Then, you can call that function in your QML.