Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Showcase
  4. SqlModels: QSqlTableModel's that are created entirely from QML
Forum Updated to NodeBB v4.3 + New Features

SqlModels: QSqlTableModel's that are created entirely from QML

Scheduled Pinned Locked Moved Unsolved Showcase
sqlqmlmodelthreadqsqltablemodel
13 Posts 2 Posters 5.6k Views 1 Watching
  • 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.
  • LeeiL Offline
    LeeiL Offline
    Leei
    wrote on last edited by Leei
    #1

    Github link
    This project currently contains two SQL Models.

    The first one is called SqlModel, which is a subclass of QSqlTableModel that can create a SQL database, and query against it, all from QML. This model lives on the UI thread.

    The second model included in this project is called SqlThreadedModel. This is a subclass of QAbstractTableModel, which serves as a proxy model for an internal SqlModel. The Internal SqlModel lives in its own thread, separate from the UI thread, and so will not hang the UI thread if queries take a long time. The SqlThreadedModel will listen to the
    SqlModel's signals and will update accordingly.

    This project was made for another one of my projects, but I made them as QML plugins, so more people could get the benefit of using them. If you have any questions or suggestions please feel free!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Thanks for sharing !

      Did you also plan to use it with other database system like PostgreSQL ?

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

      LeeiL 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Thanks for sharing !

        Did you also plan to use it with other database system like PostgreSQL ?

        LeeiL Offline
        LeeiL Offline
        Leei
        wrote on last edited by Leei
        #3

        @SGaist Well I haven't tested it with any other SQL driver, but there is a property for changing that, located here, it defaults to QSQLITE. I only have experience with SQLite, but the queries being made on the database are standard ones.

        If someone has an interest in using it for PostgreSQL, and I do not support a feature, I will surely add it in.

        Thank you for the welcome! :)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The problem I see with that property as string is that you can change the database type but you can't give settings like user name and password.

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

          LeeiL 1 Reply Last reply
          0
          • SGaistS SGaist

            The problem I see with that property as string is that you can change the database type but you can't give settings like user name and password.

            LeeiL Offline
            LeeiL Offline
            Leei
            wrote on last edited by
            #5

            @SGaist Ahh, good catch. I can easily add in some more Q_PROPERTY's to allow users to edit those settings, user name and password are just something I don't use, so they weren't initially included. The QSqlDatabase isn't formed until the Component.onCompleted signal is emitted, so any of the database features can be added in.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Rather than have a property per QSqlDatabase property, I'd go with a small class that contains them so you can re-use it easily for all database backends. That will also make the QML code clearer.

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

              LeeiL 1 Reply Last reply
              0
              • SGaistS SGaist

                Rather than have a property per QSqlDatabase property, I'd go with a small class that contains them so you can re-use it easily for all database backends. That will also make the QML code clearer.

                LeeiL Offline
                LeeiL Offline
                Leei
                wrote on last edited by Leei
                #7

                @SGaist That is exactly what I did! There is a new grouped property called "sqlDatabase". All the database settings are housed in a Database class. Defining the settings looks like:

                SqlModel {
                  id:  sqlModel;
                
                  sqlDatabase {
                    userName: "Fred"
                    password: "123ABC"
                    numericalPrecisionPolicy: Database.HighPrecision
                    port: 192 // Anything except INT_MIN
                    connectionName: "FRED_DB"
                    // ...
                  }
                
                }
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Nice ! What about dbSettings or dbParameters ? It might make it clearer that you are not instantiating a database but just providing information to set things up.

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

                  LeeiL 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Nice ! What about dbSettings or dbParameters ? It might make it clearer that you are not instantiating a database but just providing information to set things up.

                    LeeiL Offline
                    LeeiL Offline
                    Leei
                    wrote on last edited by
                    #9

                    @SGaist Very true. I changed the name to "databaseSettings". Thanks for the input!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You're welcome !

                      Did you consider to submit your work for inclusion in Qt ?

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

                      LeeiL 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        You're welcome !

                        Did you consider to submit your work for inclusion in Qt ?

                        LeeiL Offline
                        LeeiL Offline
                        Leei
                        wrote on last edited by
                        #11

                        @SGaist I did actually! What's the process to go about doing this?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Good !

                          Take a look at the Gerrit introduction wiki page to get started.

                          The module that looks the most suitable (to me) is QtDeclarative. You'll have to refactor your code a bit to follow Qt's coding style guide and naming convention.

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

                          LeeiL 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Good !

                            Take a look at the Gerrit introduction wiki page to get started.

                            The module that looks the most suitable (to me) is QtDeclarative. You'll have to refactor your code a bit to follow Qt's coding style guide and naming convention.

                            LeeiL Offline
                            LeeiL Offline
                            Leei
                            wrote on last edited by
                            #13

                            @SGaist I don't mind refactoring it, it's not very long. I will definitely look into this. Thanks!

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved