Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QScrollArea sizing issue
Qt 6.11 is out! See what's new in the release blog

QScrollArea sizing issue

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 9.8k Views 3 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.
  • SGaistS SGaist

    Hi,

    No, not at all. With scrollArea = QScrollArea(self) you are just giving it a parent and when you call show on it, it will be shown in the parent viewport.

    With setLayout(scrollArea), that's just wrong. You can't set a widget as layout.

    So neither are doing what you want.

    In the constructor of your dialog you should have something like:

    layout = QVBoxLayout()
    layout->addWidget(scrollArea)
    self.setLayout(layout)
    

    or the short version:

    layout = QVBoxLayout(self) # Create a layout and apply it to it's parent
    layout->addWidget(scrollArea)
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #5

    @SGaist
    Thanks for this, when I get back to code I will review in this light.

    Does that indeed mean that the example I copied from, https://stackoverflow.com/a/28826793/489865, is not right, as @mrjj was suggesting? That posts says, and I (thought I) followed the code:

    •on the bottom is parent widget (for example QDialog)
    •on top of this is scrollable area (QScrollArea) of fixed size

    i.e. scrollarea straight on dialog, you are putting it on layout, he does not do that? I am getting really confused with the layers of widgets/layouts/scrollareas... :(

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

      No, the content of the widget is described correctly on that post.

      The two points of interest:

      • on the bottom is parent widget (for example QDialog)
      • on top of this is scrollable area (QScrollArea) of fixed size

      You see ? No layouts involved there. The fact that it's passing a parent to the QScrollArea makes it appear within the dialog.

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

      JonBJ 1 Reply Last reply
      0
      • SGaistS SGaist

        No, the content of the widget is described correctly on that post.

        The two points of interest:

        • on the bottom is parent widget (for example QDialog)
        • on top of this is scrollable area (QScrollArea) of fixed size

        You see ? No layouts involved there. The fact that it's passing a parent to the QScrollArea makes it appear within the dialog.

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

        @SGaist
        Thanks, but I thought I was to do the same as him, yet in mine you're telling me I need a QVBoxLayout that he does not need.....

        I started with a dialog with a vboxlayout and some content. My content overflows the dialog vertically. I want to have a vertical scrollbar against the side of the dialog for its content. That's all I want to add to the original code. I'm finding this mega-complex.... :(

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

          What the examples shows is how a QScrollArea work not how to make it fit in a widget.

          From your description you want your scroll area to fill your widget so that you don't have to resize it, hence you need to put the area in a layout applied to said widget.

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

          JonBJ 1 Reply Last reply
          1
          • SGaistS SGaist

            What the examples shows is how a QScrollArea work not how to make it fit in a widget.

            From your description you want your scroll area to fill your widget so that you don't have to resize it, hence you need to put the area in a layout applied to said widget.

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

            @SGaist
            OK, thanks, I'll have a play.

            All I need to understand is the hierarchy I need. If I started with
            dialog -> vboxlayout1 -> content
            I will now need
            dialog -> vboxlayout2 -> scrollarea ( -> widget ?) -> vboxlayout1 -> content
            Is that right? I'll need a separate vboxlayout from the one I started from, I don't somehow apply the scrollarea to the original vboxlayout?

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

              To make things clearer:

              widget -> vboxlayout1 -> content <- future content of the QScrollArea

              dialog -> vboxlayout2 -> scrollarea <- QScrollArea fitting the "main widget"

              scrollarea -> widget <- finally your widget in the QScrollArea

              See QScrollArea::setWidget

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

              JonBJ 1 Reply Last reply
              1
              • SGaistS SGaist

                To make things clearer:

                widget -> vboxlayout1 -> content <- future content of the QScrollArea

                dialog -> vboxlayout2 -> scrollarea <- QScrollArea fitting the "main widget"

                scrollarea -> widget <- finally your widget in the QScrollArea

                See QScrollArea::setWidget

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

                @SGaist
                OK, thanks a lot, at least I know what I'm trying to achieve now! :) Will try and report back.

                JonBJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @SGaist
                  OK, thanks a lot, at least I know what I'm trying to achieve now! :) Will try and report back.

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

                  @SGaist
                  OK, armed with the understanding of what the actual hierarchy should be, I have now modified to outline:

                  # self == QDialog
                  self.dialogScrollPanel = QVBoxLayout()
                  self.setLayout(self.dialogScrollPanel)
                  
                  scrollArea = QScrollArea(self)
                  scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
                  scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
                  scrollArea.setWidgetResizable(True)
                  self.dialogScrollPanel.addWidget(scrollArea)
                  
                  widget = QWidget()
                  scrollArea.setWidget(widget)
                  
                  self.vertLayout = QVBoxLayout()
                  widget.setLayout(self.vertLayout)
                  ...
                  

                  And sure enough my dialog now looks like:
                  alt text
                  which is just what I was aiming for!

                  The one bit I'm left hazy on is when you do or do not need to specify an owner/parent for a layout or widget. My code includes:

                  self.dialogScrollPanel = QVBoxLayout()
                  self.setLayout(self.dialogScrollPanel)
                  
                  widget = QWidget()
                  scrollArea.setWidget(widget)
                  
                  self.vertLayout = QVBoxLayout()
                  widget.setLayout(self.vertLayout)
                  

                  In each of these cases the constructor could equally be QVBoxLayout(self) or QWidget(self) and I see the same behaviour. In the code I've inherited it seems sometimes it passes an argument to these and sometimes not. When do I need or not need to pass a widget parent/owner argument when creating a widget or layout?

                  mrjjM 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @SGaist
                    OK, armed with the understanding of what the actual hierarchy should be, I have now modified to outline:

                    # self == QDialog
                    self.dialogScrollPanel = QVBoxLayout()
                    self.setLayout(self.dialogScrollPanel)
                    
                    scrollArea = QScrollArea(self)
                    scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
                    scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
                    scrollArea.setWidgetResizable(True)
                    self.dialogScrollPanel.addWidget(scrollArea)
                    
                    widget = QWidget()
                    scrollArea.setWidget(widget)
                    
                    self.vertLayout = QVBoxLayout()
                    widget.setLayout(self.vertLayout)
                    ...
                    

                    And sure enough my dialog now looks like:
                    alt text
                    which is just what I was aiming for!

                    The one bit I'm left hazy on is when you do or do not need to specify an owner/parent for a layout or widget. My code includes:

                    self.dialogScrollPanel = QVBoxLayout()
                    self.setLayout(self.dialogScrollPanel)
                    
                    widget = QWidget()
                    scrollArea.setWidget(widget)
                    
                    self.vertLayout = QVBoxLayout()
                    widget.setLayout(self.vertLayout)
                    

                    In each of these cases the constructor could equally be QVBoxLayout(self) or QWidget(self) and I see the same behaviour. In the code I've inherited it seems sometimes it passes an argument to these and sometimes not. When do I need or not need to pass a widget parent/owner argument when creating a widget or layout?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @JNBarchan

                    Any widget that is not given a parent will become a window.
                    So for any widget on a from, you will want to give it a parent.
                    However, inserting them into other widgets set the parent.

                    For Layouts, its its easier just to assign them to a widget and in that
                    way give a parent.

                    So in most cases you will just want to give a parent when constructed as
                    to make sure they are deleted.

                    JonBJ 1 Reply Last reply
                    2
                    • mrjjM mrjj

                      @JNBarchan

                      Any widget that is not given a parent will become a window.
                      So for any widget on a from, you will want to give it a parent.
                      However, inserting them into other widgets set the parent.

                      For Layouts, its its easier just to assign them to a widget and in that
                      way give a parent.

                      So in most cases you will just want to give a parent when constructed as
                      to make sure they are deleted.

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

                      @mrjj
                      Thanks. I suspect the key is:

                      However, inserting them into other widgets set the parent.

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

                        I usually follow these rules:

                        • When I create layout that should be the "main" layout of a widget, I pass that widget as parent
                          Note that's just an habit, if you prefer the setLayout method then go for it, just be consistent all over your code base.
                        • All widgets that are put inside a layout will be re-parented appropriately so I don't pass a parent to them.
                        • Unless I'm going to manipulate the layout later on I don't make it a member of the class

                        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
                        1

                        • Login

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