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. I would need someone to teach me some things about QScrollArea.
QtWS25 Last Chance

I would need someone to teach me some things about QScrollArea.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 234 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.
  • C Offline
    C Offline
    CEO.
    wrote on last edited by CEO.
    #1

    Hello, please just as we have the .setWidget() method when using a QScrollArea, do we have the .setLayout() method for it? Please pardon the structure of my sentences.

    I mean this for example:
    <>

    Class app(QWidget):
      (the function def init and super codes)
        grid = QGridLayout ()
        self.setLayout(grid)
        vbox = QVBoxLayout()
        gbox = QGroupBox()
        grid.addWidget(gbox)
        gbox.setLayout(vbox)
        hbox = QVBoxLayout ()
       vbox.addLayout(hbox)
        
       btn = QPushButton()
       hbox.addWidget(btn)
    
    
       scroll = QScrollArea ()
       scroll.setWidget(gbox)
       scroll.setVerticalScrollBarPolicy(Qt.ScrollBarwaysOn)
       scroll.setHor....(..AlwaysOn)
       grid.addWidget(scrol)
    

    </>

    I wrote these codes off heart just to explain my point so I can be educated properly. From the codes, it can be seen that grid is the parent layout, and its layout is set on the QWidget subclass, window or self.

    Now, I set the gbox which is a group box on the scroll area. With the little I've studied from practices, anything coded within the gbox will be scrollable based on the code here, and anything outside the gbox won't be scrollable.

    Please is there a way I can make the whole contents of the Window scrollable? Can one use scroll.setLayout() on Qt instead of scroll.set() so as to make the whole components resting on that layout scrollable?

    Please, I am not an expert but an enthusiast who loves practicing, taking note of observations and then asking questions when necessary so I can be better enlightened and informed. Kindly enlighten me and help me

    Pl45m4P 1 Reply Last reply
    0
    • C CEO.

      Hello, please just as we have the .setWidget() method when using a QScrollArea, do we have the .setLayout() method for it? Please pardon the structure of my sentences.

      I mean this for example:
      <>

      Class app(QWidget):
        (the function def init and super codes)
          grid = QGridLayout ()
          self.setLayout(grid)
          vbox = QVBoxLayout()
          gbox = QGroupBox()
          grid.addWidget(gbox)
          gbox.setLayout(vbox)
          hbox = QVBoxLayout ()
         vbox.addLayout(hbox)
          
         btn = QPushButton()
         hbox.addWidget(btn)
      
      
         scroll = QScrollArea ()
         scroll.setWidget(gbox)
         scroll.setVerticalScrollBarPolicy(Qt.ScrollBarwaysOn)
         scroll.setHor....(..AlwaysOn)
         grid.addWidget(scrol)
      

      </>

      I wrote these codes off heart just to explain my point so I can be educated properly. From the codes, it can be seen that grid is the parent layout, and its layout is set on the QWidget subclass, window or self.

      Now, I set the gbox which is a group box on the scroll area. With the little I've studied from practices, anything coded within the gbox will be scrollable based on the code here, and anything outside the gbox won't be scrollable.

      Please is there a way I can make the whole contents of the Window scrollable? Can one use scroll.setLayout() on Qt instead of scroll.set() so as to make the whole components resting on that layout scrollable?

      Please, I am not an expert but an enthusiast who loves practicing, taking note of observations and then asking questions when necessary so I can be better enlightened and informed. Kindly enlighten me and help me

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @CEO

      Usually you don't set a QLayout to a QScrollArea since it kinda does the opposite thing.
      Btw: I hadn't realized that your other topic contains a question (i.e. this question, about how to use scrollArea). I thought it was a question to those, who read your "guide".

      Create your widget, apply a layout with the content and add this widget as such to the scrollArea with addWidget.
      The QScrollArea would be your centralWidget then (assuming you are using a QMainWindow). Keep in mind that the content of a QMainWindow cant take the whole window space. Some space at the top and bottom is reserved for toolBars, statusBars and other stuff.

      • https://doc.qt.io/qt-5/qmainwindow.html#qt-main-window-framework

      If I'm not wrong setLayout is only available because a QScrollArea is a widget itself, so it inherits this function from QWidget base class.
      Like almost everywhere, the same "rules" apply in programming: Just because you can do something, it doesn't mean that it's recommended to do or that you should do it :)

      Edit:

      Your question was already answered by @SGaist and @JoeCFD here:

      • https://forum.qt.io/topic/128783/qlayouts-in-a-qscrollarea/3

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      C 1 Reply Last reply
      2
      • Pl45m4P Pl45m4

        @CEO

        Usually you don't set a QLayout to a QScrollArea since it kinda does the opposite thing.
        Btw: I hadn't realized that your other topic contains a question (i.e. this question, about how to use scrollArea). I thought it was a question to those, who read your "guide".

        Create your widget, apply a layout with the content and add this widget as such to the scrollArea with addWidget.
        The QScrollArea would be your centralWidget then (assuming you are using a QMainWindow). Keep in mind that the content of a QMainWindow cant take the whole window space. Some space at the top and bottom is reserved for toolBars, statusBars and other stuff.

        • https://doc.qt.io/qt-5/qmainwindow.html#qt-main-window-framework

        If I'm not wrong setLayout is only available because a QScrollArea is a widget itself, so it inherits this function from QWidget base class.
        Like almost everywhere, the same "rules" apply in programming: Just because you can do something, it doesn't mean that it's recommended to do or that you should do it :)

        Edit:

        Your question was already answered by @SGaist and @JoeCFD here:

        • https://forum.qt.io/topic/128783/qlayouts-in-a-qscrollarea/3
        C Offline
        C Offline
        CEO.
        wrote on last edited by
        #3

        @Pl45m4 smiles.
        I believe I will be learning a lot from you because you seem more versatile and I notice you're always trying to help coders facing challenges resolve their issues. Thanks for this.

        I actually tried the scroll.setLayout(), no error in the code but the scrollArea became inactive. From your explanation and what I studied from RealPython.com site, it therefore means the QScrollArea is not a subclass of QWidget but a widget.

        Now, is there any way I can make the whole contents of a Window scrollable using the QWidgets class? I don't want to use QMainWindow class, I know I can use the .setCentralWidget() method there.

        How can I make a Window with different layouts of same level scrollable?

        Is it that there must be a parent layout?

        SGaistS 1 Reply Last reply
        0
        • C CEO.

          @Pl45m4 smiles.
          I believe I will be learning a lot from you because you seem more versatile and I notice you're always trying to help coders facing challenges resolve their issues. Thanks for this.

          I actually tried the scroll.setLayout(), no error in the code but the scrollArea became inactive. From your explanation and what I studied from RealPython.com site, it therefore means the QScrollArea is not a subclass of QWidget but a widget.

          Now, is there any way I can make the whole contents of a Window scrollable using the QWidgets class? I don't want to use QMainWindow class, I know I can use the .setCentralWidget() method there.

          How can I make a Window with different layouts of same level scrollable?

          Is it that there must be a parent layout?

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          @CEO said in I would need someone to teach me some things about QScrollArea.:

          I actually tried the scroll.setLayout(), no error in the code but the scrollArea became inactive. From your explanation and what I studied from RealPython.com site, it therefore means the QScrollArea is not a subclass of QWidget but a widget.

          You are wrong there, see the QScrollArea documentation. You'll see it inherits QAbstractScrollArea which inherits QFrame which inherits QWidget.

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

          C 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            @CEO said in I would need someone to teach me some things about QScrollArea.:

            I actually tried the scroll.setLayout(), no error in the code but the scrollArea became inactive. From your explanation and what I studied from RealPython.com site, it therefore means the QScrollArea is not a subclass of QWidget but a widget.

            You are wrong there, see the QScrollArea documentation. You'll see it inherits QAbstractScrollArea which inherits QFrame which inherits QWidget.

            C Offline
            C Offline
            CEO.
            wrote on last edited by
            #5

            @SGaist what is the QScrollArea? It is not a subclass of the QWidget. A layout manager can't be set on it.

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

              I gave you the full chain of inheritance back to QWidget.

              Yes you can put a layout on an QScrollArea. As already said by @Pl45m4, it's not what you do when you want to use QScrollArea to manage a widget bigger than for example the screen. Still, there might be cases where putting something in a layout on a QScrollArea could make sense. However I do not have an example at hand.

              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