@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())
[image: 9a0e161f-aa93-4379-b525-58de3fd38efa.png]