Search code examples
pythonpyqt4qscrollarea

Can I make a groupbox outside of a scroll area?


I have a QGroupBox with a title '123'. Inside the QGroupBox, there should be a QScrollArea. That means that the title '123' of QGrouBox should be outside of the QScrollArea.

My sample codes are as below.

import sys

import PyQt4
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Example(QWidget):
    def __init__(self, parent = None):
        super().__init__()

        btn = QPushButton('button')

        scroll = QScrollArea()
        scroll.setWidgetResizable(True)
        scroll.setWidget(btn)


        groupbox = QGroupBox('123')
        groupbox.setLayout(scroll)


        self.show()


def main():
    app = QApplication(sys.argv)
    main = Example()
    main.show()
    sys.exit(app.exec_())

As you can see above, now it returns TypeError: setLayout(self, QLayout): argument 1 has unexpected type 'QScrollArea'.

I'm just wondering if this is achievable? Thanks!!

enter image description here


Solution

  • When you want to add the content to a QGroupBox you must do through a layout that contains the necessary widgets, in this case as it is only a widget we can use any layout, and in this layout we add the widget that this case is the QScrollArea as sample then:

    class Example(QWidget):
        def __init__(self, parent = None):
            super().__init__()
            self.setLayout(QVBoxLayout())
    
            btn = QPushButton('button')
            scroll = QScrollArea()
            scroll.setWidgetResizable(True)
            scroll.setWidget(btn)
    
            groupbox = QGroupBox('123', self)
            groupbox.setLayout(QVBoxLayout())
            groupbox.layout().addWidget(scroll)
    
            self.layout().addWidget(groupbox)
    

    Screenshot:

    enter image description here