Rt Rtt
Rt Rtt

Reputation: 617

How to use QScrollArea for one or many QGroupBox?

I have been trying to insert one/many QGroupBox into one QScrollArea.

The problem is: The Scroll bar does not show.

Here is my code:

# -*- 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_())

I want to achieve this effect. (Just for representation) But the code above shows no scroller.

enter image description here

Upvotes: 3

Views: 9302

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

Your code has the following problems:

  • You do not have to add a layout to QScrollArea, what you must pass is a widget, so that layout that you have built must be added to a widget, and that widget must be set to QScrollArea.

  • Every time you call createLayout_group you are overwriting the member of the class self.groupbox, self.layout_groupbox, etc, that is unnecessary and can lead to problems, it would be right for that method to return the QGroupBox.

  • another error is that if you use setWidgetResizable(False) the internal widget will be contracted and that is not pleasing to the eye

  • and the other problem is that you should not set a fixed size, but only a fixed width, if you want to set a fixed size then you should enable the scrollbar to be visible always.


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

Output:

enter image description here

Upvotes: 7

Related Questions