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
Forum Updated to NodeBB v4.3 + New Features

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 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.
  • C Offline
    C Offline
    Cleiton Bueno
    wrote on 16 Sept 2016, 20:07 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
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 18 Sept 2016, 05:31 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

      C 1 Reply Last reply 18 Sept 2016, 14:58
      0
      • P p3c0
        18 Sept 2016, 05:31

        @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);
        }
        
        C Offline
        C Offline
        Cleiton Bueno
        wrote on 18 Sept 2016, 14:58 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
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 18 Sept 2016, 16:33 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

          C 1 Reply Last reply 18 Sept 2016, 19:31
          1
          • P p3c0
            18 Sept 2016, 16:33

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

            C Offline
            C Offline
            Cleiton Bueno
            wrote on 18 Sept 2016, 19:31 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
            • P Offline
              P Offline
              p3c0
              Moderators
              wrote on 19 Sept 2016, 05:36 last edited by
              #6

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

              157

              1 Reply Last reply
              0

              3/6

              18 Sept 2016, 14:58

              • Login

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