Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Custom component and SQL data

    QML and Qt Quick
    3
    8
    1398
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      JasonS last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • ?
        A Former User last edited by

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

        1 Reply Last reply Reply Quote 1
        • J
          JasonS last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • ?
            A Former User last edited by

            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.

            1 Reply Last reply Reply Quote 1
            • J
              JasonS last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • J
                  JasonS last edited by

                  Is this what you are thinking about by chance?

                  https://github.com/JosephMillsAtWork/QmlSql

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Might be yes

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post