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. PyQt6 inheritance
Qt 6.11 is out! See what's new in the release blog

PyQt6 inheritance

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 4.6k 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.
  • M MAX001

    @JonB Error
    08d11c23-938b-435b-8ab5-68cbd0885c60-image.png
    I will change to objects frame1 and frame2 Thanks

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

    @MAX001
    Recursion error: your Frame1 & Frame2 inherit App class. Your App class's __init__() creates Frame1 & Frame2 instances. Oh dear.

    I do not know what you are trying to achieve or how you think your code is intended to work.

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

      Hi,

      Your Frame1 and Frame2 classes are a pretty convoluted way to add QPushButtons to your main widget class. You should drop that useless inheritance (that is wrongly done) and simply build your GUI within 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

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Your Frame1 and Frame2 classes are a pretty convoluted way to add QPushButtons to your main widget class. You should drop that useless inheritance (that is wrongly done) and simply build your GUI within your main widget.

        M Offline
        M Offline
        MAX001
        wrote on last edited by
        #6

        @SGaist Sorry but how to implement it correctly?
        https://stackoverflow.com/questions/21702897/pyqt-class-inheritance I found something like that.
        But what to do with constructor __init__ what parameters should I pass

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

          If you re-read the examples from that thread, you'll see that the additional widget create inherit from QWidget and not from the MainWindow where they are used.

          In any case, as I already wrote, you are making things way too complicated for what you want to achieve.

          You can do everything in a single class.

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

          M 1 Reply Last reply
          2
          • M Offline
            M Offline
            MAX001
            wrote on last edited by
            #8

            I wanna create classes because i implemented it thought functions and i have more then 1000 row of code 😬
            And I have some similar frames witch i can make as object of Frame 3 for example and add .clicked.connect event for the necessary buttons related to certain objects
            As the result 1 class 3 objects of that class and less code

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

              Here you seem to go to the other extreme by trying to put everything in separate widgets which is not a good idea.

              The part you posted here really does not justify the added complexity you have.

              You should draw your UI and then the logic flow.

              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
              2
              • SGaistS SGaist

                If you re-read the examples from that thread, you'll see that the additional widget create inherit from QWidget and not from the MainWindow where they are used.

                In any case, as I already wrote, you are making things way too complicated for what you want to achieve.

                You can do everything in a single class.

                M Offline
                M Offline
                MAX001
                wrote on last edited by
                #10

                @SGaist Ok, I will have 1 class, what will I put in it?
                GridLayout 2 buttons and one image. How can I then replace QWidgets with other widgets and also how to create 3 objects of the same frames through this class. Wouldn't it be the same idea as with functions and 1000 lines of code?
                1bebf8d5-ea52-406e-99c9-e8b4a351c69d-image.png 4e9d4efc-4587-457b-86ca-5103645a4646-image.png

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

                  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.

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

                  M 2 Replies Last reply
                  1
                  • SGaistS SGaist

                    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.

                    M Offline
                    M Offline
                    MAX001
                    wrote on last edited by
                    #12

                    @SGaist Thank you very much, I'll try to implement.

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      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.

                      M Offline
                      M Offline
                      MAX001
                      wrote on last edited by MAX001
                      #13

                      @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)
                      
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • M MAX001

                        @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)
                        
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @MAX001 Why don't you simply use QStackedWidget @SGaist suggested instead of all these close()/show()?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        M 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @MAX001 Why don't you simply use QStackedWidget @SGaist suggested instead of all these close()/show()?

                          M Offline
                          M Offline
                          MAX001
                          wrote on last edited by
                          #15

                          @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 2 QStackedWidget pages in memory, which would be good.

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

                            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.

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

                            M 1 Reply Last reply
                            1
                            • SGaistS SGaist

                              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.

                              M Offline
                              M Offline
                              MAX001
                              wrote on last edited by
                              #17

                              @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).

                              JonBJ 1 Reply Last reply
                              0
                              • M MAX001

                                @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).

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

                                @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.

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

                                  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.

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

                                  M 1 Reply Last reply
                                  2
                                  • SGaistS SGaist

                                    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.

                                    M Offline
                                    M Offline
                                    MAX001
                                    wrote on last edited by
                                    #20

                                    @SGaist @JonB
                                    As far as I understand the process with memory, how it will be occupied, when something will be loaded or deleted is not clear.

                                    To begin with, I just need to make everything, connect to make it work and then look at the optimization.
                                    Thank you both for your advice.

                                    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