【问题标题】:QScrollArea is expanding instead of scrollingQScrollArea 正在扩展而不是滚动
【发布时间】:2014-09-01 17:52:56
【问题描述】:

我有一个QScrollArea,我会在其中添加一些其他小部件。我想要的是QScrollArea 在其中的小部件扩展其大小时开始滚动。

但滚动区域开始自行扩展。

这是我使用的代码:

import sys
from PyQt4 import QtGui, QtCore

class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.vbox = QtGui.QVBoxLayout()
        self.setLayout(self.vbox)

    def add_button(self):
        tmp = QtGui.QPushButton("...", self)
        self.vbox.addWidget(tmp)

class ScrollArea(QtGui.QScrollArea):
    def __init__(self, parent=None):
        QtGui.QScrollArea.__init__(self, parent)

        self.vbox = QtGui.QVBoxLayout()

        self.w = Widget(self)
        self.setWidget(self.w)    #set the widget to provide scrolling for here
        self.vbox.addWidget(self.w)

        self.plus = QtGui.QPushButton("button", self)
        self.connect(self.plus, QtCore.SIGNAL("clicked()"), self.add_button)
        self.vbox.addWidget(self.plus)

        self.setLayout(self.vbox)

    def add_button(self):
        self.w.add_button()

app = QtGui.QApplication(sys.argv)
main = ScrollArea()
main.show()
sys.exit(app.exec_())

我还尝试在ScrollArea 类中设置子小部件self.w 的布局,而不是在其自己的类中,但效果相同。

【问题讨论】:

  • 根据文档,QScrollArea 能够在里面显示 一个 小部件,您必须使用 setWidget 方法添加小部件,而不是构建自己的布局并手动将其应用于该区域。见here
  • 是的,你说得对,我忘了那个。但不幸的是,添加行 self.setWidget(self.w) 并不会改变行为。根据文档,它现在应该为小部件 self.w 提供滚动,但不知何故它没有。
  • 你还在打电话给setLayout是破坏行为的原因。
  • 你说得对,我成功了!如果想要将子级添加到滚动区域负责的小部件,则为该小部件的布局调用setSizeContraint(QLayout.SetMinAndMaxSize)(或使用类似参数)非常重要。如果您不介意,我将在下面的答案中进行总结。

标签: python pyqt4 qscrollarea


【解决方案1】:

正如 Bakuriu 已经指出的,重要的是不要将自己的布局应用于 QScrollArea,而只是通过 setWidget 指定一个小部件(在下面用 w 表示)作为滚动条区域应该小心。

设置w 的布局很重要在调用setWidget 之前 并且如果想将其他几个孩子添加到w(如上面的QPushButtons问题)为w 的布局调用setSizeConstraint 也很重要(例如setSizeContraint(QLayout.SetMinAndMaxSize),更多选项请参见this)。关注this link,了解更多QScrollArea行为详情。

以下代码是一个工作示例:

import sys
from PyQt4 import QtGui, QtCore

class Widget(QtGui.QWidget):
    """ Here we add more buttons, if the size of the buttons exceeds the size of the widget scrolling starts. """
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.vbox = QtGui.QVBoxLayout()
        self.vbox.setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize)
        self.setLayout(self.vbox)

    def add_button(self):
        tmp = QtGui.QPushButton("...", self)
        self.vbox.addWidget(tmp)

class ScrollArea(QtGui.QScrollArea):
    """ This scroll area only handles one widget for which scrolling should be provided. """
    def __init__(self, parent=None):
        QtGui.QScrollArea.__init__(self, parent)

        self.w = Widget()
        self.setWidget(self.w)    #set the widget to provide scrolling for here

    def add_button(self):
        self.w.add_button()

class MainWindow(QtGui.QWidget):
    """ Use this widget to arrange the QScrollArea and the QPushButton. """
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self)

        self.vbox = QtGui.QVBoxLayout()
        self.scroll = ScrollArea(self)
        self.vbox.addWidget(self.scroll)

        self.plus = QtGui.QPushButton("button", self)
        self.connect(self.plus, QtCore.SIGNAL("clicked()"), self.add_button)
        self.vbox.addWidget(self.plus)

        self.setLayout(self.vbox)

    def add_button(self):
        self.scroll.add_button()

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 2011-10-11
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多