<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to take down padding from some cells in QTableWidget]]></title><description><![CDATA[<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/0e629161-1418-4410-b69b-dcf11828f4c4.jpg" alt="ec0d444b-5ce8-4410-a65d-fb4358211c05-spacing_around_button_problem.jpg" class=" img-fluid img-markdown" /><br />
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</p>
<pre><code>QTableWidget::item {
  padding: 5px 10px;
}
</code></pre>
<p dir="auto">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</p>
]]></description><link>https://forum.qt.io/topic/165103/how-to-take-down-padding-from-some-cells-in-qtablewidget</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 07:42:41 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/165103.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 18 Sep 2026 19:33:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to take down padding from some cells in QTableWidget on Sat, 19 Sep 2026 09:54:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rida_zouga">@<bdi>rida_zouga</bdi></a><br />
You cannot use stylesheet to achieve any difference between individual cells/columns.  <code>QTableWidget::item {</code> applies to all items and cannot be "qualified" to apply only to some.</p>
<p dir="auto">Nor is there some setting on a <code>QTableWidgetItem</code> or the underlying <code>setItem(index, value, some-role)</code> to control padding.  Padding is just a paint area feature.</p>
<p dir="auto">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.</p>
<p dir="auto">Here is complete PyQt6 program showing what you are wanting to achieve:</p>
<pre><code class="language-python">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())

</code></pre>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/9a0e161f-aa93-4379-b525-58de3fd38efa.png" alt="Screenshot From 2026-09-19 10-52-50.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/840190</link><guid isPermaLink="true">https://forum.qt.io/post/840190</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 19 Sep 2026 09:54:12 GMT</pubDate></item><item><title><![CDATA[Reply to How to take down padding from some cells in QTableWidget on Sat, 19 Sep 2026 20:42:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rida_zouga">@<bdi>rida_zouga</bdi></a><br />
OK :)  Now that you are using the color just for the background and filling first you can drop the alpha/opacity, e.g. <code>QColor(255, 255, 0)</code> should be pure yellow.  Mine was just for illustration.</p>
]]></description><link>https://forum.qt.io/post/840205</link><guid isPermaLink="true">https://forum.qt.io/post/840205</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 19 Sep 2026 20:42:25 GMT</pubDate></item><item><title><![CDATA[Reply to How to take down padding from some cells in QTableWidget on Sat, 19 Sep 2026 16:45:40 GMT]]></title><description><![CDATA[<p dir="auto">THAT'S EXACTLY WHAT I WANT. THANK YOU SO MUCH!</p>
]]></description><link>https://forum.qt.io/post/840202</link><guid isPermaLink="true">https://forum.qt.io/post/840202</guid><dc:creator><![CDATA[rida_zouga]]></dc:creator><pubDate>Sat, 19 Sep 2026 16:45:40 GMT</pubDate></item><item><title><![CDATA[Reply to How to take down padding from some cells in QTableWidget on Sat, 19 Sep 2026 15:40:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rida_zouga">@<bdi>rida_zouga</bdi></a><br />
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.</p>
<p dir="auto">If it helps: you can first fill the whole of the rectangle <code>fillRect(option.rect)</code> to get a background color on the rectangle passed in and then call <code>option.rect.adjust()</code> to reduce the rectangle for the padding.  If that is what you wanting to achieve?  Just change the order:</p>
<pre><code class="language-python">painter.fillRect(option.rect, QColor(255, 255, 0, 120))
option.rect.adjust(10, 5, -10, -5)
super().paint(painter, option, index)
</code></pre>
]]></description><link>https://forum.qt.io/post/840198</link><guid isPermaLink="true">https://forum.qt.io/post/840198</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 19 Sep 2026 15:40:38 GMT</pubDate></item><item><title><![CDATA[Reply to How to take down padding from some cells in QTableWidget on Sat, 19 Sep 2026 13:35:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> <img src="https://ddgobkiprc33d.cloudfront.net/9dc84e67-86a3-4ecd-8b96-715638d02417.png" alt="4765f746-785f-4c06-9837-6b7fa7720b46-9a0e161f-aa93-4379-b525-58de3fd38efa.png" class=" img-fluid img-markdown" /><br />
In other words the text stays where it is in that position away from the gridline <em>(shown by red line in image)</em>, but the space with white background <em>(shown by surrounding blue line)</em> 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.</p>
]]></description><link>https://forum.qt.io/post/840195</link><guid isPermaLink="true">https://forum.qt.io/post/840195</guid><dc:creator><![CDATA[rida_zouga]]></dc:creator><pubDate>Sat, 19 Sep 2026 13:35:25 GMT</pubDate></item><item><title><![CDATA[Reply to How to take down padding from some cells in QTableWidget on Sat, 19 Sep 2026 12:23:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rida_zouga">@<bdi>rida_zouga</bdi></a><br />
I am not sure I understand you.  Perhaps someone else does?  Per the comment, I only put that yellow in so you can <em>see</em> which cells have padding and which do not.  (I don't have your icon for column 0 to show.)  You would not have the <code>fillRect()</code> code in your actual project.  Other than that it just does what your <code>padding: 5px 10px;</code> did to all cells except those in column 0.</p>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/840193</link><guid isPermaLink="true">https://forum.qt.io/post/840193</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 19 Sep 2026 12:23:48 GMT</pubDate></item><item><title><![CDATA[Reply to How to take down padding from some cells in QTableWidget on Sat, 19 Sep 2026 12:10:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> 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.</p>
]]></description><link>https://forum.qt.io/post/840192</link><guid isPermaLink="true">https://forum.qt.io/post/840192</guid><dc:creator><![CDATA[rida_zouga]]></dc:creator><pubDate>Sat, 19 Sep 2026 12:10:58 GMT</pubDate></item><item><title><![CDATA[Reply to How to take down padding from some cells in QTableWidget on Sat, 19 Sep 2026 09:54:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rida_zouga">@<bdi>rida_zouga</bdi></a><br />
You cannot use stylesheet to achieve any difference between individual cells/columns.  <code>QTableWidget::item {</code> applies to all items and cannot be "qualified" to apply only to some.</p>
<p dir="auto">Nor is there some setting on a <code>QTableWidgetItem</code> or the underlying <code>setItem(index, value, some-role)</code> to control padding.  Padding is just a paint area feature.</p>
<p dir="auto">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.</p>
<p dir="auto">Here is complete PyQt6 program showing what you are wanting to achieve:</p>
<pre><code class="language-python">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())

</code></pre>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/9a0e161f-aa93-4379-b525-58de3fd38efa.png" alt="Screenshot From 2026-09-19 10-52-50.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/840190</link><guid isPermaLink="true">https://forum.qt.io/post/840190</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 19 Sep 2026 09:54:12 GMT</pubDate></item></channel></rss>