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

SqlModels: QSqlTableModel's that are created entirely from QML



  • 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!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Thanks for sharing !

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



  • @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! :)


  • Lifetime Qt Champion

    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.



  • @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.


  • Lifetime Qt Champion

    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.



  • @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"
        // ...
      }
    
    }
    

  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    You're welcome !

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



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


  • Lifetime Qt Champion

    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.



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