为了账号安全,请及时绑定邮箱和手机立即绑定

信号 QscrollArea

信号 QscrollArea

慕尼黑5688855 2023-02-07 09:34:39
我想知道是否有可能在每次调整窗口大小时有信号或其他东西知道滚动何时可用以及何时不再可用?import sysfrom PyQt5.QtCore import *from PyQt5.QtWidgets import *class Widget(QWidget):    def __init__(self, parent= None):        super(Widget, self).__init__()        widget = QWidget()        layout = QVBoxLayout(self)        for _ in range(10):            btn = QPushButton()            layout.addWidget(btn)        widget.setLayout(layout)        scroll = QScrollArea()        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)        scroll.setWidget(widget)        vLayout = QVBoxLayout(self)        vLayout.addWidget(scroll)        self.setLayout(vLayout)if __name__ == '__main__':    app = QApplication(sys.argv)    dialog = Widget()    dialog.show()    app.exec_()
查看完整描述

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')


查看完整回答
反对 回复 2023-02-07
  • 1 回答
  • 0 关注
  • 80 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信