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 35.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 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
              • JonBJ JonB

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

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

                @JonB I write the code on a simple text editor and run it on Ubuntu's command line..

                JonBJ 1 Reply Last reply
                0
                • J john_hobbyist

                  @JonB I write the code on a simple text editor and run it on Ubuntu's command line..

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

                  @john_hobbyist
                  Yeah, that's not a great starting point :) That means you have to put print() statements everywhere if you want to know what's going on.

                  Unfortunately so far as I know Qt Creator still offers nothing fur running/debugging Python. (Has this changed?) I used PyCharm as editor and debugger for my Python coding, it was great, but not integrated with Qt Creator if you are designing UI files there. Else, I don't know but running under pdb might be better than nothing at all.

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

                    Yes, I work with "prints" inside code.... So here how things are: The program goes to the below methods (source on my previous posts) , but it does not displays the rectangle so it does not call screenshot...Any ideas?

                    Sorry! Updated the correct code!

                        def mousePressEvent(self, event):
                            print("56")
                            '''
                                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:
                                print("57")
                    
                                position = QtCore.QPoint(event.pos())
                                print("58")
                                if self.selection.isVisible():
                                    print("59")
                                    # visible selection
                                    if (self.upper_left - position).manhattanLength() < 20:
                                        print("60")
                                        # close to upper left corner, drag it
                                        self.mode = "drag_upper_left"
                                        print("61")
                                    elif (self.lower_right - position).manhattanLength() < 20:
                                        print("62")
                                        # close to lower right corner, drag it
                                        self.mode = "drag_lower_right"
                                        print("63")
                                    else:
                                        print("64")
                                        # clicked somewhere else, hide selection
                                        self.selection.hide()
                                        print("65")
                                else:
                                    # no visible selection, start new selection
                                    print("66")
                                    self.upper_left = position
                                    print("67")
                                    self.lower_right = position
                                    print("68")
                                    self.mode = "drag_lower_right"
                                    print("69")
                                    self.selection.show()
                                    print("70")
                    
                        def mouseMoveEvent(self, event):
                            print("71")
                            '''
                                Mouse moved. If selection is visible, drag it according to drag mode.
                            '''
                            if self.selection.isVisible():
                                print("72")
                                # visible selection
                                if self.mode == "drag_lower_right":
                                    print("73")
                                    self.lower_right = QtCore.QPoint(event.pos())
                                    print("74")
                                elif self.mode == "drag_upper_left":
                                    print("75")
                                    self.upper_left = QtCore.QPoint(event.pos())
                                    print("76")
                                # update geometry
                                self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())
                                print("77")
                    
                    def take_screenshot():
                        print("205")
                        from PyQt5 import QtCore as pyqt5c
                        print("206")
                        from PyQt5 import QtWidgets as pyqt5w
                        print("207")
                        screen = pyqt5w.QApplication.primaryScreen()
                        print("208")
                        winid = pyqt5w.QApplication.desktop().winId()
                        print("209")
                        pixmap = screen.grabWindow(winid)
                        print("210")
                    
                        ba = pyqt5c.QByteArray()
                        print("211")
                        buff = pyqt5c.QBuffer(ba)
                        print("212")
                        pixmap.save(buff, "PNG")
                        print("213")
                        return ba.data()
                    
                    if __name__ == "__main__":
                        print("214")
                        app = QApplication(sys.argv)
                        print("215")
                        icon = QIcon(os.path.join(selfDir, '_static', 'orthoview.ico'))
                        print("216")
                        app.setWindowIcon(icon)
                        print("217")
                        from PyQt5.QtWidgets import *
                        print("218")
                        data = take_screenshot()
                        print("219")
                        window = QWidget()
                        print("220")
                        layout = QtWidgets.QVBoxLayout(window)
                        print("221")
                        pixmap = QPixmap()
                        print("222")
                        pixmap.loadFromData(data)
                        print("223")
                        label = QtWidgets.QLabel()
                        print("224")
                        label.setPixmap(pixmap)
                        print("225")
                        
                        layout.addWidget(label)     
                        print("226")
                        geometry = app.desktop().availableGeometry()        
                        print("227")
                        window.setFixedSize(geometry.width(), geometry.height())       
                        print("228")
                        window = OrthoView()
                        print("229")
                        window.show()
                        print("230")
                        sys.exit(app.exec_())
                    
                    JonBJ 1 Reply Last reply
                    0
                    • J john_hobbyist

                      Yes, I work with "prints" inside code.... So here how things are: The program goes to the below methods (source on my previous posts) , but it does not displays the rectangle so it does not call screenshot...Any ideas?

                      Sorry! Updated the correct code!

                          def mousePressEvent(self, event):
                              print("56")
                              '''
                                  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:
                                  print("57")
                      
                                  position = QtCore.QPoint(event.pos())
                                  print("58")
                                  if self.selection.isVisible():
                                      print("59")
                                      # visible selection
                                      if (self.upper_left - position).manhattanLength() < 20:
                                          print("60")
                                          # close to upper left corner, drag it
                                          self.mode = "drag_upper_left"
                                          print("61")
                                      elif (self.lower_right - position).manhattanLength() < 20:
                                          print("62")
                                          # close to lower right corner, drag it
                                          self.mode = "drag_lower_right"
                                          print("63")
                                      else:
                                          print("64")
                                          # clicked somewhere else, hide selection
                                          self.selection.hide()
                                          print("65")
                                  else:
                                      # no visible selection, start new selection
                                      print("66")
                                      self.upper_left = position
                                      print("67")
                                      self.lower_right = position
                                      print("68")
                                      self.mode = "drag_lower_right"
                                      print("69")
                                      self.selection.show()
                                      print("70")
                      
                          def mouseMoveEvent(self, event):
                              print("71")
                              '''
                                  Mouse moved. If selection is visible, drag it according to drag mode.
                              '''
                              if self.selection.isVisible():
                                  print("72")
                                  # visible selection
                                  if self.mode == "drag_lower_right":
                                      print("73")
                                      self.lower_right = QtCore.QPoint(event.pos())
                                      print("74")
                                  elif self.mode == "drag_upper_left":
                                      print("75")
                                      self.upper_left = QtCore.QPoint(event.pos())
                                      print("76")
                                  # update geometry
                                  self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())
                                  print("77")
                      
                      def take_screenshot():
                          print("205")
                          from PyQt5 import QtCore as pyqt5c
                          print("206")
                          from PyQt5 import QtWidgets as pyqt5w
                          print("207")
                          screen = pyqt5w.QApplication.primaryScreen()
                          print("208")
                          winid = pyqt5w.QApplication.desktop().winId()
                          print("209")
                          pixmap = screen.grabWindow(winid)
                          print("210")
                      
                          ba = pyqt5c.QByteArray()
                          print("211")
                          buff = pyqt5c.QBuffer(ba)
                          print("212")
                          pixmap.save(buff, "PNG")
                          print("213")
                          return ba.data()
                      
                      if __name__ == "__main__":
                          print("214")
                          app = QApplication(sys.argv)
                          print("215")
                          icon = QIcon(os.path.join(selfDir, '_static', 'orthoview.ico'))
                          print("216")
                          app.setWindowIcon(icon)
                          print("217")
                          from PyQt5.QtWidgets import *
                          print("218")
                          data = take_screenshot()
                          print("219")
                          window = QWidget()
                          print("220")
                          layout = QtWidgets.QVBoxLayout(window)
                          print("221")
                          pixmap = QPixmap()
                          print("222")
                          pixmap.loadFromData(data)
                          print("223")
                          label = QtWidgets.QLabel()
                          print("224")
                          label.setPixmap(pixmap)
                          print("225")
                          
                          layout.addWidget(label)     
                          print("226")
                          geometry = app.desktop().availableGeometry()        
                          print("227")
                          window.setFixedSize(geometry.width(), geometry.height())       
                          print("228")
                          window = OrthoView()
                          print("229")
                          window.show()
                          print("230")
                          sys.exit(app.exec_())
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #69

                      @john_hobbyist
                      Seems to be much the same code as you started with, re-pasted. I don't understand how you said you had moved from Qt4 to Qt5 with QtGui changes, yet now you have QtGui.QLabel, which as far as I know should not work....

                      Anyway, I don't know what path your code is or is not following. It's time for you to put your own print() statements in if you want to understand what is going on....

                      J 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @john_hobbyist
                        Seems to be much the same code as you started with, re-pasted. I don't understand how you said you had moved from Qt4 to Qt5 with QtGui changes, yet now you have QtGui.QLabel, which as far as I know should not work....

                        Anyway, I don't know what path your code is or is not following. It's time for you to put your own print() statements in if you want to understand what is going on....

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

                        @JonB Yes, I have put "prints" inside the code, that is how I figure out on which parts of code the execution goes...

                        JonBJ 1 Reply Last reply
                        0
                        • J john_hobbyist

                          @JonB Yes, I have put "prints" inside the code, that is how I figure out on which parts of code the execution goes...

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

                          @john_hobbyist
                          But you don't tell me, so you want me to guess?

                          Does it hit the self.selection.show()? Does it hit the move code? What is your usage of Python is instead of == in if self.mode is "drag_lower_right"?

                          I leave it to you now.

                          J 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @john_hobbyist
                            But you don't tell me, so you want me to guess?

                            Does it hit the self.selection.show()? Does it hit the move code? What is your usage of Python is instead of == in if self.mode is "drag_lower_right"?

                            I leave it to you now.

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

                            @JonB Sorry! I updated the right code! Please see above 2-3 posts..
                            When I press the mouse button it goes: 56, 57,58, 59, 62, 63, 56, 57,58, 59, 62, 63....
                            When I move the mouse, it goes 71, 72, 73, 74, 77, 71, 72, 73, 74, 77...
                            Either I have loaded an image or not!

                            JonBJ SGaistS 2 Replies Last reply
                            0
                            • J john_hobbyist

                              @JonB Sorry! I updated the right code! Please see above 2-3 posts..
                              When I press the mouse button it goes: 56, 57,58, 59, 62, 63, 56, 57,58, 59, 62, 63....
                              When I move the mouse, it goes 71, 72, 73, 74, 77, 71, 72, 73, 74, 77...
                              Either I have loaded an image or not!

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

                              @john_hobbyist
                              I don't know.

                               self.lower_right = QtCore.QPoint(event.pos())
                              self.upper_left = QtCore.QPoint(event.pos())
                              self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())
                              

                              Are the coordinates of event.pos() correct for setGeometry()? Start by getting some visible rubber band, e.g. put suitable constants in there instead of relying on mouse positions?

                              1 Reply Last reply
                              1
                              • J john_hobbyist

                                @JonB Sorry! I updated the right code! Please see above 2-3 posts..
                                When I press the mouse button it goes: 56, 57,58, 59, 62, 63, 56, 57,58, 59, 62, 63....
                                When I move the mouse, it goes 71, 72, 73, 74, 77, 71, 72, 73, 74, 77...
                                Either I have loaded an image or not!

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

                                @john_hobbyist did you try to just get the rubber band stuff working on a minimal widget ?

                                You can then apply that knowledge to your more complex application.

                                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

                                  @john_hobbyist did you try to just get the rubber band stuff working on a minimal widget ?

                                  You can then apply that knowledge to your more complex application.

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

                                  @SGaist I am searching how do this.... and basically what @JonB suggested...

                                  Update: I noticed that the rectangle works but it is constrained inside tool bar...(upper left corner) not inside the area that I load the image...

                                  problem.jpg

                                  mrjjM 1 Reply Last reply
                                  0
                                  • J john_hobbyist

                                    @SGaist I am searching how do this.... and basically what @JonB suggested...

                                    Update: I noticed that the rectangle works but it is constrained inside tool bar...(upper left corner) not inside the area that I load the image...

                                    problem.jpg

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

                                    @john_hobbyist
                                    Hi
                                    Progress :) \o/

                                    Check for RubberbandEnhancedLabel (if you use it still ) what parent you assign to it
                                    when you create it.

                                    J 1 Reply Last reply
                                    2
                                    • mrjjM mrjj

                                      @john_hobbyist
                                      Hi
                                      Progress :) \o/

                                      Check for RubberbandEnhancedLabel (if you use it still ) what parent you assign to it
                                      when you create it.

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

                                      @mrjj You mean this:

                                      label = RubberbandEnhancedLabel()
                                      

                                      but I do not use it. I have it as comment from previous code. Basically the whole unused/commented code is this:

                                          app = QtWidgets.QApplication([])
                                      
                                          screen_pixmap = QScreen.grabWindow (self, win_id, left, top, width, height)
                                          
                                          window = QtGui.QWidget()
                                          
                                          layout = QtWidgets.QVBoxLayout(window)
                                          label = RubberbandEnhancedLabel()
                                          label.setPixmap(screen_pixmap)
                                          layout.addWidget(label)
                                          geometry = app.desktop().availableGeometry()
                                          window.setFixedSize(geometry.width(), geometry.height())
                                      

                                      which I do not use (copied from one of the sources above...). Should I use something from here?

                                      mrjjM 1 Reply Last reply
                                      0
                                      • J john_hobbyist

                                        @mrjj You mean this:

                                        label = RubberbandEnhancedLabel()
                                        

                                        but I do not use it. I have it as comment from previous code. Basically the whole unused/commented code is this:

                                            app = QtWidgets.QApplication([])
                                        
                                            screen_pixmap = QScreen.grabWindow (self, win_id, left, top, width, height)
                                            
                                            window = QtGui.QWidget()
                                            
                                            layout = QtWidgets.QVBoxLayout(window)
                                            label = RubberbandEnhancedLabel()
                                            label.setPixmap(screen_pixmap)
                                            layout.addWidget(label)
                                            geometry = app.desktop().availableGeometry()
                                            window.setFixedSize(geometry.width(), geometry.height())
                                        

                                        which I do not use (copied from one of the sources above...). Should I use something from here?

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

                                        @john_hobbyist
                                        Hi
                                        So what IS the blue rectangle then ?

                                        Check the parent where you create it.

                                        J 1 Reply Last reply
                                        1
                                        • mrjjM mrjj

                                          @john_hobbyist
                                          Hi
                                          So what IS the blue rectangle then ?

                                          Check the parent where you create it.

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

                                          @mrjj Please look in the previous posts. I have posted everything...

                                          mrjjM 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