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 37.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS 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
            • J john_hobbyist

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

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

              @john_hobbyist
              Yes but its hard to know what you currently have.
              So the blue rectangle is a QRubberband ?

              So there place where you create it, what is used as the parent.
              It kinda seems like the Toolbar as else it would not clip to it.

              is self.selection the RubberBand ?

              J 1 Reply Last reply
              1
              • mrjjM mrjj

                @john_hobbyist
                Yes but its hard to know what you currently have.
                So the blue rectangle is a QRubberband ?

                So there place where you create it, what is used as the parent.
                It kinda seems like the Toolbar as else it would not clip to it.

                is self.selection the RubberBand ?

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

                @mrjj Yes, you are right! It is used inside

                MyMplCanvas
                

                Yes it is rubberband,

                self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)
                
                mrjjM 1 Reply Last reply
                0
                • J john_hobbyist

                  @mrjj Yes, you are right! It is used inside

                  MyMplCanvas
                  

                  Yes it is rubberband,

                  self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #82

                  @john_hobbyist
                  Hi
                  Hmm The reason its up in the Toolbar seems not to be be related to parent

                  class MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
                  ....
                  self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)

                  I assume here self is MyMplCanvas and as far as we know MyMplCanvas is the class that draw the image.

                  So the Rubberband works, but cant be dragged outside that little area ?

                  J 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @john_hobbyist
                    Hi
                    Hmm The reason its up in the Toolbar seems not to be be related to parent

                    class MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
                    ....
                    self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)

                    I assume here self is MyMplCanvas and as far as we know MyMplCanvas is the class that draw the image.

                    So the Rubberband works, but cant be dragged outside that little area ?

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

                    @mrjj "So the Rubberband works, but cant be dragged outside that little area ?"
                    Yes!

                    mrjjM 1 Reply Last reply
                    0
                    • J john_hobbyist

                      @mrjj "So the Rubberband works, but cant be dragged outside that little area ?"
                      Yes!

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

                      @john_hobbyist
                      Ok. That is bizarre as that sounds like it is inside the toolbar.
                      and the mouseXXX functions are under the MyMplCanvas ?

                      I don't get this as then it should not work if it's placed in toolbar as the mouse events go into
                      MyMplCanvas.

                      unde this line
                      self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)

                      is it possible to
                      print (self) ?

                      Im not sure if it just shows the address.

                      J 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @john_hobbyist
                        Ok. That is bizarre as that sounds like it is inside the toolbar.
                        and the mouseXXX functions are under the MyMplCanvas ?

                        I don't get this as then it should not work if it's placed in toolbar as the mouse events go into
                        MyMplCanvas.

                        unde this line
                        self.selection = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)

                        is it possible to
                        print (self) ?

                        Im not sure if it just shows the address.

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

                        @mrjj

                        Both

                        def mousePressEvent(self, event):
                        

                        and

                        def mouseMoveEvent(self, event):
                        

                        are under

                        class MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
                        

                        Yes, the answer in:

                        print (self)
                        

                        is:

                        <__main__.MyMplCanvas object at 0x7f717bb5f700>
                        
                        mrjjM 1 Reply Last reply
                        0
                        • J john_hobbyist

                          @mrjj

                          Both

                          def mousePressEvent(self, event):
                          

                          and

                          def mouseMoveEvent(self, event):
                          

                          are under

                          class MyMplCanvas(mpl_qt.FigureCanvasQTAgg):
                          

                          Yes, the answer in:

                          print (self)
                          

                          is:

                          <__main__.MyMplCanvas object at 0x7f717bb5f700>
                          
                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #86

                          @john_hobbyist
                          Hi
                          Oh that actually worked. the print(self)
                          so the parent really is MyMplCanvas.

                          So how does it show the blue rect ?

                          You click on the Canvas and it appears up there ?

                          J 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @john_hobbyist
                            Hi
                            Oh that actually worked. the print(self)
                            so the parent really is MyMplCanvas.

                            So how does it show the blue rect ?

                            You click on the Canvas and it appears up there ?

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

                            @mrjj Yes I click on the large area (where the images are depicted) and I see the result as I depicted in the figure above, BUT it never completes the operation, meaning that as I move it, the rectangle keeps changing on that small area..

                            mrjjM 1 Reply Last reply
                            0
                            • J john_hobbyist

                              @mrjj Yes I click on the large area (where the images are depicted) and I see the result as I depicted in the figure above, BUT it never completes the operation, meaning that as I move it, the rectangle keeps changing on that small area..

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

                              @john_hobbyist
                              Ok. since it seems it should be in the Canvas, I would put some print() under

                              self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())

                              in mouseMove and inspect the values and see
                              like
                              print( QtCore.QRect(self.upper_left, self.lower_right).normalized() )

                              and see what values it sets. Maybe that can give a hint.

                              J 1 Reply Last reply
                              2
                              • J john_hobbyist

                                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_()
                                
                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #89

                                @john_hobbyist said in Merge two pieces of code:

                                class RubberbandEnhancedLabel(QtGui.QLabel):
                                
                                    def __init__(self, parent=None):
                                        QtGui.QLabel.__init__(self, parent)
                                        self.selection = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, sel
                                

                                Your rubber band should be constrained within your RubberbandEnhancedLabel. On your screenshot, where exactly do you claim that RubberbandEnhancedLabel to be? Perhaps you should show the same screenshot but with an image in the label so we can see?

                                And what values does self.upper_left, self.lower_right have for the rubberband shown in the screenshot?

                                J 1 Reply Last reply
                                1
                                • mrjjM mrjj

                                  @john_hobbyist
                                  Ok. since it seems it should be in the Canvas, I would put some print() under

                                  self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())

                                  in mouseMove and inspect the values and see
                                  like
                                  print( QtCore.QRect(self.upper_left, self.lower_right).normalized() )

                                  and see what values it sets. Maybe that can give a hint.

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

                                  @mrjj This the value for the upper left corner inside the canvas that images are depicted:

                                  PyQt5.QtCore.QRect(8, 0, 25, 69)
                                  

                                  And this is the value for the lower right corner:

                                  PyQt5.QtCore.QRect(32, 68, 506, 543)
                                  

                                  (with some error). So the values are changing...

                                  And when I go inside the area of the toolbox in upper left area, I get this:

                                  Widget::paintEngine: Should no longer be called
                                  QPainter::begin: Paint device returned engine == 0, type: 1
                                  QPainter::end: Painter not active, aborted
                                  

                                  which I get it repeatedly as I change tool icon...

                                  1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @john_hobbyist said in Merge two pieces of code:

                                    class RubberbandEnhancedLabel(QtGui.QLabel):
                                    
                                        def __init__(self, parent=None):
                                            QtGui.QLabel.__init__(self, parent)
                                            self.selection = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, sel
                                    

                                    Your rubber band should be constrained within your RubberbandEnhancedLabel. On your screenshot, where exactly do you claim that RubberbandEnhancedLabel to be? Perhaps you should show the same screenshot but with an image in the label so we can see?

                                    And what values does self.upper_left, self.lower_right have for the rubberband shown in the screenshot?

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

                                    @JonB said in Merge two pieces of code:

                                    RubberbandEnhancedLabel

                                    Yes you are right RubberbandEnhancedLabel is not used inside the code...! Here is a new screenshot with an image.

                                    problem2.jpg

                                    mrjjM 1 Reply Last reply
                                    0
                                    • J john_hobbyist

                                      @JonB said in Merge two pieces of code:

                                      RubberbandEnhancedLabel

                                      Yes you are right RubberbandEnhancedLabel is not used inside the code...! Here is a new screenshot with an image.

                                      problem2.jpg

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

                                      @john_hobbyist
                                      Yes it seems outside the canvas, but the self we used said it was named Cnavas so
                                      I dont know how that is possible.

                                      ALso
                                      PyQt5.QtCore.QRect(8, 0, 25, 69)
                                      seems ok but its like it has the wrong parent.

                                      I can not guess what is going on.

                                      J 1 Reply Last reply
                                      2
                                      • mrjjM mrjj

                                        @john_hobbyist
                                        Yes it seems outside the canvas, but the self we used said it was named Cnavas so
                                        I dont know how that is possible.

                                        ALso
                                        PyQt5.QtCore.QRect(8, 0, 25, 69)
                                        seems ok but its like it has the wrong parent.

                                        I can not guess what is going on.

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

                                        @mrjj If I post my code, will that help?

                                        mrjjM 1 Reply Last reply
                                        0
                                        • J john_hobbyist

                                          @mrjj If I post my code, will that help?

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

                                          @john_hobbyist
                                          Hi
                                          It might as then we are sure its the current code that you have right now.
                                          Also others might spot what the issue can be.

                                          I think its might be Local / Global coordinate issues but I cannot understand how
                                          the Rubberband can react to MouseEvents from the Canvas widget since its
                                          clearly outside.

                                          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