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. QScrollArea is not resizing as expected
QtWS25 Last Chance

QScrollArea is not resizing as expected

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 4 Posters 625 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.
  • ArtofkaA Offline
    ArtofkaA Offline
    Artofka
    wrote on last edited by Artofka
    #1

    When I use QScrollArea in layout with Stretch, after resizing I'll get the big blank area where stretch is added.
    That's a minimal example that show my problem

    from PySide2 import QtCore, QtWidgets, QtGui
    
    
    class BugWithScrollareaAndStretch(QtWidgets.QWidget):
        def __init__(self):
            super(BugWithScrollareaAndStretch, self).__init__()
    
            scrollarea = QtWidgets.QScrollArea()
            scrollarea.setWidgetResizable(True)
    
            scrollareaLayout = QtWidgets.QVBoxLayout()
            for i in range(20):
                scrollareaLayout.addWidget(QtWidgets.QPushButton(f'Button # {i}'))
    
            scrollareaWidget = QtWidgets.QWidget()
            scrollareaWidget.setLayout(scrollareaLayout)
            scrollarea.setWidget(scrollareaWidget)
    
    
    
            bottomButton = QtWidgets.QPushButton()
    
            mainLay = QtWidgets.QVBoxLayout()
            mainLay.addWidget(scrollarea)
            
            # Scrollarea is not resizing properly with the .addStretch()
            mainLay.addStretch()
            mainLay.addWidget(bottomButton)
    
            self.setLayout(mainLay)
    
    
    app = QtWidgets.QApplication([])
    widget = BugWithScrollareaAndStretch()
    widget.show()
    app.exec_()
    
    
    

    This is how it looks in action:

    5a7926de-5950-476f-a7bd-065924ddb41c-image.png

    JonBJ 1 Reply Last reply
    0
    • ArtofkaA Artofka

      When I use QScrollArea in layout with Stretch, after resizing I'll get the big blank area where stretch is added.
      That's a minimal example that show my problem

      from PySide2 import QtCore, QtWidgets, QtGui
      
      
      class BugWithScrollareaAndStretch(QtWidgets.QWidget):
          def __init__(self):
              super(BugWithScrollareaAndStretch, self).__init__()
      
              scrollarea = QtWidgets.QScrollArea()
              scrollarea.setWidgetResizable(True)
      
              scrollareaLayout = QtWidgets.QVBoxLayout()
              for i in range(20):
                  scrollareaLayout.addWidget(QtWidgets.QPushButton(f'Button # {i}'))
      
              scrollareaWidget = QtWidgets.QWidget()
              scrollareaWidget.setLayout(scrollareaLayout)
              scrollarea.setWidget(scrollareaWidget)
      
      
      
              bottomButton = QtWidgets.QPushButton()
      
              mainLay = QtWidgets.QVBoxLayout()
              mainLay.addWidget(scrollarea)
              
              # Scrollarea is not resizing properly with the .addStretch()
              mainLay.addStretch()
              mainLay.addWidget(bottomButton)
      
              self.setLayout(mainLay)
      
      
      app = QtWidgets.QApplication([])
      widget = BugWithScrollareaAndStretch()
      widget.show()
      app.exec_()
      
      
      

      This is how it looks in action:

      5a7926de-5950-476f-a7bd-065924ddb41c-image.png

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Artofka
      Hello and welcome.

      I don't know, but are you expecting it to do with that stretch in vertical layout below the scroll area? I think it's about half and half for the scroll and the stretch, seems reasonable to me?

      ArtofkaA 1 Reply Last reply
      0
      • JonBJ JonB

        @Artofka
        Hello and welcome.

        I don't know, but are you expecting it to do with that stretch in vertical layout below the scroll area? I think it's about half and half for the scroll and the stretch, seems reasonable to me?

        ArtofkaA Offline
        ArtofkaA Offline
        Artofka
        wrote on last edited by
        #3

        @JonB Thanks for responce. I was expected from QScrollArea to fill all the area untill bottom button

        M JonBJ 2 Replies Last reply
        0
        • ArtofkaA Artofka

          @JonB Thanks for responce. I was expected from QScrollArea to fill all the area untill bottom button

          M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #4

          @Artofka said in QScrollArea is not resizing as expected:

          @JonB Thanks for responce. I was expected from QScrollArea to fill all the area untill bottom button

          mainLay.addStretch()
          The word stretch is confusing here, because it doesn't concern the scrollarea but the spacer that was inserted.
          The main goal of a strech spacer is to force all the widgets (here your buttons) to their minimum height.

          You can experiment these with the Designer. Great tool to understand how layout, spacer, etc, work together.

          1 Reply Last reply
          0
          • ArtofkaA Artofka

            @JonB Thanks for responce. I was expected from QScrollArea to fill all the area untill bottom button

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Artofka
            I'm not the expert, I just play with Qt's layout stuff till it does what I want/I beat it into submission! ;-) But the point of a stretch is to fill/take up space (it's a QSpacerItem, "The QSpacerItem class provides blank space in a layout"), normally as much as possible. It stretches to consume the available space. It's used to push the following thing right horizontally or down vertically, just as you show here.

            So I guess you want "QScrollArea to fill all the area untill bottom button", i.e. be as tall as possible, then start scrolling? Did you try it without adding that stretch?

            I hope I have not told you the wrong thing! You may have to wait for a more knowledgeable person than I!

            ArtofkaA 1 Reply Last reply
            0
            • JonBJ JonB

              @Artofka
              I'm not the expert, I just play with Qt's layout stuff till it does what I want/I beat it into submission! ;-) But the point of a stretch is to fill/take up space (it's a QSpacerItem, "The QSpacerItem class provides blank space in a layout"), normally as much as possible. It stretches to consume the available space. It's used to push the following thing right horizontally or down vertically, just as you show here.

              So I guess you want "QScrollArea to fill all the area untill bottom button", i.e. be as tall as possible, then start scrolling? Did you try it without adding that stretch?

              I hope I have not told you the wrong thing! You may have to wait for a more knowledgeable person than I!

              ArtofkaA Offline
              ArtofkaA Offline
              Artofka
              wrote on last edited by Artofka
              #6

              @JonB @mpergand The reason for stretch is to keep bottomButton on it's place when I visually do replace QScrollArea with another QWidget (that smaller) using theit isVisible(True/False)

              JonBJ 1 Reply Last reply
              0
              • ArtofkaA Artofka

                @JonB @mpergand The reason for stretch is to keep bottomButton on it's place when I visually do replace QScrollArea with another QWidget (that smaller) using theit isVisible(True/False)

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @Artofka
                I think somebody else should tell you the right way to do that!

                I will comment on one thing. Although you can use visible or adding/removing to toggle which widget is visible, normally the most convenient is to put the alternatives in a QStackedWidget, q.v. However, I know/found that a stacked widget usually takes up the amount of space of the largest of its alternatives. Maybe that would help here, maybe not, but it's worth knowing about that widget.

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

                  Hi,

                  From the looks of it, you should check QStackedWidget to handle your multiple widgets.

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

                  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