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 44.4k 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.
  • J john_hobbyist

    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??

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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
    2
    • JonBJ JonB

      @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 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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        1
        • SGaistS SGaist

          You did not import the module.

          J Offline
          J Offline
          john_hobbyist
          wrote on 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            1
            • SGaistS SGaist

              You import QPixmap directly so use it directly as well.

              J Offline
              J Offline
              john_hobbyist
              wrote on 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...

              JonBJ 1 Reply Last reply
              0
              • J john_hobbyist

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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 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
                  • J Offline
                    J Offline
                    john_hobbyist
                    wrote on last edited by
                    #54
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      john_hobbyist
                      wrote on last edited by
                      #55

                      What modifications should I do in the command?

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

                        I am trying to modify/incorporate from the correct running code from the stackoverflow that @JonB suggested. With this:

                        screen_pixmap = QScreen.grabWindow(app.primaryScreen(),QApplication.desktop().winId()).save(filename, 'png')
                        

                        I get this:

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

                        And with this

                        screen_pixmap = QScreen.grabWindow(app.primaryScreen(),QApplication.desktop()).save(filename, 'png')
                        

                        I get again this:

                         construct a QApplication first.
                        Traceback (most recent call last):
                          File "code_2.py", line 341, in <module>
                            screen_pixmap = QScreen.grabWindow(app.primaryScreen(),QApplication.desktop()).save(filename, 'png')
                        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)
                        
                        
                        

                        in which I cannot find a reliable solution from google/stackoverflow...

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

                          @john_hobbyist said in Merge two pieces of code:

                          grabWindow

                          This is not a class method, you need an instance of QScreen.

                          In what way is that related to your original QRubberBand question ?

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

                          J SGaistS 2 Replies Last reply
                          1
                          • SGaistS SGaist

                            @john_hobbyist said in Merge two pieces of code:

                            grabWindow

                            This is not a class method, you need an instance of QScreen.

                            In what way is that related to your original QRubberBand question ?

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

                            @SGaist Yes, I try to use stackoverflow code that has an approach for what I am trying so many days to do. Please look previous posts. And these are errors...

                            1 Reply Last reply
                            0
                            • SGaistS SGaist

                              @john_hobbyist said in Merge two pieces of code:

                              grabWindow

                              This is not a class method, you need an instance of QScreen.

                              In what way is that related to your original QRubberBand question ?

                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #59

                              I already answered to the errors you are getting.

                              @SGaist said in Merge two pieces of code:

                              This is not a class method, you need an instance of QScreen.

                              You really should read the documentation of the classes you are using rather than just blindingly copy paste.

                              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
                              3
                              • J Offline
                                J Offline
                                john_hobbyist
                                wrote on last edited by
                                #60

                                I have been making many changes all day. The rest of the application runs, but nowhere I can see a rubberband rectangle in order to select/printscreen an area. I get these warnings though...I am searching it, but any idea would help...

                                QWidget::paintEngine: Should no longer be called
                                QPainter::begin: Paint device returned engine == 0, type: 1
                                QPainter::end: Painter not active, aborted
                                
                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #61

                                  Where exactly are you doing custom painting ?

                                  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
                                  1
                                  • J Offline
                                    J Offline
                                    john_hobbyist
                                    wrote on last edited by john_hobbyist
                                    #62

                                    I don't know! This is the modified last part of my code:

                                    if __name__ == "__main__":
                                        app = QApplication(sys.argv)
                                        icon = QIcon(os.path.join(selfDir, '_static', 'orthoview.ico'))
                                    
                                        app.setWindowIcon(icon)
                                        from PyQt5.QtWidgets import *
                                        data = take_screenshot()
                                        window = QWidget()
                                        layout = QtWidgets.QVBoxLayout(window) 
                                    
                                        pixmap = QPixmap()
                                        pixmap.loadFromData(data)
                                        label = QtWidgets.QLabel()
                                        label.setPixmap(pixmap)
                                        
                                        layout.addWidget(label)         
                                        geometry = app.desktop().availableGeometry()        
                                        window.setFixedSize(geometry.width(), geometry.height())        
                                    
                                        window = OrthoView()
                                        window.show()
                                        sys.exit(app.exec_())
                                    

                                    I used/modified this:

                                    import sys
                                    
                                    from PySide2 import QtCore, QtGui, QtWidgets
                                    
                                    
                                    def take_screenshot():
                                        from PyQt5 import QtCore as pyqt5c
                                        from PyQt5 import QtWidgets as pyqt5w
                                    
                                        screen = pyqt5w.QApplication.primaryScreen()
                                        winid = pyqt5w.QApplication.desktop().winId()
                                        pixmap = screen.grabWindow(winid)
                                    
                                        ba = pyqt5c.QByteArray()
                                        buff = pyqt5c.QBuffer(ba)
                                        pixmap.save(buff, "PNG")
                                        return ba.data()
                                    
                                    
                                    if __name__ == "__main__":
                                        app = QtWidgets.QApplication(sys.argv)
                                    
                                        data = take_screenshot()
                                    
                                        pixmap = QtGui.QPixmap()
                                        pixmap.loadFromData(data)
                                    
                                        label = QtWidgets.QLabel()
                                        label.setPixmap(pixmap)
                                        label.show()
                                    
                                        sys.exit(app.exec_())
                                    

                                    (source: https://stackoverflow.com/questions/59118938/type-error-when-calling-qscreen-grabwindow)

                                    JonBJ 1 Reply Last reply
                                    0
                                    • J john_hobbyist

                                      I don't know! This is the modified last part of my code:

                                      if __name__ == "__main__":
                                          app = QApplication(sys.argv)
                                          icon = QIcon(os.path.join(selfDir, '_static', 'orthoview.ico'))
                                      
                                          app.setWindowIcon(icon)
                                          from PyQt5.QtWidgets import *
                                          data = take_screenshot()
                                          window = QWidget()
                                          layout = QtWidgets.QVBoxLayout(window) 
                                      
                                          pixmap = QPixmap()
                                          pixmap.loadFromData(data)
                                          label = QtWidgets.QLabel()
                                          label.setPixmap(pixmap)
                                          
                                          layout.addWidget(label)         
                                          geometry = app.desktop().availableGeometry()        
                                          window.setFixedSize(geometry.width(), geometry.height())        
                                      
                                          window = OrthoView()
                                          window.show()
                                          sys.exit(app.exec_())
                                      

                                      I used/modified this:

                                      import sys
                                      
                                      from PySide2 import QtCore, QtGui, QtWidgets
                                      
                                      
                                      def take_screenshot():
                                          from PyQt5 import QtCore as pyqt5c
                                          from PyQt5 import QtWidgets as pyqt5w
                                      
                                          screen = pyqt5w.QApplication.primaryScreen()
                                          winid = pyqt5w.QApplication.desktop().winId()
                                          pixmap = screen.grabWindow(winid)
                                      
                                          ba = pyqt5c.QByteArray()
                                          buff = pyqt5c.QBuffer(ba)
                                          pixmap.save(buff, "PNG")
                                          return ba.data()
                                      
                                      
                                      if __name__ == "__main__":
                                          app = QtWidgets.QApplication(sys.argv)
                                      
                                          data = take_screenshot()
                                      
                                          pixmap = QtGui.QPixmap()
                                          pixmap.loadFromData(data)
                                      
                                          label = QtWidgets.QLabel()
                                          label.setPixmap(pixmap)
                                          label.show()
                                      
                                          sys.exit(app.exec_())
                                      

                                      (source: https://stackoverflow.com/questions/59118938/type-error-when-calling-qscreen-grabwindow)

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

                                      @john_hobbyist
                                      Maybe I misunderstand, but you call take_screenshot()as the very first thing you do, before you have created or shown anything at all? What would you expect from that, logically?

                                      EDIT
                                      OK, you're trying to screenshot what's already on the screen? Sorry, I misunderstood. Still seems very early but may be correct. Ignore this post if it is :)

                                      I'm not sure I see anything here to do with custom painting. Do you use a debugger? Do you step through your code to find out where your problems come from? We need you to (learn to) do that, people here don't know where the errors come in your code.

                                      J 1 Reply Last reply
                                      1
                                      • JonBJ JonB

                                        @john_hobbyist
                                        Maybe I misunderstand, but you call take_screenshot()as the very first thing you do, before you have created or shown anything at all? What would you expect from that, logically?

                                        EDIT
                                        OK, you're trying to screenshot what's already on the screen? Sorry, I misunderstood. Still seems very early but may be correct. Ignore this post if it is :)

                                        I'm not sure I see anything here to do with custom painting. Do you use a debugger? Do you step through your code to find out where your problems come from? We need you to (learn to) do that, people here don't know where the errors come in your code.

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

                                        @JonB Yes, I am trying to find the errors!

                                        JonBJ 1 Reply Last reply
                                        0
                                        • J john_hobbyist

                                          @JonB Yes, I am trying to find the errors!

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

                                          @john_hobbyist
                                          So, nicely, do you use some Python debugger to execute your code?

                                          J 1 Reply Last reply
                                          1

                                          • Login

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