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. How to add to QVBoxLayout after it was associated with a QScrollArea ?
Forum Updated to NodeBB v4.3 + New Features

How to add to QVBoxLayout after it was associated with a QScrollArea ?

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 366 Views
  • 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 would like to add widgets to a QVBoxLayout sometime later in my code after some user interaction.

    The existing code I have works fine if I add widgets to my layout before I perform the QScrollArea but if I were to add them after that, it does not show up. In addition to addWidget() is there some other calls I need to make to tell Qt that the QVBoxLayout content has been updated ?

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

      Answering my own question.

      I need to make the scroll resizable, i.e.

      scroll = QtWidgets.QScrollArea(self)
      scroll.setWidgetResizable(True)
      

      Cheers

      JonBJ 1 Reply Last reply
      0
      • nicholas_yueN nicholas_yue

        Answering my own question.

        I need to make the scroll resizable, i.e.

        scroll = QtWidgets.QScrollArea(self)
        scroll.setWidgetResizable(True)
        

        Cheers

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @nicholas_yue
        Without understanding why, I, and most others, always set setWidgetResizable(True)! I'm sure there are cases where one does not want to do this, especially since it defaults to false, but I don't know what they are. :)

        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