Unsolved PyQt6 inheritance
-
Not counting the MainWindow, you need five custom widgets, one for each "Frame" and one for the "title, image, detect" piece.
Based on the green button, you will put these 4 widgets in a QStackedWidget in your MainWindow.
If you want, you can have a "Frame" class that provides the green button in a layout and where you will set the other widgets in so you don't have to repeat that part.
-
@SGaist Thank you very much, I'll try to implement.
-
@SGaist Sorry again!
It's ok with such inheritance ?
Is it possible that Frame2 covers Frame1?class App(QWidget): def __init__(self): super().__init__() self.grid = QGridLayout(self) self.frame1 = Frame1(self) self.frame2 = Frame2(self) self.frame3 = Frame3(self) self.grid.addWidget(self.frame1, 0, 0) self.grid.addWidget(self.frame2, 0, 0) self.grid.addWidget(self.frame3, 0, 0) self.frame2.close() self.frame3.close() def goto_frame1(self): self.frame2.close() self.frame1.show() def goto_frame2(self): self.frame3.close() self.frame2.show() def goto_frame3(self): self.frame2.close() self.frame3.show() def start_detection(self): self.frame1.close() self.frame2.show() class Frame1(QWidget): def __init__(self, parent): super().__init__(parent) self.grid = QGridLayout(self) self.start_button = QPushButton("START", self) self.grid.addWidget(self.start_button, 0, 0) self.start_button.clicked.connect(parent.start_detection) def clear_widgets(self): for i in range(self.grid.count()): self.grid.itemAt(i).widget().setParent(None) class Frame2(QWidget): def __init__(self, parent): super().__init__(parent) self.grid = QGridLayout(self) self.back_button = QPushButton("BACK", self) self.eye_detect_btn = QPushButton("Detect", self) self.grid.addWidget(self.eye_detect_btn, 0, 0) self.grid.addWidget(self.back_button, 1, 0) self.back_button.clicked.connect(parent.goto_frame1) self.eye_detect_btn.clicked.connect(parent.goto_frame3) def clear_widgets(self): for i in range(self.grid.count()): self.grid.itemAt(i).widget().setParent(None) class Frame3(QWidget): def __init__(self, parent): super().__init__(parent) self.grid = QGridLayout(self) self.back_button = QPushButton("BACK", self) self.eye_detect_btn = QPushButton("Frame 3", self) self.grid.addWidget(self.eye_detect_btn, 0, 0) self.grid.addWidget(self.back_button, 1, 0) self.back_button.clicked.connect(parent.goto_frame2) def clear_widgets(self): for i in range(self.grid.count()): self.grid.itemAt(i).widget().setParent(None)
-
-
@jsulm As I understand it, in
QStackedWidget
, all widgets are saved throughout the entire program, thus consuming memory. If it would be possible to delete them and, if necessary, reload them, so that there would always be only 1 or 2QStackedWidget
pages in memory, which would be good. -
While I appreciate your worries about memory consumption, the 4 panels you showed before do not warrant to be deleted and recreated every time.
You might want to give more details about the inner workings of your application so that we can better understand your situation.
-
@SGaist I want to recognize different objects in the application from the picture from the video and in real time through the camera. Now the camera and video are about 20 fps, and it takes up enough memory for processing, and it would be desirable that the application does not take up a lot of additional resources.
Therefore, I want to programm the application as efficiently as possible, but I don’t know what practices professionals use, they do everything through classes, but how, through functions, through
QStackedWidget
. What technique do they use to implement such applications (efficient and fast). -
@MAX001
I would not have thought that these widgets would occupy much memory relative to the image processing.
I am also not convinced that your hiding & showing of widgets is freeing the memory you think it is. -
What amount of memory are we talking about here ?
If your video pipeline eats 1Gb of memory per image then the small amount of widgets you use don't even enter the scale.Professionals, as you say, start by making things work, then optimize. Build the backend that processes the images, benchmark it, see where goes the memory. See if you can do something about that. Then test drive it for your various sources, and again measure.
Your current attempts to optimize your GUI, as already stated several times, is misguided. Make it simple, make it work, and only once you have something simple and clean will you see if there's really a need to do all these deletions/real-showing. As my fellows already wrote, deletion is not guaranteed to happen immediately and the memory released is not guaranteed to be returned to the OS instantly either.
-