[Python] QScrollArea.ensureWidgetVisible method does not show target widget

Discussão em 'Python' iniciado por Stack, Junho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make the last QPushButton visible by using method QScrollArea().ensureWidgetVisible(), but as you can see this method doesn't scroll till the last QPushButton.

    Example

    [​IMG]

    Could you please assist and solve my issue perhaps issue with setFrameStyle? thank you in advance.

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *


    class Widget(QWidget):

    def __init__(self, parent= None):
    super(Widget, self).__init__()
    self.setFixedHeight(200)

    #Container Widget
    widget = QWidget()
    #Layout of Container Widget
    layout = QVBoxLayout(self)
    for _ in range(20):
    btn = QPushButton("test")
    layout.addWidget(btn)
    widget.setLayout(layout)


    #Scroll Area Properties
    scroll = QScrollArea()
    scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
    scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    scroll.setWidgetResizable(False)
    scroll.setWidget(widget)

    # print(scroll.verticalScrollBar().maximum())
    # vbar = scroll.verticalScrollBar()
    # vbar.setValue(vbar.maximum())
    #vbar.setValue(vbar.maximum())


    #Scroll Area Layer add
    vLayout = QVBoxLayout(self)
    vLayout.addWidget(scroll)
    self.setLayout(vLayout)


    # items = (layout.itemAt(i) for i in range(layout.count()))
    # for w in items:
    # print(w)
    print(layout.count())
    #scroll.ensureWidgetVisible(layout.itemAt(layout.count()-5).widget(), xMargin=10, yMargin=10 )
    scroll.ensureWidgetVisible(layout.itemAt(layout.count()-1).widget() )
    print(layout.itemAt(layout.count()-1).widget(),"last widget")


    if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = Widget()
    dialog.show()

    app.exec_()

    Continue reading...

Compartilhe esta Página