Home > Net >  Pysides6 columnSpan backwards from setting
Pysides6 columnSpan backwards from setting

Time:03-31

I'm trying to create an app with a file browser that is 1/5 the width of a tab pane.

Why is the layout columnSpan backwards from what I set it?

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("My App")

        layout = QGridLayout()

        layout.addWidget(FileTree(), 0, 0, 1, 1)
        layout.addWidget(Tabs(), 0, 1, 1, 5)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

column widths

CodePudding user response:

The row and column span refer to the grid cells of the layout, not the ratio of each row or column.

Setting a span means that the widget (or layout) will occupy that amount of cells, not that it will be 5 times bigger, as the size of each cell depends on the size (and hint) of its content. Since you have nothing else in the other 4 columns, those cells are actually empty.

It works exactly like a table: when you span a cell to make it occupy 2 columns, but the second column has no width, the cell will keep the same size.

Note that even if you had widgets in another row and in any of those columns, this would still not mean that the tab widget would occupy 5 times the space of the first, especially if those widgets were small: QTreeView inherits QAbstractItemView, which by default has an expanding size policy in both directions.

To achieve the wanted ratio, you must use setColumnStretch(), and not set any span unless you actually need it for widgets in other rows:

        layout.addWidget(FileTree())
        layout.addWidget(Tabs(), 0, 1)
        layout.setColumnStretch(0, 1)
        layout.setColumnStretch(1, 5)
  • Related