1

我有一个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_())

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

4

1 回答 1

1

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

在调用w 之前设置 的布局很重要setWidget,如果想要添加其他几个孩子w(如上述问题中的 s ),调用for的布局QPushButton也很重要(例如,请参阅以获得更多选项)。请点击此链接以获取有关.setSizeConstraintwsetSizeContraint(QLayout.SetMinAndMaxSize)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_())
于 2014-09-02T09:15:47.253 回答