Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget, how to pick up changes?
Forum Updated to NodeBB v4.3 + New Features

QTableWidget, how to pick up changes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 520 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I have a QTableWidget, it contains fields form a database. I'm sure there is a better way to do what I am trying to do....

    I have the following JSON:

         {"cid":"demoTable"
     ,"columns":[{"intPri":{"align":"right"
                             ,"hdg":"ID"
                           ,"width":80}}
           ,{"vcFirstName":{"align":"left"
                             ,"hdg":"First Name"
                           ,"width":100}}
             ,{"vcSurName":{"align":"left"
                             ,"hdg":"Surname"
                           ,"width":100}}
             ,{"vcAddress":{"align":"left"
                             ,"hdg":"Address"
                           ,"width":340}}
                 ,{"vcSex":{"align":"center"
                             ,"hdg":"Sex"
                           ,"width":80}}]
      ,"alias":"s1"
     ,"module":"mdSQL"
    ,"command":"query"
        ,"key":"intPri"
    ,"handler":"simon.js@setupTable"
        ,"sql":"SELECT"
             + " intPri, vcFirstName, vcSurName, vcAddress, vcSex "
             + "FROM"
             + " test.tblForm ORDER BY vcSurName, vcFirstName"}
    

    My code processes the JSON and creates the table then populates it with the results after executing the SQL query.

    When I create the table an initially populate it I call:

     const QSignalBlocker blocker(*pobjTable);
    

    To prevent any signals. I then create a property with keyed items that contains the original data, each item in the JSON is keyed using the primary key (intPri) and the data field name, the value associate with this is the value as read from the database.

    When I edit the data in the table, a signal is emitted when the cellChanged signal is emitted.

    This is where I am stuck, is it possible for me to get the data item from the table using the primary key and field name ?

    I can only see item by row and column which doesn't help me since rows and columns can change. If there is a better way to check for changes can someone please educate me ?

    Kind Regards,
    Sy

    JonBJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I have a QTableWidget, it contains fields form a database. I'm sure there is a better way to do what I am trying to do....

      I have the following JSON:

           {"cid":"demoTable"
       ,"columns":[{"intPri":{"align":"right"
                               ,"hdg":"ID"
                             ,"width":80}}
             ,{"vcFirstName":{"align":"left"
                               ,"hdg":"First Name"
                             ,"width":100}}
               ,{"vcSurName":{"align":"left"
                               ,"hdg":"Surname"
                             ,"width":100}}
               ,{"vcAddress":{"align":"left"
                               ,"hdg":"Address"
                             ,"width":340}}
                   ,{"vcSex":{"align":"center"
                               ,"hdg":"Sex"
                             ,"width":80}}]
        ,"alias":"s1"
       ,"module":"mdSQL"
      ,"command":"query"
          ,"key":"intPri"
      ,"handler":"simon.js@setupTable"
          ,"sql":"SELECT"
               + " intPri, vcFirstName, vcSurName, vcAddress, vcSex "
               + "FROM"
               + " test.tblForm ORDER BY vcSurName, vcFirstName"}
      

      My code processes the JSON and creates the table then populates it with the results after executing the SQL query.

      When I create the table an initially populate it I call:

       const QSignalBlocker blocker(*pobjTable);
      

      To prevent any signals. I then create a property with keyed items that contains the original data, each item in the JSON is keyed using the primary key (intPri) and the data field name, the value associate with this is the value as read from the database.

      When I edit the data in the table, a signal is emitted when the cellChanged signal is emitted.

      This is where I am stuck, is it possible for me to get the data item from the table using the primary key and field name ?

      I can only see item by row and column which doesn't help me since rows and columns can change. If there is a better way to check for changes can someone please educate me ?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @SPlatten
      I don't follow: when you have a row and column you can look in the model at that index and retrieve whatever value you want/need. If you e.g. put the key field value in the row somewhere you can get at that.

      You realise QTableWidget is only a convenience widget aimed at simple cases? You have no control over the model it uses. Knowing you and your requirements, sooner or later wouldn't you be much better off with a QTableView and your own model, where you can do much more?

      If all you want to do is access a SQL database you would be better of with a QSql... model. You might also impose a proxy model to add further stuff if required. QTableWidget model is not ideal.

      SPlattenS 1 Reply Last reply
      0
      • JonBJ JonB

        @SPlatten
        I don't follow: when you have a row and column you can look in the model at that index and retrieve whatever value you want/need. If you e.g. put the key field value in the row somewhere you can get at that.

        You realise QTableWidget is only a convenience widget aimed at simple cases? You have no control over the model it uses. Knowing you and your requirements, sooner or later wouldn't you be much better off with a QTableView and your own model, where you can do much more?

        If all you want to do is access a SQL database you would be better of with a QSql... model. You might also impose a proxy model to add further stuff if required. QTableWidget model is not ideal.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #3

        @JonB , I originally started out with using row and column but they can't be used when sorting is enabled, because the data you think they point to changes.

        QTableWidget has a function which has several variants item one of these allows you to pass the row and column: https://doc.qt.io/qt-6/qtablewidget.html#item

        If you are trying to locate a specific cell in the table, it can move around depending on the sorting if enabled....

        Kind Regards,
        Sy

        JonBJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @JonB , I originally started out with using row and column but they can't be used when sorting is enabled, because the data you think they point to changes.

          QTableWidget has a function which has several variants item one of these allows you to pass the row and column: https://doc.qt.io/qt-6/qtablewidget.html#item

          If you are trying to locate a specific cell in the table, it can move around depending on the sorting if enabled....

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @SPlatten
          What can I say? Don't use QTableWidget. With your own model (and view) you can do things like access the row, column in the base, unsorted model, assuming the sorting is done with a proxy model like QSortFilterProxyModel. Anything that can be done in a QTableWidget can be done with QTableView + your own model, and only gives more flexibility; the reverse is not true. I have indicated that given your question/requirement history I would be "surprised" if QTableWidget is the best choice for you.

          SPlattenS 1 Reply Last reply
          2
          • JonBJ JonB

            @SPlatten
            What can I say? Don't use QTableWidget. With your own model (and view) you can do things like access the row, column in the base, unsorted model, assuming the sorting is done with a proxy model like QSortFilterProxyModel. Anything that can be done in a QTableWidget can be done with QTableView + your own model, and only gives more flexibility; the reverse is not true. I have indicated that given your question/requirement history I would be "surprised" if QTableWidget is the best choice for you.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @JonB , are they're any helpful tutorials or guides that could help me to do this?

            Kind Regards,
            Sy

            JonBJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JonB , are they're any helpful tutorials or guides that could help me to do this?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @SPlatten
              I'm not one for tutorials or guides. I just do and read and see how it goes.

              All of this is just Qt's model/view architecture. Have you at least read https://doc.qt.io/qt-6/model-view-programming.html and https://doc.qt.io/qt-6/modelview.html? What about https://doc.qt.io/qt-6/qtwidgets-itemviews-customsortfiltermodel-example.html for understand how filtering/sorting a model works (you may only need to use existing QSortFilterProxyModel).

              A QTableWidget is merely derived from a QTableView, and it adds in its own "hidden" model. You can do the UI stuff from QTableView and provide your own model to it for the backend.

              SPlattenS 1 Reply Last reply
              0
              • JonBJ JonB

                @SPlatten
                I'm not one for tutorials or guides. I just do and read and see how it goes.

                All of this is just Qt's model/view architecture. Have you at least read https://doc.qt.io/qt-6/model-view-programming.html and https://doc.qt.io/qt-6/modelview.html? What about https://doc.qt.io/qt-6/qtwidgets-itemviews-customsortfiltermodel-example.html for understand how filtering/sorting a model works (you may only need to use existing QSortFilterProxyModel).

                A QTableWidget is merely derived from a QTableView, and it adds in its own "hidden" model. You can do the UI stuff from QTableView and provide your own model to it for the backend.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #7

                @JonB , thank you, I will take a look now.

                Kind Regards,
                Sy

                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