Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to take down padding from some cells in QTableWidget
Qt 6.11 is out! See what's new in the release blog

How to take down padding from some cells in QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 256 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.
  • R Offline
    R Offline
    rida_zouga
    wrote last edited by
    #1

    ec0d444b-5ce8-4410-a65d-fb4358211c05-spacing_around_button_problem.jpg
    I have the following QTableWidget, with all the cells being instances of QTableWidgetItem Except first cell which holds a QPushButton. Now how can I take down the padding from the cell containing the button and leave padding as is for other cells, since I have this QStyleSheet rule

    QTableWidget::item {
      padding: 5px 10px;
    }
    

    Using PyQt6 and please if the solution involves using a Delegate to explain well how this can be accomplished, since I find it really hard do understand the Delegate class and its methods. Thanks

    JonBJ 1 Reply Last reply
    0
    • R rida_zouga

      ec0d444b-5ce8-4410-a65d-fb4358211c05-spacing_around_button_problem.jpg
      I have the following QTableWidget, with all the cells being instances of QTableWidgetItem Except first cell which holds a QPushButton. Now how can I take down the padding from the cell containing the button and leave padding as is for other cells, since I have this QStyleSheet rule

      QTableWidget::item {
        padding: 5px 10px;
      }
      

      Using PyQt6 and please if the solution involves using a Delegate to explain well how this can be accomplished, since I find it really hard do understand the Delegate class and its methods. Thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by JonB
      #2

      @rida_zouga
      You cannot use stylesheet to achieve any difference between individual cells/columns. QTableWidget::item { applies to all items and cannot be "qualified" to apply only to some.

      Nor is there some setting on a QTableWidgetItem or the underlying setItem(index, value, some-role) to control padding. Padding is just a paint area feature.

      So, yes, you do need to write a delegate and attach it to your table. Although they seem intimidating initially they are actually very simple once you use them.

      Here is complete PyQt6 program showing what you are wanting to achieve:

      import sys
      from PyQt6.QtWidgets import (
          QApplication, QStyledItemDelegate, QStyleOptionViewItem, QTableWidget, QTableWidgetItem)
      from PyQt6.QtGui import QColor
      from PyQt6.QtCore import Qt
      
      class TableDelegate(QStyledItemDelegate):
          def paint(self, painter, option, index):
              option = QStyleOptionViewItem(option)
      
              # for all cells/items *other than* those in column 0
              # adjust the rectangle to draw/paint in for the padding at each side
              if index.column() != 0:
                  option.rect.adjust(10, 5, -10, -5)
      
              # Visualise the actual non-padded area, just so you can see what the effect is
              painter.save()
              painter.fillRect(option.rect, QColor(255, 255, 0, 120))
              painter.restore()
      
              # allow the cell to be painted, with its rectangle having been potentially changed above
              super().paint(painter, option, index)
      
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          tw = QTableWidget(5, 5)
          for row in range(5):
              for col in range(5):
                  tw.setItem(row, col, QTableWidgetItem("Hello"))
          # Create an instance of your delegate class and attach it to the table widget
          tw.setItemDelegate(TableDelegate())
          tw.show()
          sys.exit(app.exec())
      
      

      Screenshot From 2026-09-19 10-52-50.png

      1 Reply Last reply
      4
      • R rida_zouga has marked this topic as solved
      • R Offline
        R Offline
        rida_zouga
        wrote last edited by
        #3

        @JonB Thank you for the response, I get it know, though I have to ask if there is a way now for that padding to happen to text only (for example for 2nd column cells) but background-color of cell contains the entire cell, thus that yellow background reaches the table gridlines.

        JonBJ 1 Reply Last reply
        0
        • R rida_zouga

          @JonB Thank you for the response, I get it know, though I have to ask if there is a way now for that padding to happen to text only (for example for 2nd column cells) but background-color of cell contains the entire cell, thus that yellow background reaches the table gridlines.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote last edited by JonB
          #4

          @rida_zouga
          I am not sure I understand you. Perhaps someone else does? Per the comment, I only put that yellow in so you can see which cells have padding and which do not. (I don't have your icon for column 0 to show.) You would not have the fillRect() code in your actual project. Other than that it just does what your padding: 5px 10px; did to all cells except those in column 0.

          If you know you want it to apply/not apply to a given column where you know the column number (e.g. your "2nd column cells") it already does that. If you are asking for some reason if you can examine the cell content dynamically at runtime (e.g. for "text" or "empty" etc.) then, yes, we can make it do that, but is that what you really mean? Otherwise you would have to spell out just what you intend.

          R 1 Reply Last reply
          2
          • JonBJ JonB

            @rida_zouga
            I am not sure I understand you. Perhaps someone else does? Per the comment, I only put that yellow in so you can see which cells have padding and which do not. (I don't have your icon for column 0 to show.) You would not have the fillRect() code in your actual project. Other than that it just does what your padding: 5px 10px; did to all cells except those in column 0.

            If you know you want it to apply/not apply to a given column where you know the column number (e.g. your "2nd column cells") it already does that. If you are asking for some reason if you can examine the cell content dynamically at runtime (e.g. for "text" or "empty" etc.) then, yes, we can make it do that, but is that what you really mean? Otherwise you would have to spell out just what you intend.

            R Offline
            R Offline
            rida_zouga
            wrote last edited by
            #5

            @JonB 4765f746-785f-4c06-9837-6b7fa7720b46-9a0e161f-aa93-4379-b525-58de3fd38efa.png
            In other words the text stays where it is in that position away from the gridline (shown by red line in image), but the space with white background (shown by surrounding blue line) has the yellow background-color as well. Thus the padding only pushed the text, but the cell entire background-color is yellow. and Thanks once again.

            JonBJ 1 Reply Last reply
            0
            • R rida_zouga

              @JonB 4765f746-785f-4c06-9837-6b7fa7720b46-9a0e161f-aa93-4379-b525-58de3fd38efa.png
              In other words the text stays where it is in that position away from the gridline (shown by red line in image), but the space with white background (shown by surrounding blue line) has the yellow background-color as well. Thus the padding only pushed the text, but the cell entire background-color is yellow. and Thanks once again.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by JonB
              #6

              @rida_zouga
              I am sorry but I still do not know what you are asking. In your descriptions I am afraid I am not sure which bits you want versus which you do not.

              If it helps: you can first fill the whole of the rectangle fillRect(option.rect) to get a background color on the rectangle passed in and then call option.rect.adjust() to reduce the rectangle for the padding. If that is what you wanting to achieve? Just change the order:

              painter.fillRect(option.rect, QColor(255, 255, 0, 120))
              option.rect.adjust(10, 5, -10, -5)
              super().paint(painter, option, index)
              
              1 Reply Last reply
              2
              • R Offline
                R Offline
                rida_zouga
                wrote last edited by
                #7

                THAT'S EXACTLY WHAT I WANT. THANK YOU SO MUCH!

                JonBJ 1 Reply Last reply
                0
                • R rida_zouga

                  THAT'S EXACTLY WHAT I WANT. THANK YOU SO MUCH!

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote last edited by
                  #8

                  @rida_zouga
                  OK :) Now that you are using the color just for the background and filling first you can drop the alpha/opacity, e.g. QColor(255, 255, 0) should be pure yellow. Mine was just for illustration.

                  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
                  • Unsolved