Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QGraphicsView showEvent not working.
Forum Update on Monday, May 27th 2025

QGraphicsView showEvent not working.

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyqt5showevent
23 Posts 6 Posters 3.9k 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.
  • J jsulm
    28 Jun 2021, 05:47

    @mrjj said in QGraphicsView showEvent not working.:

    how can it be anything but a member function?

    Indentations define the scope in Python, like {} do in C++.
    If the code is really like that then showEvent is indeed not a method in PhotoViewer.

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 28 Jun 2021, 05:51 last edited by
    #5

    @jsulm
    Hi
    I know about the invisible white space for scope control but i do wonder what it then
    defaults too. a global function ? i guess since then the scope is not the class one.
    There is no such thing as "override" to make sure you do in fact override ?

    J 1 Reply Last reply 28 Jun 2021, 05:53
    0
    • J Offline
      J Offline
      jeremy_k
      wrote on 28 Jun 2021, 05:53 last edited by
      #6

      I believe that self is just a convention. The first parameter of a non-static class function receives the instance of the class. There's nothing stopping you from using this or anything else. There's also nothing stopping a non-member function from having a first argument called self.

      class MyClass:
          def __init__(this):
              this.value = 1
          def f(the_object):
              return the_object.value
      
      def callTheFunction(self):
          print(self.f())
      
      instance = MyClass()
      callTheFunction(instance)
      

      Expected output: 1

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • M mrjj
        28 Jun 2021, 05:51

        @jsulm
        Hi
        I know about the invisible white space for scope control but i do wonder what it then
        defaults too. a global function ? i guess since then the scope is not the class one.
        There is no such thing as "override" to make sure you do in fact override ?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 28 Jun 2021, 05:53 last edited by
        #7

        @mrjj said in QGraphicsView showEvent not working.:

        defaults too. a global function

        Well, just a function.
        I'm not sure there is something like "override" - I did not do much Python in last years :-)

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 28 Jun 2021, 06:00 last edited by
          #8

          @jeremy_k
          ok. That's actually much like c++ except you don't have to see, write or care about it :)
          Thank you.

          @jsulm
          ok and not a function in function, but simply just a file scope ? function.

          J 1 Reply Last reply 28 Jun 2021, 06:06
          0
          • M mrjj
            28 Jun 2021, 06:00

            @jeremy_k
            ok. That's actually much like c++ except you don't have to see, write or care about it :)
            Thank you.

            @jsulm
            ok and not a function in function, but simply just a file scope ? function.

            J Offline
            J Offline
            jeremy_k
            wrote on 28 Jun 2021, 06:06 last edited by jeremy_k
            #9

            @mrjj said in QGraphicsView showEvent not working.:

            @jeremy_k
            ok. That's actually much like c++ except you don't have to see, write or care about it :)

            Python is great like that. It pushes you to do it yourself, or use tooling to solve problems. The tooling is unable to give definitive answers because so many typos result in legal code with different behavior. I'm ready to go back to C++.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            M 1 Reply Last reply 28 Jun 2021, 06:18
            1
            • J jeremy_k
              28 Jun 2021, 06:06

              @mrjj said in QGraphicsView showEvent not working.:

              @jeremy_k
              ok. That's actually much like c++ except you don't have to see, write or care about it :)

              Python is great like that. It pushes you to do it yourself, or use tooling to solve problems. The tooling is unable to give definitive answers because so many typos result in legal code with different behavior. I'm ready to go back to C++.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 28 Jun 2021, 06:18 last edited by
              #10

              @jeremy_k
              Well, i had very little experience with it. back when it was kinda new, i did fool around with it a bit and
              then mail the code to home. At home, the program suddenly didn't run as expected and i realized how deadly spaces can be and that was the end of my excitement over the language.

              But its very popular. So I guess it does works well for some people.

              1 Reply Last reply
              0
              • J jeremy_k
                28 Jun 2021, 03:25

                The indentation is wrong for that definition of showEvent() to be part of PhotoViewer.

                D Offline
                D Offline
                darrenleeleelee1
                wrote on 28 Jun 2021, 06:20 last edited by
                #11

                @jeremy_k sorry, what did you mean, I do not understand well.

                M J 2 Replies Last reply 28 Jun 2021, 06:22
                0
                • D darrenleeleelee1
                  28 Jun 2021, 06:20

                  @jeremy_k sorry, what did you mean, I do not understand well.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 28 Jun 2021, 06:22 last edited by
                  #12

                  @darrenleeleelee1

                  Hi
                  The function is too much intended so its not part of the class

                  alt text

                  1 Reply Last reply
                  1
                  • D darrenleeleelee1
                    28 Jun 2021, 06:20

                    @jeremy_k sorry, what did you mean, I do not understand well.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 28 Jun 2021, 06:22 last edited by
                    #13

                    @darrenleeleelee1

                    class PhotoViewer(QtWidgets.QGraphicsView):
                        tool_lock = 'mouse'
                        def __init__(self, parent):
                            ...
                        
                        def showEvent(self, event):
                            super().showEvent(event)
                            print("Test")
                            self.showEventHelper()
                        def showEventHelper(self):
                            self.fitInView()
                    

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

                    D 2 Replies Last reply 28 Jun 2021, 06:24
                    3
                    • J jsulm
                      28 Jun 2021, 06:22

                      @darrenleeleelee1

                      class PhotoViewer(QtWidgets.QGraphicsView):
                          tool_lock = 'mouse'
                          def __init__(self, parent):
                              ...
                          
                          def showEvent(self, event):
                              super().showEvent(event)
                              print("Test")
                              self.showEventHelper()
                          def showEventHelper(self):
                              self.fitInView()
                      
                      D Offline
                      D Offline
                      darrenleeleelee1
                      wrote on 28 Jun 2021, 06:24 last edited by
                      #14

                      @jsulm oh, sorry I am not notice that, but this is not a problem, it way my typo when I copy from my vscode, in my vscode is

                      class PhotoViewer(QtWidgets.QGraphicsView):
                          tool_lock = 'mouse'
                          def __init__(self, parent):
                              super(PhotoViewer, self).__init__(parent)
                              ...
                          def showEvent(self, event):
                              super().showEvent(event)
                              print("Test")
                              self.showEventHelper()
                          def showEventHelper(self):
                              self.fitInView()
                      

                      and the problem still not solve.

                      1 Reply Last reply
                      0
                      • J jsulm
                        28 Jun 2021, 06:22

                        @darrenleeleelee1

                        class PhotoViewer(QtWidgets.QGraphicsView):
                            tool_lock = 'mouse'
                            def __init__(self, parent):
                                ...
                            
                            def showEvent(self, event):
                                super().showEvent(event)
                                print("Test")
                                self.showEventHelper()
                            def showEventHelper(self):
                                self.fitInView()
                        
                        D Offline
                        D Offline
                        darrenleeleelee1
                        wrote on 28 Jun 2021, 06:25 last edited by darrenleeleelee1
                        #15

                        @jsulm sorry it is my mistake, the print("Test") still not be run.Even when I correct the scope of functions.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 28 Jun 2021, 20:56 last edited by
                          #16

                          Hi,

                          There's an issue with your init function. The first parameter shall be a scene, the second is the parent.

                          See the class documentation

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

                          J 1 Reply Last reply 28 Jun 2021, 23:03
                          0
                          • S SGaist
                            28 Jun 2021, 20:56

                            Hi,

                            There's an issue with your init function. The first parameter shall be a scene, the second is the parent.

                            See the class documentation

                            J Offline
                            J Offline
                            jeremy_k
                            wrote on 28 Jun 2021, 23:03 last edited by
                            #17

                            @SGaist I wonder if that documentation is malformatted due to Python not supporting function overloading. The highlighted line indicates two arguments for the constructor. Inset below that appears to be a second constructor version that only accepts an optional QWidget parent. The two sentences at the end initially attracted my attention.

                            1532348d-e0f8-4cdf-b1c8-4722c85296db-image.png

                            Asking a question about code? http://eel.is/iso-c++/testcase/

                            J 1 Reply Last reply 29 Jun 2021, 06:50
                            0
                            • J jeremy_k
                              28 Jun 2021, 23:03

                              @SGaist I wonder if that documentation is malformatted due to Python not supporting function overloading. The highlighted line indicates two arguments for the constructor. Inset below that appears to be a second constructor version that only accepts an optional QWidget parent. The two sentences at the end initially attracted my attention.

                              1532348d-e0f8-4cdf-b1c8-4722c85296db-image.png

                              J Offline
                              J Offline
                              JonB
                              wrote on 29 Jun 2021, 06:50 last edited by
                              #18

                              @jeremy_k
                              Are you just commenting on the confusion, or have you changed your code? Whatever the case you will need to set the view's scene, either through the constructor or via setScene().

                              J 1 Reply Last reply 29 Jun 2021, 07:19
                              0
                              • J JonB
                                29 Jun 2021, 06:50

                                @jeremy_k
                                Are you just commenting on the confusion, or have you changed your code? Whatever the case you will need to set the view's scene, either through the constructor or via setScene().

                                J Offline
                                J Offline
                                jeremy_k
                                wrote on 29 Jun 2021, 07:19 last edited by
                                #19

                                @JonB It's not my code.

                                I'm not convinced that misuse of the constructor which doesn't throw an exception or lead to program termination will cause a correctly implemented showEvent() to be ignored. A view without a scene is blank, but still a valid and showable QWidget.

                                import PyQt5.QtWidgets as QtWidgets
                                
                                class View(QtWidgets.QGraphicsView):
                                    def showEvent(self, event):
                                        print("showEvent")
                                        super().showEvent(event)
                                
                                app = QtWidgets.QApplication([])
                                view = View()
                                view.show()
                                
                                ~:$ python test.py
                                showEvent
                                

                                Asking a question about code? http://eel.is/iso-c++/testcase/

                                J 1 Reply Last reply 29 Jun 2021, 07:27
                                0
                                • J jeremy_k
                                  29 Jun 2021, 07:19

                                  @JonB It's not my code.

                                  I'm not convinced that misuse of the constructor which doesn't throw an exception or lead to program termination will cause a correctly implemented showEvent() to be ignored. A view without a scene is blank, but still a valid and showable QWidget.

                                  import PyQt5.QtWidgets as QtWidgets
                                  
                                  class View(QtWidgets.QGraphicsView):
                                      def showEvent(self, event):
                                          print("showEvent")
                                          super().showEvent(event)
                                  
                                  app = QtWidgets.QApplication([])
                                  view = View()
                                  view.show()
                                  
                                  ~:$ python test.py
                                  showEvent
                                  
                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 29 Jun 2021, 07:27 last edited by JonB
                                  #20

                                  @jeremy_k said in QGraphicsView showEvent not working.:

                                  A view without a scene is blank, but still a valid and showable QWidget.

                                  I was not aware of this. Your code shows showEvent being printed. Originally you said "Test never be printed out". So what is the situation?

                                  J 1 Reply Last reply 29 Jun 2021, 07:32
                                  0
                                  • J JonB
                                    29 Jun 2021, 07:27

                                    @jeremy_k said in QGraphicsView showEvent not working.:

                                    A view without a scene is blank, but still a valid and showable QWidget.

                                    I was not aware of this. Your code shows showEvent being printed. Originally you said "Test never be printed out". So what is the situation?

                                    J Offline
                                    J Offline
                                    jeremy_k
                                    wrote on 29 Jun 2021, 07:32 last edited by jeremy_k
                                    #21

                                    @JonB said in QGraphicsView showEvent not working.:

                                    @jeremy_k said in QGraphicsView showEvent not working.:

                                    A view without a scene is blank, but still a valid and showable QWidget.

                                    I was not aware of this. Your code shows showEvent being printed. Originally you said "Test never be printed out".

                                    I'm not the OP.

                                    Asking a question about code? http://eel.is/iso-c++/testcase/

                                    J 1 Reply Last reply 29 Jun 2021, 07:35
                                    1
                                    • J jeremy_k
                                      29 Jun 2021, 07:32

                                      @JonB said in QGraphicsView showEvent not working.:

                                      @jeremy_k said in QGraphicsView showEvent not working.:

                                      A view without a scene is blank, but still a valid and showable QWidget.

                                      I was not aware of this. Your code shows showEvent being printed. Originally you said "Test never be printed out".

                                      I'm not the OP.

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 29 Jun 2021, 07:35 last edited by JonB
                                      #22

                                      @jeremy_k
                                      Oh, LOL, sorry about that! Well @darrenleeleelee1, what is the difference between your case where you say showEvent() is not hit and @jeremy_k's case where he shows it being hit? Or has this been solved and I am not understanding?!

                                      1 Reply Last reply
                                      1
                                      • D Offline
                                        D Offline
                                        darrenleeleelee1
                                        wrote on 29 Jun 2021, 11:30 last edited by
                                        #23

                                        thanks guy I get my goal by the other way

                                        1 Reply Last reply
                                        0

                                        14/23

                                        28 Jun 2021, 06:24

                                        • Login

                                        • Login or register to search.
                                        14 out of 23
                                        • First post
                                          14/23
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved