How to remove padding of QCheckBox in a QTreeWidget cell

240 Views Asked by At

I've subclassed QWidget and used setItemWidget to set it as a cell widget. Here's the code for the subclass

from PyQt5.QtWidgets import QWidget, QHBoxLayout, QCheckBox, QLabel
from PyQt5.QtCore import Qt


class QFrozenWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        checkbox = QCheckBox()
        label = QLabel()
        label.setText("▲")
        layout = QHBoxLayout(self)
        layout.setAlignment(Qt.AlignLeft)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(checkbox)
        layout.addWidget(label)

This is how I set the widget to the cell, treeWidget_AddressTable was already declared in the ui file, it's normal QTreeWidget

current_row = QTreeWidgetItem()
frozen_widget = QFrozenWidget()
self.treeWidget_AddressTable.addTopLevelItem(current_row)
self.treeWidget_AddressTable.setItemWidget(current_row,FROZEN_COL,frozen_widget)

qt misaligned checkbox

Even with Qt.AlignLeft or spacing the checkbox has a gap between itself and the leftmost side of the freeze column. How do I remove it? I've subclassed QWidget as an attempt, I'm open to any kind of neat solution to this

Have a nice day people

1

There are 1 best solutions below

0
Korcan Karaokçu On

As stated in the comments, setRootIsDecorated(False) works but it also makes it impossible to expand items for the user. I've found out that the true answer to this was to use setIndentation() with the desired parameter. I'll be accepting this answer, thanks for the help