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 39.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 mrjj
    15 Jul 2021, 21:05

    @john_hobbyist

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

    J Offline
    J Offline
    john_hobbyist
    wrote on 15 Jul 2021, 21:31 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'
    
    
    M 1 Reply Last reply 16 Jul 2021, 05:15
    0
    • J john_hobbyist
      15 Jul 2021, 21:31

      @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'
      
      
      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 16 Jul 2021, 05:15 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 16 Jul 2021, 09:07 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 16 Jul 2021, 09:11 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 16 Jul 2021, 11:01
          2
          • S SGaist
            16 Jul 2021, 09:11

            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 16 Jul 2021, 11:01 last edited by
            #38

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 16 Jul 2021, 11:52 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 16 Jul 2021, 12:32
              2
              • S SGaist
                16 Jul 2021, 11:52

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

                J Offline
                J Offline
                john_hobbyist
                wrote on 16 Jul 2021, 12:32 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?

                J 1 Reply Last reply 16 Jul 2021, 13:06
                0
                • J john_hobbyist
                  16 Jul 2021, 12:32

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 16 Jul 2021, 13:06 last edited by
                  #41

                  @john_hobbyist
                  I don't think so, rubber band is the right way to do it. Even if there were another way, like just clicking, I presume you would face the same issues getting it right as you are now for rubber band.

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    john_hobbyist
                    wrote on 16 Jul 2021, 15:50 last edited by
                    #42

                    I try for many hours to incorporate another code in the code from the github repository. I have fixed some errors (because it was written basically for PyQt4). But I cannot fix this error:

                    Traceback (most recent call last):
                      File "code_2.py", line 333, in <module>
                        screen_pixmap = QtGui.QPixmap.grabWidget(app.desktop()) 
                    NameError: name 'QtGui' is not defined
                    Segmentation fault (core dumped)
                    
                    

                    I change the QtWidgets to QtGui and I get this error:

                    Traceback (most recent call last):
                    File "code_2.py", line 333, in <module>
                      screen_pixmap = QtWidgets.QPixmap.grabWidget(app.desktop()) 
                    AttributeError: module 'PyQt5.QtWidgets' has no attribute 'QPixmap'
                    Segmentation fault (core dumped)
                    
                    

                    Any ideas??

                    M J 2 Replies Last reply 16 Jul 2021, 15:54
                    0
                    • J john_hobbyist
                      16 Jul 2021, 15:50

                      I try for many hours to incorporate another code in the code from the github repository. I have fixed some errors (because it was written basically for PyQt4). But I cannot fix this error:

                      Traceback (most recent call last):
                        File "code_2.py", line 333, in <module>
                          screen_pixmap = QtGui.QPixmap.grabWidget(app.desktop()) 
                      NameError: name 'QtGui' is not defined
                      Segmentation fault (core dumped)
                      
                      

                      I change the QtWidgets to QtGui and I get this error:

                      Traceback (most recent call last):
                      File "code_2.py", line 333, in <module>
                        screen_pixmap = QtWidgets.QPixmap.grabWidget(app.desktop()) 
                      AttributeError: module 'PyQt5.QtWidgets' has no attribute 'QPixmap'
                      Segmentation fault (core dumped)
                      
                      

                      Any ideas??

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 16 Jul 2021, 15:54 last edited by
                      #43

                      Hi
                      Are you using Qtpy5 ?

                      I think its
                      from PyQt5.QtGui import QPixmap

                      https://www.pythonguis.com/faq/adding-images-to-pyqt5-applications/

                      J 1 Reply Last reply 16 Jul 2021, 16:27
                      1
                      • M mrjj
                        16 Jul 2021, 15:54

                        Hi
                        Are you using Qtpy5 ?

                        I think its
                        from PyQt5.QtGui import QPixmap

                        https://www.pythonguis.com/faq/adding-images-to-pyqt5-applications/

                        J Offline
                        J Offline
                        john_hobbyist
                        wrote on 16 Jul 2021, 16:27 last edited by
                        #44

                        @mrjj I have just tried what you suggest... Nothing changed!

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          john_hobbyist
                          wrote on 16 Jul 2021, 16:35 last edited by john_hobbyist
                          #45

                          Below is the original code (source: https://stackoverflow.com/questions/34220275/how-to-select-a-region-with-qrubberband-on-a-qlabel-like-in-ksnapshot). I changed "PyQt4" -> "PyQt5" and "QtGui" -> "QtWidgets". Should I change anything else? Any ideas??

                          from PyQt4 import QtGui, QtCore
                          
                          class RubberbandEnhancedLabel(QtGui.QLabel):
                          
                              def __init__(self, parent=None):
                                  QtGui.QLabel.__init__(self, parent)
                                  self.selection = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self)
                          
                              def mousePressEvent(self, event):
                                  '''
                                      Mouse is pressed. If selection is visible either set dragging mode (if close to border) or hide selection.
                                      If selection is not visible make it visible and start at this point.
                                  '''
                          
                                  if event.button() == QtCore.Qt.LeftButton:
                          
                                      position = QtCore.QPoint(event.pos())
                                      if self.selection.isVisible():
                                          # visible selection
                                          if (self.upper_left - position).manhattanLength() < 20:
                                              # close to upper left corner, drag it
                                              self.mode = "drag_upper_left"
                                          elif (self.lower_right - position).manhattanLength() < 20:
                                              # close to lower right corner, drag it
                                              self.mode = "drag_lower_right"
                                          else:
                                              # clicked somewhere else, hide selection
                                              self.selection.hide()
                                      else:
                                          # no visible selection, start new selection
                                          self.upper_left = position
                                          self.lower_right = position
                                          self.mode = "drag_lower_right"
                                          self.selection.show()
                          
                              def mouseMoveEvent(self, event):
                                  '''
                                      Mouse moved. If selection is visible, drag it according to drag mode.
                                  '''
                                  if self.selection.isVisible():
                                      # visible selection
                                      if self.mode is "drag_lower_right":
                                          self.lower_right = QtCore.QPoint(event.pos())
                                      elif self.mode is "drag_upper_left":
                                          self.upper_left = QtCore.QPoint(event.pos())
                                      # update geometry
                                      self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())
                          
                          app = QtGui.QApplication([])
                          
                          screen_pixmap = QtGui.QPixmap.grabWindow(app.desktop().winId())
                          
                          window = QtGui.QWidget()
                          layout = QtGui.QVBoxLayout(window)
                          label = RubberbandEnhancedLabel()
                          label.setPixmap(screen_pixmap)
                          layout.addWidget(label)
                          geometry = app.desktop().availableGeometry()
                          window.setFixedSize(geometry.width(), geometry.height())
                          window.show()
                          app.exec_()
                          
                          J 1 Reply Last reply 22 Jul 2021, 19:53
                          0
                          • J john_hobbyist
                            16 Jul 2021, 15:50

                            I try for many hours to incorporate another code in the code from the github repository. I have fixed some errors (because it was written basically for PyQt4). But I cannot fix this error:

                            Traceback (most recent call last):
                              File "code_2.py", line 333, in <module>
                                screen_pixmap = QtGui.QPixmap.grabWidget(app.desktop()) 
                            NameError: name 'QtGui' is not defined
                            Segmentation fault (core dumped)
                            
                            

                            I change the QtWidgets to QtGui and I get this error:

                            Traceback (most recent call last):
                            File "code_2.py", line 333, in <module>
                              screen_pixmap = QtWidgets.QPixmap.grabWidget(app.desktop()) 
                            AttributeError: module 'PyQt5.QtWidgets' has no attribute 'QPixmap'
                            Segmentation fault (core dumped)
                            
                            

                            Any ideas??

                            J Offline
                            J Offline
                            JonB
                            wrote on 16 Jul 2021, 17:34 last edited by JonB
                            #46

                            @john_hobbyist said in Merge two pieces of code:

                            File "code_2.py", line 333, in <module>
                              screen_pixmap = QtWidgets.QPixmap.grabWidget(app.desktop()) 
                            AttributeError: module 'PyQt5.QtWidgets' has no attribute 'QPixmap'
                            

                            QPixMap is not a widget so it won't be in QtWidgets. It will be in QtGui. As per https://doc.qt.io/qt-5/qpixmap.html.

                            I changed "PyQt4" -> "PyQt5" and "QtGui" -> "QtWidgets".

                            You are supposed to be selective about that. Nobody said everything in QtGui changed to QtWidgets between 4 & 5. Just widgetty stuff!

                            J 1 Reply Last reply 17 Jul 2021, 16:55
                            2
                            • J JonB
                              16 Jul 2021, 17:34

                              @john_hobbyist said in Merge two pieces of code:

                              File "code_2.py", line 333, in <module>
                                screen_pixmap = QtWidgets.QPixmap.grabWidget(app.desktop()) 
                              AttributeError: module 'PyQt5.QtWidgets' has no attribute 'QPixmap'
                              

                              QPixMap is not a widget so it won't be in QtWidgets. It will be in QtGui. As per https://doc.qt.io/qt-5/qpixmap.html.

                              I changed "PyQt4" -> "PyQt5" and "QtGui" -> "QtWidgets".

                              You are supposed to be selective about that. Nobody said everything in QtGui changed to QtWidgets between 4 & 5. Just widgetty stuff!

                              J Offline
                              J Offline
                              john_hobbyist
                              wrote on 17 Jul 2021, 16:55 last edited by john_hobbyist
                              #47

                              @JonB: Hi, as I posted previously:

                              Traceback (most recent call last):
                                File "code_2.py", line 333, in <module>
                                  screen_pixmap = QtGui.QPixmap.grabWidget(app.desktop()) 
                              NameError: name 'QtGui' is not defined
                              Segmentation fault (core dumped)
                              

                              I have also tried this....

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 17 Jul 2021, 17:02 last edited by
                                #48

                                You did not import the module.

                                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 17 Jul 2021, 17:06
                                1
                                • S SGaist
                                  17 Jul 2021, 17:02

                                  You did not import the module.

                                  J Offline
                                  J Offline
                                  john_hobbyist
                                  wrote on 17 Jul 2021, 17:06 last edited by
                                  #49

                                  @SGaist: Something else than these?

                                  import os
                                  import sys
                                  import cv2
                                  from matplotlib.figure import Figure
                                  import numpy as np
                                  from PyQt5.QtGui import QIcon
                                  from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget,QAction, QFileDialog, QMenu, 
                                                               QToolBar, QHBoxLayout, QTreeView, QFileSystemModel, QSizePolicy, 
                                                               QMessageBox)
                                  from PyQt5.QtCore import Qt, QDir, QStandardPaths, QFileInfo
                                  import matplotlib.backends.backend_qt5agg as mpl_qt
                                  from PyQt5 import QtWidgets, QtCore
                                  from PyQt5.QtGui import QPixmap
                                  
                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 17 Jul 2021, 17:07 last edited by
                                    #50

                                    You import QPixmap directly so use it directly as well.

                                    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 17 Jul 2021, 17:25
                                    1
                                    • S SGaist
                                      17 Jul 2021, 17:07

                                      You import QPixmap directly so use it directly as well.

                                      J Offline
                                      J Offline
                                      john_hobbyist
                                      wrote on 17 Jul 2021, 17:25 last edited by
                                      #51

                                      @SGaist: I used this:

                                      screen_pixmap = QPixmap.grabWidget(app.desktop())
                                      

                                      I get this:

                                      Traceback (most recent call last):
                                        File "code_2.py", line 335, in <module>
                                          screen_pixmap = QPixmap.grabWidget(app.desktop()) 
                                      AttributeError: type object 'QPixmap' has no attribute 'grabWidget'
                                      Segmentation fault (core dumped)
                                      
                                      

                                      I followed this: https://stackoverflow.com/questions/29260686/qpixmap-has-no-attribute-grabwindow

                                      and I changed to this:

                                      screen_pixmap = QScreen.grabWidget(app.desktop())
                                      

                                      and I get this:

                                      Traceback (most recent call last):
                                        File "code_2.py", line 336, in <module>
                                          screen_pixmap = QScreen.grabWidget(app.desktop()) 
                                      AttributeError: type object 'QScreen' has no attribute 'grabWidget'
                                      Segmentation fault (core dumped)
                                      

                                      When I changed to this:

                                      screen_pixmap = QScreen.grabWindow(app.desktop().winId())
                                      

                                      I get this:

                                      Must construct a QApplication first.
                                      Traceback (most recent call last):
                                        File "code_2.py", line 337, in <module>
                                          screen_pixmap = QScreen.grabWindow(app.desktop().winId()) 
                                      AttributeError: 'NoneType' object has no attribute 'winId'
                                      Segmentation fault (core dumped)
                                      

                                      When I changed to this:

                                      screen_pixmap = QScreen.grabWindow(app.desktop())
                                      

                                      I get this:

                                      Must construct a QApplication first.
                                      Traceback (most recent call last):
                                        File "code_2.py", line 338, in <module>
                                          screen_pixmap = QScreen.grabWindow(app.desktop())#.winId()) 
                                      TypeError: grabWindow(self, PyQt5.sip.voidptr, x: int = 0, y: int = 0, width: int = -1, height: int = -1): first argument of unbound method must have type 'QScreen'
                                      Segmentation fault (core dumped)
                                      

                                      I didn't find something about this...

                                      J 1 Reply Last reply 17 Jul 2021, 18:31
                                      0
                                      • J john_hobbyist
                                        17 Jul 2021, 17:25

                                        @SGaist: I used this:

                                        screen_pixmap = QPixmap.grabWidget(app.desktop())
                                        

                                        I get this:

                                        Traceback (most recent call last):
                                          File "code_2.py", line 335, in <module>
                                            screen_pixmap = QPixmap.grabWidget(app.desktop()) 
                                        AttributeError: type object 'QPixmap' has no attribute 'grabWidget'
                                        Segmentation fault (core dumped)
                                        
                                        

                                        I followed this: https://stackoverflow.com/questions/29260686/qpixmap-has-no-attribute-grabwindow

                                        and I changed to this:

                                        screen_pixmap = QScreen.grabWidget(app.desktop())
                                        

                                        and I get this:

                                        Traceback (most recent call last):
                                          File "code_2.py", line 336, in <module>
                                            screen_pixmap = QScreen.grabWidget(app.desktop()) 
                                        AttributeError: type object 'QScreen' has no attribute 'grabWidget'
                                        Segmentation fault (core dumped)
                                        

                                        When I changed to this:

                                        screen_pixmap = QScreen.grabWindow(app.desktop().winId())
                                        

                                        I get this:

                                        Must construct a QApplication first.
                                        Traceback (most recent call last):
                                          File "code_2.py", line 337, in <module>
                                            screen_pixmap = QScreen.grabWindow(app.desktop().winId()) 
                                        AttributeError: 'NoneType' object has no attribute 'winId'
                                        Segmentation fault (core dumped)
                                        

                                        When I changed to this:

                                        screen_pixmap = QScreen.grabWindow(app.desktop())
                                        

                                        I get this:

                                        Must construct a QApplication first.
                                        Traceback (most recent call last):
                                          File "code_2.py", line 338, in <module>
                                            screen_pixmap = QScreen.grabWindow(app.desktop())#.winId()) 
                                        TypeError: grabWindow(self, PyQt5.sip.voidptr, x: int = 0, y: int = 0, width: int = -1, height: int = -1): first argument of unbound method must have type 'QScreen'
                                        Segmentation fault (core dumped)
                                        

                                        I didn't find something about this...

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 17 Jul 2021, 18:31 last edited by
                                        #52

                                        @john_hobbyist
                                        The last answer in the stackoverflow link you quoted claims to have "Full example for PyQt5". Did you at least try that? Not your own version of it, just exactly the example given there.

                                        1 Reply Last reply
                                        0
                                        • J Offline
                                          J Offline
                                          john_hobbyist
                                          wrote on 17 Jul 2021, 18:36 last edited by john_hobbyist
                                          #53

                                          @JonB Yes, it works, but I try different rubberband code...please look at my first posts up...

                                          1 Reply Last reply
                                          0

                                          43/102

                                          16 Jul 2021, 15:54

                                          • Login

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