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. How to make tableWidget.setItem accept a python list?
Forum Updated to NodeBB v4.3 + New Features

How to make tableWidget.setItem accept a python list?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 430 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.
  • L Offline
    L Offline
    Lord Emperor
    wrote on 17 Apr 2023, 13:31 last edited by Lord Emperor
    #1

    I have a table that I fill with data from a JSON file.
    But because one of the values is an array(that gets converted to a python list) I get an error.
    How can I make it so that a tableWidget.setItem accepts a python list as a value for the cell of the table?
    Previously I simply converted it to a string, and put that string into the QTableWidgetItem.
    But I want to be able to later extract that data from a cell as a list, so that then I can convert it to JSON and save it.
    I read the documentation, and supposedly this could be related to Subclassing, but I could not understand how it works.

    Here is the part of my code:

    for r in range(len(db_account_rows)):
        for c in range(len(db_account_columns)):
            self.tableWidget.setItem(r, c, QtWidgets.QTableWidgetItem(
                    db_bot_accounts[list(db_account_rows)[r]][list(db_account_columns)[c]]
                   # f'{db_bot_accounts[list(db_account_rows)[r]][list(db_account_columns)[c]]}'
                    ))
    

    The commented part is how it was previously.
    And the uncommented part seems to work right until it encounters a python list in the dict that was created from JSON.

    J 1 Reply Last reply 17 Apr 2023, 13:36
    0
    • L Lord Emperor
      17 Apr 2023, 13:31

      I have a table that I fill with data from a JSON file.
      But because one of the values is an array(that gets converted to a python list) I get an error.
      How can I make it so that a tableWidget.setItem accepts a python list as a value for the cell of the table?
      Previously I simply converted it to a string, and put that string into the QTableWidgetItem.
      But I want to be able to later extract that data from a cell as a list, so that then I can convert it to JSON and save it.
      I read the documentation, and supposedly this could be related to Subclassing, but I could not understand how it works.

      Here is the part of my code:

      for r in range(len(db_account_rows)):
          for c in range(len(db_account_columns)):
              self.tableWidget.setItem(r, c, QtWidgets.QTableWidgetItem(
                      db_bot_accounts[list(db_account_rows)[r]][list(db_account_columns)[c]]
                     # f'{db_bot_accounts[list(db_account_rows)[r]][list(db_account_columns)[c]]}'
                      ))
      

      The commented part is how it was previously.
      And the uncommented part seems to work right until it encounters a python list in the dict that was created from JSON.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Apr 2023, 13:36 last edited by
      #2

      @Lord-Emperor QTableWidgetItem has no constructor which takes any list/array.
      You can use https://doc.qt.io/qt-6/qtablewidgetitem.html#setData to set a QVariant, but the question is: what should the table cell do with your list? How should it be shown?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 17 Apr 2023, 13:44
      2
      • J jsulm
        17 Apr 2023, 13:36

        @Lord-Emperor QTableWidgetItem has no constructor which takes any list/array.
        You can use https://doc.qt.io/qt-6/qtablewidgetitem.html#setData to set a QVariant, but the question is: what should the table cell do with your list? How should it be shown?

        L Offline
        L Offline
        Lord Emperor
        wrote on 17 Apr 2023, 13:44 last edited by Lord Emperor
        #3

        @jsulm
        Same as I did before, this list ca be represented simply as text.
        Because I know this list is always going to be small.
        "["item1", "item2", "item3", "item4"]"
        But be stored in the background the same way as it was given to the cell.
        So that when using self.tableWidget.item(_row, _column) I would get the exact same list I put in, and not a string of text.
        How would one achieve that?
        If possible can you please provide an example? I'm really new to QT, and a lot of concepts are way too new for me.

        S 1 Reply Last reply 17 Apr 2023, 19:29
        0
        • L Lord Emperor
          17 Apr 2023, 13:44

          @jsulm
          Same as I did before, this list ca be represented simply as text.
          Because I know this list is always going to be small.
          "["item1", "item2", "item3", "item4"]"
          But be stored in the background the same way as it was given to the cell.
          So that when using self.tableWidget.item(_row, _column) I would get the exact same list I put in, and not a string of text.
          How would one achieve that?
          If possible can you please provide an example? I'm really new to QT, and a lot of concepts are way too new for me.

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Apr 2023, 19:29 last edited by
          #4

          Hi,

          There's currently no real benefit with what you ask.

          QTableWidgetItem does not handle lists the way you want it. You would also need a custom delegate to render that list which, while not complicated, would mean a lot of list to string operation. This mean burning CPUs for nothing. Doing a join when creating the item and a split when you want to retrieve the data is both simple and cost effective.

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

          1 Reply Last reply
          1
          • L Lord Emperor has marked this topic as solved on 18 Apr 2023, 07:28

          1/4

          17 Apr 2023, 13:31

          • Login

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