1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to get stretch factor for QSpacerItems

Discussão em 'Python' iniciado por Stack, Setembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    When you addStretch() or insertStretch() to a QBoxLayout, a QSpacerItem is added/inserted to the layout and you can set the stretch factor while doing so.

    Now I would like to retrieve that stretch factor from the SpacerItem but I can't find how to do so.
    item.sizePolicy().verticalStretch() always returns 0, as can be demonstrated with this example:

    from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QSpacerItem


    class Window(QWidget):
    def __init__(self):
    super().__init__()
    self.setGeometry(700, 500, 100, 200)
    self.layout = QVBoxLayout(self)

    for i in range(3):
    but = QPushButton(f'Push {i}')
    self.layout.addWidget(but)
    if i == 0:
    but.clicked.connect(self.click)

    self.layout.insertStretch(1, 2)
    self.layout.insertStretch(3, 3)

    def click(self):
    for i in (1, 3):
    stretch = self.layout.itemAt(i)
    assert isinstance(stretch, QSpacerItem)
    pol = stretch.sizePolicy()
    print(pol.verticalStretch())


    app = QApplication([])
    window = Window()
    window.show()
    app.exec()


    So where is the stretch factor stored? and is this info accessible somehow or would I have to keep a separate reference to keep the stretch info available?

    Continue reading...

Compartilhe esta Página