Important: Please read the 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

    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.

    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


    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?

  • Lifetime Qt Champion

    Might be yes

Log in to reply