Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Centered and sortable icons in QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Centered and sortable icons in QTableWidget

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 2.0k Views 2 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.
  • Q Offline
    Q Offline
    Qoissi
    wrote on last edited by
    #1

    Hello,

    I'm desperately looking for a solution to be able to display icons in a QTableWidget column so that the icons are BOTH centered and sortable.

    Centering looks not possible via a normal QTableWidgetItem(QIcon()) so I rather use a QLabel and set the item via setCellWidget() method.

    iconItem = QtWidgets.QLabel()
    iconItem.setPixmap(iconPixmap)
    iconItem.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
    myTableWidget.setCellWidget(x, y, iconItem)
    

    But I didn't find any solution to sort such QLabel items in a QTableWidget column.

    Sorting QTableWidgetItem icons is relatively easy by subclassing QTableWidgetItem, but here I didn't find any solution to center them, all the icons are stuck on the left side of the cells.

    iconItem = QtWidgets.QTableWidgetItem()   # Sorting is easy by subclassing
    iconItem.setIcon(QtGui.QIcon(iconPixmap))
    iconItem.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)   # Has NO effect on icon centering
    myTableWidget.setItem(x, y, iconItem)
    

    Note my table has many other columns, with text and numbers.

    If anyone has an idea I would be very grateful, thanks a lot for your help.

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, I had a similar problem with QIcons in my QTableWidget, I solved it by using a QToolButton as a "middleman", say like this:
      tb = QtWidgets.QToolButton()
      tb.setIcon(QtGui.QIcon(iconPixmap))
      myTableWidget.setCellWidget(x, y, tb)

      Q 1 Reply Last reply
      0
      • hskoglundH hskoglund

        Hi, I had a similar problem with QIcons in my QTableWidget, I solved it by using a QToolButton as a "middleman", say like this:
        tb = QtWidgets.QToolButton()
        tb.setIcon(QtGui.QIcon(iconPixmap))
        myTableWidget.setCellWidget(x, y, tb)

        Q Offline
        Q Offline
        Qoissi
        wrote on last edited by
        #3

        @hskoglund thanks but your solution looks equivalent to the QLabel method I use to center, and unfortunately by this way the column becomes unsortable. I'm looking for a solution to be able to center AND sort...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Another solution could be to implement your own QStyledItemDelegate to paint your icon the way you want it. That way the sorting will be available.

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

          Q 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Another solution could be to implement your own QStyledItemDelegate to paint your icon the way you want it. That way the sorting will be available.

            Q Offline
            Q Offline
            Qoissi
            wrote on last edited by
            #5

            @SGaist Thank you very much for that suggestion! Looks promising, however this looks a bit complicated, the doc is quite obscure and I couldn't find any obvious PyQT examples for such technic. If you would have some sample usages to share that could help me that would be super great. Thanks again for your help.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Did you check the Star Delegate example ? It's not yet ported to Python but is a pretty simple implementation to follow.

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

              Q 1 Reply Last reply
              1
              • SGaistS SGaist

                Did you check the Star Delegate example ? It's not yet ported to Python but is a pretty simple implementation to follow.

                Q Offline
                Q Offline
                Qoissi
                wrote on last edited by
                #7

                @SGaist Thanks again, wasn't that complicated indeed, now works like a charm! Problem solved.

                1 Reply Last reply
                1

                • Login

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