Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Best practices to use CRUD SQL in QML
QtWS25 Last Chance

Best practices to use CRUD SQL in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
sqlqmlqsqlqueryqsqlquerymodel
6 Posts 2 Posters 4.0k Views
  • 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.
  • Cleiton BuenoC Offline
    Cleiton BuenoC Offline
    Cleiton Bueno
    wrote on last edited by
    #1

    I have implemented in a .cpp one QSqlQueryModel where step a name for QmlApplicationViewer.rootContext()->setContextProperty("ALIAS", myObject)

    And main.qml carry in ListView in my model the myObject and runs smoothly.

    But now I need to perform select, insert and update in QML and am trying to use QQuerySql but I can not run the model of the ListView.

    My old implementation is one of this template:
    https://wiki.qt.io/How_to_Use_a_QSqlQueryModel_in_QML

    My new implementation is something like this:
    https://forum.qt.io/topic/51219/solved-qsqlquerymodel-for-qml-tableview/2

    What would be the best/efficient way via QML perform insert, update, delete and select:

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Cleiton-Bueno QSqlQueryModel provides setQuery to which you can pass your query string. Now since you have access of the cpp class which contains QSqlQueryModel you can introduce another Q_INVOKABLE function in that class which can be called directly from QML. And this function will internally call setQuery. Something like:

      void MyClass::executeQuery(QString query) //exposed to QML
      {
          QSqlQuery q;
          q.prepare(query);
          q.exec();
          setQuery(q);
      }
      

      157

      Cleiton BuenoC 1 Reply Last reply
      0
      • p3c0P p3c0

        @Cleiton-Bueno QSqlQueryModel provides setQuery to which you can pass your query string. Now since you have access of the cpp class which contains QSqlQueryModel you can introduce another Q_INVOKABLE function in that class which can be called directly from QML. And this function will internally call setQuery. Something like:

        void MyClass::executeQuery(QString query) //exposed to QML
        {
            QSqlQuery q;
            q.prepare(query);
            q.exec();
            setQuery(q);
        }
        
        Cleiton BuenoC Offline
        Cleiton BuenoC Offline
        Cleiton Bueno
        wrote on last edited by
        #3

        @p3c0 said in Best practices to use CRUD SQL in QML:

        executeQuery

        But I can send a "INSERT ..." or "CREATE ..." or "UPDATE ..." for example via executeQuery ()

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @Cleiton-Bueno Since this function takes a QString you can form a string on QML side and pass to this function. Or if you know the fields and its values on QML side you can create a JS object which can then be converted to QVariantMap on C++ side. This map can then be iterated and your desired query can be created.

          157

          Cleiton BuenoC 1 Reply Last reply
          1
          • p3c0P p3c0

            @Cleiton-Bueno Since this function takes a QString you can form a string on QML side and pass to this function. Or if you know the fields and its values on QML side you can create a JS object which can then be converted to QVariantMap on C++ side. This map can then be iterated and your desired query can be created.

            Cleiton BuenoC Offline
            Cleiton BuenoC Offline
            Cleiton Bueno
            wrote on last edited by
            #5

            @p3c0 Nice! Liked it. I will implement and test.
            With the function this way "generic" I can do a SELECT and use straight into a model: in QML ListView example?

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @Cleiton-Bueno Yes. If the query string is correct it will work.

              157

              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