1 回答
TA贡献1818条经验 获得超7个赞
由于 QScrollBar 通常仅在其范围最大值大于 0 时才可见,因此您可以只检查该值。
class Widget(QWidget):
def __init__(self, parent= None):
# ....
# note that the scroll area is now an instance member
self.scroll = QScrollArea()
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scroll.setWidget(widget)
self.scroll.verticalScrollBar().rangeChanged.connect(self.checkScrollBarRange)
vLayout = QVBoxLayout(self)
vLayout.addWidget(self.scroll)
self.setLayout(vLayout)
# the next is important when resizing the widget containing the scroll area
self.scroll.installEventFilter(self)
def eventFilter(self, source, event):
if source == self.scroll and event.type() == QEvent.Resize:
self.checkScrollBarRange()
return super().eventFilter(source, event)
def checkScrollBarRange(self):
if (self.scroll.verticalScrollBarPolicy() != Qt.ScrollBarAlwaysOff and
self.scroll.verticalScrollBar().maximum()):
print('vertical scroll bar IS visible')
else:
print('vertical scroll bar NOT visible')
添加回答
举报