Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Custom component and SQL data



  • I have a multi-tab QML form that loads a custom component with various controls such as TextEdit boxes, buttons, labels and want to load data from SQLite into the TextEdit boxes to provide update capabilities. I've found a couple things that seem like the would work, but I'm having a difficult time translating the examples into my application. I think the Books example is doing what I want to do, but this example is using a UI file, not a QML form (QML file) like I have and am getting caught up on making the connection between the two. The example I'm referring to is http://doc.qt.io/qt-5/qtsql-books-example.html

    Does anyone have any guidance to help me load data from SQLite and map it to UI objects in QML?



  • Hi! You're lucky, there is already a component to access SQLite databases from QML, see Qt Quick Local Storage QML Types :-)



  • That was my original solution, but I wasn't able to find a way to specify the path and name of the database file.

    https://forum.qt.io/topic/76166/location-of-sqlite-database/5

    Do you know of a way to set the path and file name?



  • Mhh, unfortunatly, setting the filename is not supported. I think (might be wrong) this is because LocalStorage is implemented as a plugin to QML's Javascript engine. The best way to use your SQLite file from QML is probably to write a backend in C++ that accesses the file and exposes one ListModel for each DB table to the QML side.



  • So that's how I got down the path of the Books example. I have a C++ backend creating a QSqlRelationalTableModel and can load the data to a ListView easily by setting the model, but my controls are not in the LIstView format. I was attempting to use a role name of the QSqlRelationalTableModel and set the text of a TextEdit control, but kept running into Unable to assign [undefined] to QString. I found a similar issue that thought they weren't generating their role names correctly so when looking for a solution to that I ran into mapping SQL data to widgets. I was hoping for an easy way to map data to controls instead of writing a bunch of code.


  • Lifetime Qt Champion

    Hi,

    There's also this wiki entry that shows how to use a QSqlQueryModel with QML.

    IIRC there was a project aiming to provide a QML wrapper to Qt's SQL module but I can't find it currently.



  • Is this what you are thinking about by chance?

    https://github.com/JosephMillsAtWork/QmlSql


  • Lifetime Qt Champion

    Might be yes


Log in to reply