我正在尝试使用方法QPushButton使最后一个QScrollArea().ensureWidgetVisible()可见,但是正如您所看到的,该方法不会滚动到最后一个QPushButton





您能协助解决setFrameStyle的问题吗?先感谢您。

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_()

最佳答案

问题在于,出于效率原因,直到显示小部件尺寸时才进行计算或更新,在您的情况下,QScrollArea的视口尚未更新其尺寸,因此将滚动条移动到中间位置。一种可能的解决方案是在显示后的瞬间使用QTimer::singleShot()调用函数ensureWidgetVisible()

import sys
from functools import partial
from PyQt5 import QtCore, QtGui, QtWidgets


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

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

        scroll = QtWidgets.QScrollArea()
        scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        scroll.setWidgetResizable(False)
        scroll.setWidget(widget)

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

        last_widget = layout.itemAt(layout.count()-1).widget()
        QtCore.QTimer.singleShot(0, partial(scroll.ensureWidgetVisible, last_widget))


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


或只需在之前致电show()

...
last_widget = layout.itemAt(layout.count()-1).widget()
self.show()
scroll.ensureWidgetVisible(last_widget)

关于python - QScrollArea.ensureWidgetVisible方法不显示目标小部件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52450219/

10-12 23:26