Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PySide2: Scrollable QVBoxLayout with QScrollArea ?
Forum Updated to NodeBB v4.3 + New Features

PySide2: Scrollable QVBoxLayout with QScrollArea ?

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nicholas_yueN Offline
    nicholas_yueN Offline
    nicholas_yue
    wrote on last edited by
    #1

    I am adding wdigets programmatically to a QVBoxLayout that is initially empty.

    I would like the vertical scroll bar to appear when the QVBoxLayout can no longer hold all the widgets in view.

    I tried with the following approach but it didn't work.

    Any specific advice ?

    import sys
    from PySide2 import QtWidgets, QtCore
    
    class Example(QtWidgets.QWidget):
        
        def __init__(self):
            super(Example, self).__init__()
            
            self.initUI()
            
        def initUI(self):
            
            scroll = QtWidgets.QScrollArea()
            scroll.setWidgetResizable(True)
            vbox = QtWidgets.QVBoxLayout()
            vbox.setAlignment(QtCore.Qt.AlignTop)
            scroll.setLayout(vbox)
            vbox.addStretch(1)
            for index in range(20):
                vbox.addWidget(QtWidgets.QPushButton('button {}'.format(index)))
            self.setLayout(vbox)    
            
            self.setGeometry(300, 300, 300, 150)
            self.setWindowTitle('Buttons')    
            self.show()
            
    def main():
        
        app = QtWidgets.QApplication(sys.argv)
        ex = Example()
        ex.show()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do not set the layout on the scroll area. Set it on a widget that you will set on the QScrollArea.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      nicholas_yueN 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Do not set the layout on the scroll area. Set it on a widget that you will set on the QScrollArea.

        nicholas_yueN Offline
        nicholas_yueN Offline
        nicholas_yue
        wrote on last edited by
        #3

        @SGaist said in PySide2: Scrollable QVBoxLayout with QScrollArea ?:

        Hi,

        Do not set the layout on the scroll area.

        Got it. I should not do this

                scroll.setLayout(vbox)
        

        Set it on a widget that you will set on the QScrollArea.

        Are you able to expand more on what you mean ? Do I create another widget for QScrollArea or is there an existing widget in QScrollArea I should be using ? Are you able to share some code example of what you mean ?

        Cheers

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Just create a QWidget, set the layout on it and then call QScrollArea's setWidget method as shown in the class documentation .

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          nicholas_yueN 1 Reply Last reply
          0
          • SGaistS SGaist

            Just create a QWidget, set the layout on it and then call QScrollArea's setWidget method as shown in the class documentation .

            nicholas_yueN Offline
            nicholas_yueN Offline
            nicholas_yue
            wrote on last edited by
            #5

            @SGaist Thank you, I got it working.

            import sys
            from PySide2 import QtWidgets, QtCore
            
            class Example(QtWidgets.QWidget):
                
                def __init__(self):
                    super(Example, self).__init__()
                    
                    self.initUI()
                    
                def initUI(self):
                    
                    scroll = QtWidgets.QScrollArea(self)
                    vbox = QtWidgets.QVBoxLayout()
                    for index in range(20):
                        vbox.addWidget(QtWidgets.QPushButton('button {}'.format(index)))
                    widget = QtWidgets.QWidget()
                    widget.setLayout(vbox)
                    scroll.setWidget(widget)
                    
                    self.setGeometry(300, 300, 300, 150)
                    self.setWindowTitle('Buttons')    
                    self.show()
                    
            def main():
                
                app = QtWidgets.QApplication(sys.argv)
                ex = Example()
                ex.show()
                sys.exit(app.exec_())
            
            
            if __name__ == '__main__':
                main()
            
            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              One thing: do not call show from the constructor. It's not to the widget to impose to be shown on construction.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              nicholas_yueN 1 Reply Last reply
              1
              • SGaistS SGaist

                One thing: do not call show from the constructor. It's not to the widget to impose to be shown on construction.

                nicholas_yueN Offline
                nicholas_yueN Offline
                nicholas_yue
                wrote on last edited by
                #7

                @SGaist Good catch, thanks for the advice.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved