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. Merge two pieces of code
Forum Updated to NodeBB v4.3 + New Features

Merge two pieces of code

Scheduled Pinned Locked Moved Unsolved Qt for Python
102 Posts 5 Posters 38.2k 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
    #21

    As already suggested, the mouse handling methods for rubber shall not be in the MainWindow but in your class that shows the image.

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

      As already suggested, the mouse handling methods for rubber shall not be in the MainWindow but in your class that shows the image.

      J Offline
      J Offline
      john_hobbyist
      wrote on last edited by
      #22

      @SGaist So methods inside this method?

      def updateFrame(self):
      
      mrjjM 1 Reply Last reply
      0
      • J john_hobbyist

        @SGaist So methods inside this method?

        def updateFrame(self):
        
        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #23

        @john_hobbyist
        Hi
        Nope, it don't work like that. the mouse function overrides that Qt must call and
        they cant be in another function.

        under
        class MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
        def mousePressEvent(self, event):
        print("PRESS")

        and see if it types "PRESS" if you click on the "image"

        If it won't compile it means mpl_qt.FigureCanvasQTAgg is not a QWidget and
        its not really possible.

        J 2 Replies Last reply
        2
        • mrjjM mrjj

          @john_hobbyist
          Hi
          Nope, it don't work like that. the mouse function overrides that Qt must call and
          they cant be in another function.

          under
          class MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
          def mousePressEvent(self, event):
          print("PRESS")

          and see if it types "PRESS" if you click on the "image"

          If it won't compile it means mpl_qt.FigureCanvasQTAgg is not a QWidget and
          its not really possible.

          J Offline
          J Offline
          john_hobbyist
          wrote on last edited by john_hobbyist
          #24

          @mrjj I have just tried it. It prints "PRESS"!

          But again if I move the rubberband methods under MyMplCanvas and these:

              super().__init__(parent)
          
              self.rubberBand = None
              self.origin = None
          

          again the initial error...!!!

          mrjjM 1 Reply Last reply
          0
          • mrjjM mrjj

            @john_hobbyist
            Hi
            Nope, it don't work like that. the mouse function overrides that Qt must call and
            they cant be in another function.

            under
            class MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
            def mousePressEvent(self, event):
            print("PRESS")

            and see if it types "PRESS" if you click on the "image"

            If it won't compile it means mpl_qt.FigureCanvasQTAgg is not a QWidget and
            its not really possible.

            J Offline
            J Offline
            john_hobbyist
            wrote on last edited by
            #25
            This post is deleted!
            1 Reply Last reply
            0
            • J john_hobbyist

              @mrjj I have just tried it. It prints "PRESS"!

              But again if I move the rubberband methods under MyMplCanvas and these:

                  super().__init__(parent)
              
                  self.rubberBand = None
                  self.origin = None
              

              again the initial error...!!!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #26

              @john_hobbyist

              Ok that's good news so it is a QWidget. Then there is hope :)
              It reacted to the mousepress!

              What error ?

              the set_canvas one ?

              J 1 Reply Last reply
              1
              • mrjjM mrjj

                @john_hobbyist

                Ok that's good news so it is a QWidget. Then there is hope :)
                It reacted to the mousepress!

                What error ?

                the set_canvas one ?

                J Offline
                J Offline
                john_hobbyist
                wrote on last edited by john_hobbyist
                #27

                @mrjj :-)

                Traceback (most recent call last):
                  File "code.py", line 653, in <module>
                    window = OrthoView()
                  File "code.py", line 279, in __init__
                    self.plotCanvas = MyMplCanvas(self)
                  File "code.py", line 109, in __init__
                    super().__init__(parent)
                  File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5agg.py", line 21, in __init__
                    super().__init__(figure=figure)
                  File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5.py", line 214, in __init__
                    super().__init__(figure=figure)
                  File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1722, in __init__
                    figure.set_canvas(self)
                AttributeError: 'OrthoView' object has no attribute 'set_canvas'
                
                mrjjM 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #28

                  You do realize that your OrthoView does not have such a method ? That its base class (QMainWindow) does not either ?

                  Therefore since you are taking your code from another project, you might want to check the code of said project for that method.

                  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
                  • J john_hobbyist

                    @mrjj :-)

                    Traceback (most recent call last):
                      File "code.py", line 653, in <module>
                        window = OrthoView()
                      File "code.py", line 279, in __init__
                        self.plotCanvas = MyMplCanvas(self)
                      File "code.py", line 109, in __init__
                        super().__init__(parent)
                      File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5agg.py", line 21, in __init__
                        super().__init__(figure=figure)
                      File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5.py", line 214, in __init__
                        super().__init__(figure=figure)
                      File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1722, in __init__
                        figure.set_canvas(self)
                    AttributeError: 'OrthoView' object has no attribute 'set_canvas'
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #29

                    @john_hobbyist said in Merge two pieces of code:

                    set_canvas

                    But this is not related to the RubberBand thing. ?

                    if i look in the ORG project
                    https://github.com/kklmn/OrthoView/blob/master/OrthoView.py

                    there is no such method?
                    So did you add it or where does it come from ?

                    1 Reply Last reply
                    1
                    • J Offline
                      J Offline
                      john_hobbyist
                      wrote on last edited by john_hobbyist
                      #30

                      @SGaist, @mrjj : I have posted the code previously. But again I got the code form github repository and I inserted inside the code for rubberband (all those I have posted in my initial post) I execute the python code and again the same error.... Any ideas??

                      mrjjM 1 Reply Last reply
                      0
                      • J john_hobbyist

                        @SGaist, @mrjj : I have posted the code previously. But again I got the code form github repository and I inserted inside the code for rubberband (all those I have posted in my initial post) I execute the python code and again the same error.... Any ideas??

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #31

                        @john_hobbyist
                        Hi
                        the
                        'MyMplCanvas' object has no attribute 'rubberBand'
                        or the
                        object has no attribute 'set_canvas'

                        1 Reply Last reply
                        1
                        • J Offline
                          J Offline
                          john_hobbyist
                          wrote on last edited by
                          #32

                          Hi, sorry, what should I do?

                          mrjjM 1 Reply Last reply
                          0
                          • J john_hobbyist

                            Hi, sorry, what should I do?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #33

                            @john_hobbyist

                            hi
                            fix the error.
                            Tell what line number and what it says.

                            J 1 Reply Last reply
                            1
                            • mrjjM mrjj

                              @john_hobbyist

                              hi
                              fix the error.
                              Tell what line number and what it says.

                              J Offline
                              J Offline
                              john_hobbyist
                              wrote on last edited by
                              #34

                              @mrjj Hi, for the posted source code above...this is the error:

                              Traceback (most recent call last):
                                File "code.py", line 513, in <module>
                                  window = OrthoView()
                                File "code.py", line 279, in __init__
                                  self.plotCanvas = MyMplCanvas(self)
                                File "code.py", line 113, in __init__
                                  super().__init__(parent)
                                File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5agg.py", line 21, in __init__
                                  super().__init__(figure=figure)
                                File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5.py", line 214, in __init__
                                  super().__init__(figure=figure)
                                File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1722, in __init__
                                  figure.set_canvas(self)
                              AttributeError: 'OrthoView' object has no attribute 'set_canvas'
                              
                              
                              mrjjM 1 Reply Last reply
                              0
                              • J john_hobbyist

                                @mrjj Hi, for the posted source code above...this is the error:

                                Traceback (most recent call last):
                                  File "code.py", line 513, in <module>
                                    window = OrthoView()
                                  File "code.py", line 279, in __init__
                                    self.plotCanvas = MyMplCanvas(self)
                                  File "code.py", line 113, in __init__
                                    super().__init__(parent)
                                  File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5agg.py", line 21, in __init__
                                    super().__init__(figure=figure)
                                  File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backends/backend_qt5.py", line 214, in __init__
                                    super().__init__(figure=figure)
                                  File "/home/john/Desktop/test/venv/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1722, in __init__
                                    figure.set_canvas(self)
                                AttributeError: 'OrthoView' object has no attribute 'set_canvas'
                                
                                
                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #35

                                Hi

                                Ok so it has NOTHING to do with the rubberband at all.
                                Even before you try to insert that, the app was not working.

                                So it complains about
                                figure.set_canvas(self)

                                So perhaps you didnt copy the figure class etc ?

                                1 Reply Last reply
                                2
                                • J Offline
                                  J Offline
                                  john_hobbyist
                                  wrote on last edited by
                                  #36

                                  @mrjj I have copied everything from the github repository. The only small modification I did in rubberband code is that I changed this part:

                                  import sys
                                  
                                  from PySide2.QtCore import QRect
                                  from PySide2.QtCore import QSize
                                  from PySide2.QtWidgets import QWidget
                                  from PySide2.QtWidgets import QRubberBand
                                  from PySide2.QtWidgets import QApplication
                                  

                                  to this one:

                                  import rubberband
                                  import sys
                                  
                                  
                                  from PyQt5 import QtWidgets
                                  from PyQt5 import QtCore 
                                  
                                  
                                  from PyQt5.QtWidgets import * 
                                  from PyQt5 import QtCore, QtGui
                                  from PyQt5.QtGui import * 
                                  from PyQt5.QtCore import * 
                                  

                                  Could this be the source of the strange errors?

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

                                    Rather than just copying it, did you try to run the application from the repo as is ?

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

                                      Rather than just copying it, did you try to run the application from the repo as is ?

                                      J Offline
                                      J Offline
                                      john_hobbyist
                                      wrote on last edited by
                                      #38

                                      @SGaist Hi,yes, it runs without any problems!

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

                                        Then compare it to your version and see where the difference lies.

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

                                          Then compare it to your version and see where the difference lies.

                                          J Offline
                                          J Offline
                                          john_hobbyist
                                          wrote on last edited by
                                          #40

                                          @SGaist : I have tried any possible solution! Is there any other way to select an area on image apart from rubberband?

                                          JonBJ 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