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. Layout is changing when using QScrollArea in PyQt5
Forum Updated to NodeBB v4.3 + New Features

Layout is changing when using QScrollArea in PyQt5

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 2 Posters 1.3k Views 2 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 Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    It's up to you but I do not see any issue in using several QScrollArea. That will likely make your coding easier.

    As for the resizing, the idea is to react when the current widget changes and trigger a resize then. However, since you are starting having your big sized widget each in a QScrollArea will be less trouble.

    Since you are using a QMainWindow, you should also set your widget stack as central widget rather than manually placing it.

    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
    • D Offline
      D Offline
      deleted412
      wrote on last edited by
      #5

      Thanks for your help. Is it better practice to make separate classes for my QScrollAreas? Or can I include this code in my class of QMainWindow?

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

        I'd say it depends on what you are going to develop. For your current tests you can do everything in your QMainWindow.

        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
        • D Offline
          D Offline
          deleted412
          wrote on last edited by
          #7

          Thanks. For practice, I'm attempting to create a different QScrollArea in a separate class. How would I then attach this separate class to my QMainWindow?

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

            If you implement it in a separate Python file, the same way you do for other widgets, import and use.

            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
            • D Offline
              D Offline
              deleted412
              wrote on last edited by
              #9

              Okay, I understand. However, how would I accomplish this if I were to add these classes in the same Python file, i.e., in the same main() function as my QMainWindow?

              For example, I would like to pass a variable from my QMainWindow into a separate QScrollArea class. However, I would like the QScrollArea to then be attached to my QMainWindow, which does not happen in my test:

              from PyQt5.QtGui import *
              from PyQt5.QtWidgets import *
              from PyQt5.QtCore import *
              import sys
              
              
              class FirstWindow(QMainWindow):
                  def __init__(self):
                      super(FirstWindow, self).__init__()
              
                      self.setGeometry(500, 600, 600, 1000)
              
                      self.first = First("Test String")
              
              
              class First(QScrollArea):
                  def __init__(self, label_text):
                      super(First, self).__init__()
              
                      self.setGeometry(100, 300, 400, 400)
                      self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
                      self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
                      self.setWidgetResizable(True)
              
                      self.layout_1 = QVBoxLayout()
              
                      self.scroll_widget1 = QWidget(self)
                      self.scroll_widget1.setGeometry(100, 300, 398, 398)
              
                      self.scroll_widget1.setLayout(self.layout_1)
              
                      self.setWidget(self.scroll_widget1)
              
              
                      for i in range(50):
                          self.label = QLabel(self)
                          self.label.setText(label_text)
                          self.layout_1.addWidget(self.label)
              
                      self.show()
              
              
              def main():
                  app = QApplication(sys.argv)
                  win = FirstWindow()
                  win.show()
                  app.exec_()
              
              main()
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #10

                Do not call show in your subclass constructor, it's not its role to say when to be shown.

                I think you are over engineering things. A custom widget that you create should be used in the same manner as any provided by Qt.

                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
                • D Offline
                  D Offline
                  deleted412
                  wrote on last edited by
                  #11

                  Thanks! So this example is acceptable/best practice when incorporating other widgets into my QMainWindow?

                  from PyQt5.QtGui import *
                  from PyQt5.QtWidgets import *
                  from PyQt5.QtCore import *
                  import sys
                  
                  
                  class FirstWindow(QMainWindow):
                      def __init__(self):
                          super(FirstWindow, self).__init__()
                  
                          self.setGeometry(500, 600, 600, 1000)
                  
                          self.scroll = QScrollArea(self)
                          self.scroll.setGeometry(100, 300, 400, 400)
                          self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
                          self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
                          self.scroll.setWidgetResizable(True)
                  
                          self.layout_1 = QVBoxLayout()
                  
                          self.scroll_widget1 = QWidget(self)
                          self.scroll_widget1.setGeometry(100, 300, 398, 398)
                  
                          self.scroll_widget1.setLayout(self.layout_1)
                  
                          self.scroll.setWidget(self.scroll_widget1)
                  
                          for i in range(50):
                              self.label = QLabel(self)
                              self.label.setText("Test String")
                              self.layout_1.addWidget(self.label)
                  
                  def main():
                      app = QApplication(sys.argv)
                      win = FirstWindow()
                      win.show()
                      app.exec_()
                  
                  main()
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    You forgot to set your QScrollArea as central widget.

                    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
                    • D Offline
                      D Offline
                      deleted412
                      wrote on last edited by
                      #13

                      Thanks for the reminder. Why would you say that this has to be the central widget? Suppose I add a title to the GUI, this would not be the central widget?

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

                        Just because you are using a QMainWindow as base class of your main widget. Otherwise, you should put it in a layout in your main widget.

                        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