python - 一个或多个QGroupBox如何使用QScrollArea?

标签 python pyqt pyqt5 qscrollarea

我一直在尝试将一个/多个 QGroupBox 插入到一个 QScrollArea 中。

问题是:滚动条不显示。

这是我的代码:

# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtWidgets import QGroupBox
from PyQt5.QtWidgets import QScrollArea

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QGridLayout

import sys

lst = [u"D", u"E", u"EF", u"F", u"FG", u"G", u"H", u"JS", u"J", u"K", u"M", u"P", u"R", u"S", u"T", u"U", u"V", u"X", u"Y", u"Z"]

class MyApp(QWidget):
    def __init__(self):
        super(MyApp, self).__init__()

        window_width = 1200
        window_height = 600
        self.setFixedSize(window_width, window_height)

        self.initUI()

    def createLayout_group(self):
        self.groupbox = QGroupBox(u"Group1:")
        self.layout_groupbox = QVBoxLayout()

        for i in range(len(lst)):
            self.item = QCheckBox(lst[i], self.groupbox)
            self.layout_groupbox.addWidget(self.item)
        self.layout_groupbox.addStretch(1)

        self.groupbox.setLayout(self.layout_groupbox)


    def createLayout_Container(self):

        self.scrollarea = QScrollArea(self)
        self.scrollarea.setFixedSize(250, 6000)
        self.scrollarea.setWidgetResizable(False)

        self.layout_SArea = QVBoxLayout()
        self.layout_SArea.addWidget(self.groupbox)
        self.layout_SArea.addWidget(self.groupbox) # add groupbox one more to test
        self.layout_SArea.addWidget(self.groupbox) # add groupbox one more to test
        self.layout_SArea.addStretch(1)

        self.scrollarea.setLayout(self.layout_SArea)



    def initUI(self):
        self.createLayout_group()   # load one groupbox
        self.createLayout_Container()  # load groupbox container

        self.layout_All = QVBoxLayout()
        self.layout_All.addWidget(self.scrollarea)
        self.setLayout(self.layout_All)

        self.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    sys.exit(app.exec_())

我想实现这个效果。 (只是为了表示)但是上面的代码没有显示滚动条。

enter image description here

最佳答案

您的代码存在以下问题:

  • 您不必向 QScrollArea 添加布局,您必须传递的是一个小部件,因此您构建的布局必须添加到一个小部件,并且该小部件必须设置为 QScrollArea。

  • 每次您调用 createLayout_group 时,您都会覆盖类 self.groupbox、self.layout_groupbox 等的成员,这是不必要的并且可能导致问题,该方法返回 QGroupBox 是正确的。

  • 另一个错误是,如果您使用 setWidgetResizable(False),内部小部件将被收缩,这不好看

  • 另一个问题是你不应该设置一个固定的大小,而应该只设置一个固定的宽度,如果你想设置一个固定的大小,那么你应该使滚动条始终可见。


lst = [u"D", u"E", u"EF", u"F", u"FG", u"G", u"H", u"JS", u"J", u"K", u"M", u"P", u"R", u"S", u"T", u"U", u"V", u"X", u"Y", u"Z"]

class MyApp(QWidget):
    def __init__(self):
        super(MyApp, self).__init__()
        window_width = 1200
        window_height = 600
        self.setFixedSize(window_width, window_height)
        self.initUI()

    def createLayout_group(self, number):
        sgroupbox = QGroupBox("Group{}:".format(number), self)
        layout_groupbox = QVBoxLayout(sgroupbox)
        for i in range(len(lst)):
            item = QCheckBox(lst[i], sgroupbox)
            layout_groupbox.addWidget(item)
        layout_groupbox.addStretch(1)
        return sgroupbox

    def createLayout_Container(self):
        self.scrollarea = QScrollArea(self)
        self.scrollarea.setFixedWidth(250)
        self.scrollarea.setWidgetResizable(True)

        widget = QWidget()
        self.scrollarea.setWidget(widget)
        self.layout_SArea = QVBoxLayout(widget)

        for i in range(5):
            self.layout_SArea.addWidget(self.createLayout_group(i))
        self.layout_SArea.addStretch(1)

    def initUI(self):
        self.createLayout_Container()
        self.layout_All = QVBoxLayout(self)
        self.layout_All.addWidget(self.scrollarea)
        self.show()

输出:

enter image description here

关于python - 一个或多个QGroupBox如何使用QScrollArea?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47930677/

相关文章:

python - 在 QPlainTextEdit 中突出显示部分文本

python - PyQt5 动态添加带循环的小部件

python - 将预测值和残差附加到 Pandas 数据框

Python 夏娃 0.8 : how to perform a $geowithin with a $centerSphere?

python - 如何在 PyQt4 中使用 matplotlib

python - 异步Python函数处理PHP

python - 计算在给定映射的情况下可以解码消息的方式的数量

python - 为什么 C++ 代码实现的性能并不比 Python 实现更好?

python - 当我添加按钮时,KeyPressEvent() 不适用于标签

python - 如何使用 PyQt5 的 QWebEngineView 处理 "render"HTML