Layout is changing when using QScrollArea in PyQt5
-
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?
-
I'd say it depends on what you are going to develop. For your current tests you can do everything in your QMainWindow.
-
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?
-
If you implement it in a separate Python file, the same way you do for other widgets, import and use.
-
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()
-
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.
-
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()
-
You forgot to set your QScrollArea as central widget.
-
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?
-
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.