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. How to integrate a cursor in a QtWidgets app
Forum Updated to NodeBB v4.3 + New Features

How to integrate a cursor in a QtWidgets app

Scheduled Pinned Locked Moved Solved Qt for Python
23 Posts 4 Posters 3.1k Views 1 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.
  • P Offline
    P Offline
    Pianissimo89
    wrote on last edited by
    #1

    Hello

    I am new to QtWidgets and trying to build an app in QtWidgets and Python (3.x).
    the end goal of the app is to show images and a superposed cursor (to be exact, a "plus" sign of 2cm) that can be moved along the image reacting to mouse events. I want to start creating this plus sign first then integrate the images.

    i am not sure how to start. i read examples on the internet on how to do it in matplotlib. is it possible in my case to integrate matplotlib?

    thanks

    here is my desired output and the code of my app

    crosshairexample.png

    import sys
    from PySide2 import QtWidgets
    from vispy import scene
    from PySide2.QtCore import QMetaObject
    from PySide2.QtWidgets import *
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            if not MainWindow.objectName():
                MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
    
            self.centralwidget = QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.gridLayout = QGridLayout(self.centralwidget)
            self.gridLayout.setObjectName("gridLayout")
    
            self.groupBox = QGroupBox(self.centralwidget)
            self.groupBox.setObjectName("groupBox")
    
            self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
    
            MainWindow.setCentralWidget(self.centralwidget)
    
            QMetaObject.connectSlotsByName(MainWindow)
    
    
    class MainWindow(QtWidgets.QMainWindow):
    
        def __init__(self):
            super(MainWindow, self).__init__()
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
    
            # OpenGL drawing surface
            self.canvas = scene.SceneCanvas(keys='interactive')
            self.canvas.create_native()
            self.canvas.native.setParent(self)
    
            self.view = self.canvas.central_widget.add_view()
            self.setWindowTitle('MyApp')
    
    
    def main():
        import ctypes
        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('my_gui')
    
        app = QtWidgets.QApplication([])
    
        main_window = MainWindow()
        main_window.show()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    
    1 Reply Last reply
    0
    • jsulmJ jsulm

      @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

      on_mouse_press

      mousePressEvent

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

      @jsulm
      I believe you are mistaking the issue! :) The OP says this code works as-is. He puts this code in a class derived from scene.SceneCanvas, which is some OpenGL class, not Qt. I believe the mouse events are being handled inside that, not Qt, so if that wants a on_mouse_press() method so be it.

      Correct me if I am wrong. Of course if that code does not actually work as the OP implies then it's a different matter!

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

        Hi and welcome to devnet,

        Do you want to move a cursor on your canevas or change the actual mouse cursor when hovering the canevas ?

        As for matplotlib, it has a Qt backend so yes you can integrate it.

        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
        0
        • P Offline
          P Offline
          Pianissimo89
          wrote on last edited by Pianissimo89
          #3

          Hello. @SGaist yes I want to move the cursor (the red plus sign on the image) and this would move according to the mouse events (we need to click on it and drag it. when we don't click it should not move) and this would be suporposed on images i will display in the future

          i thought of doing something like this (simpler than matplotilb). but i have trouble understanding how to integrate it in the code itself (SimpleItem would be something that I draw)

          scene2 = QGraphicsScene()
          item = SimpleItem()
          scene2.addItem(item)
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            In that case, don't complicate things: items can be made movable. See the GraphicsItemFlag.

            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
            • P Offline
              P Offline
              Pianissimo89
              wrote on last edited by
              #5

              @SGaist ok I read about it. but my problem is adding an item like this (example of a square) to my canvas

              class SimpleItem(QtWidgets.QGraphicsItem):
                  def __init__(self):
                      QtWidgets.QGraphicsItem.__init__(self)
                      self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
              
                  def boundingRect(self):
                      penWidth = 1.0
                      return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                                           20 + penWidth, 20 + penWidth)
              
                  def paint(self, painter, option, widget):
                      rect = self.boundingRect()
                      painter.drawRect(rect)
              

              how can i include this to my canvas ?

              scene2 = QGraphicsScene()
              item = SimpleItem()
              scene2.addItem(item)
              
              JonBJ 1 Reply Last reply
              0
              • P Pianissimo89

                @SGaist ok I read about it. but my problem is adding an item like this (example of a square) to my canvas

                class SimpleItem(QtWidgets.QGraphicsItem):
                    def __init__(self):
                        QtWidgets.QGraphicsItem.__init__(self)
                        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
                
                    def boundingRect(self):
                        penWidth = 1.0
                        return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                                             20 + penWidth, 20 + penWidth)
                
                    def paint(self, painter, option, widget):
                        rect = self.boundingRect()
                        painter.drawRect(rect)
                

                how can i include this to my canvas ?

                scene2 = QGraphicsScene()
                item = SimpleItem()
                scene2.addItem(item)
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

                how can i include this to my canvas ?

                More or less exactly as you show, using void QGraphicsScene::addItem(QGraphicsItem *item). (You will presumably want to set where you want the item and what size it is, maybe a pen too. If this is just supposed to be a rectangle, with perhaps your own boundingrect(), why not start by deriving from QGraphicsRectItem?) What is the issue?

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Pianissimo89
                  wrote on last edited by Pianissimo89
                  #7

                  @JonB my issue is i don t understand how to integrate it in my code. I am missing something on how to link it to the canvas

                      ```
                      # OpenGL drawing surface
                      self.canvas = scene.SceneCanvas(keys='interactive')
                      self.canvas.create_native()
                      self.canvas.native.setParent(self)
                  
                  
                      self.view = self.canvas.central_widget.add_view()
                  
                      self.view.bgcolor = '#ffffff'   # set the canva to a white background
                  
                      scene2 = QGraphicsScene()
                      item = SimpleItem()
                      scene2.addItem(item)
                  
                  JonBJ 1 Reply Last reply
                  0
                  • P Pianissimo89

                    @JonB my issue is i don t understand how to integrate it in my code. I am missing something on how to link it to the canvas

                        ```
                        # OpenGL drawing surface
                        self.canvas = scene.SceneCanvas(keys='interactive')
                        self.canvas.create_native()
                        self.canvas.native.setParent(self)
                    
                    
                        self.view = self.canvas.central_widget.add_view()
                    
                        self.view.bgcolor = '#ffffff'   # set the canva to a white background
                    
                        scene2 = QGraphicsScene()
                        item = SimpleItem()
                        scene2.addItem(item)
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @Pianissimo89
                    This seems to be something to do with OpenGL, about which I know nothing. So you will have to await someone else's response.

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

                      @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

                      vispy

                      I didn't realize that you were using an external library.

                      I haven't used it yet but it seems that their Shape Draw example could be a good starting point for what you want.

                      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
                      0
                      • P Offline
                        P Offline
                        Pianissimo89
                        wrote on last edited by
                        #10

                        @SGaist thank you for the tip. it seems easy to create any line plot for example

                        data = 500* np.random.normal(size=(2, 2))
                        plot = scene.Line(data, parent=self.view.scene, color="r")
                        

                        I just have to figure out now how to attach mouse events on it.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          Pianissimo89
                          wrote on last edited by Pianissimo89
                          #11

                          by adding these two methodes i don t get any prints. why is that?

                          def on_mouse_press(self, event):
                             print(f"Press: {event.button}= | {event.buttons}=")
                          
                          def on_mouse_release(self, event):
                             print(f"Release: {event.button}= | {event.buttons}=")
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • P Pianissimo89

                            by adding these two methodes i don t get any prints. why is that?

                            def on_mouse_press(self, event):
                               print(f"Press: {event.button}= | {event.buttons}=")
                            
                            def on_mouse_release(self, event):
                               print(f"Release: {event.button}= | {event.buttons}=")
                            
                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

                            why is that?

                            Because it's wrong.
                            Please check correct event handler names here: https://doc.qt.io/qt-6/qwidget.html

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            P 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

                              why is that?

                              Because it's wrong.
                              Please check correct event handler names here: https://doc.qt.io/qt-6/qwidget.html

                              P Offline
                              P Offline
                              Pianissimo89
                              wrote on last edited by
                              #13

                              @jsulm thank you for your help. however i searched bit i am trying this snippet of code. it does not work

                              def mousePressEvent(self, event):
                                    if event.button() == self.fLeftButton:
                                        print("pressed left")
                                    if event.button() == self.fRightButton:
                                        print("pressed right")
                              
                              JonBJ 1 Reply Last reply
                              0
                              • P Pianissimo89

                                @jsulm thank you for your help. however i searched bit i am trying this snippet of code. it does not work

                                def mousePressEvent(self, event):
                                      if event.button() == self.fLeftButton:
                                          print("pressed left")
                                      if event.button() == self.fRightButton:
                                          print("pressed right")
                                
                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #14

                                @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

                                it does not work

                                Does not print what you expect? Does not get called at all? What class is this method defined inside? Since we do not know what might be in your self.fLeftButton/fRightButton nobody can know whether your code is correct. Start by putting in an unconditional print() to see you are getting here, after that deal with the button code.

                                P 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

                                  it does not work

                                  Does not print what you expect? Does not get called at all? What class is this method defined inside? Since we do not know what might be in your self.fLeftButton/fRightButton nobody can know whether your code is correct. Start by putting in an unconditional print() to see you are getting here, after that deal with the button code.

                                  P Offline
                                  P Offline
                                  Pianissimo89
                                  wrote on last edited by
                                  #15

                                  @JonB i put it in class MainWindow after the init (whole code is in the first message) unconditional print independent of buttons also does not print. so it does not detected

                                  JonBJ 1 Reply Last reply
                                  0
                                  • P Pianissimo89

                                    @JonB i put it in class MainWindow after the init (whole code is in the first message) unconditional print independent of buttons also does not print. so it does not detected

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

                                    @Pianissimo89
                                    And where are you pressing the mouse over? If I am not mistaken this will only work when directly over the main window, not a sub-widget. You might check it works in a standalone program with just a QMainWindow.

                                    P 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @Pianissimo89
                                      And where are you pressing the mouse over? If I am not mistaken this will only work when directly over the main window, not a sub-widget. You might check it works in a standalone program with just a QMainWindow.

                                      P Offline
                                      P Offline
                                      Pianissimo89
                                      wrote on last edited by
                                      #17

                                      @JonB I see your point. i clicked outside the window we see when running the program (and making the window bigger so we can click outside of the square... he we get the print...)

                                      but this is obviously not what I need. i have trouble understanding where i should put this event so i can get it even when i am on the canvas...

                                      JonBJ 1 Reply Last reply
                                      0
                                      • P Pianissimo89

                                        @JonB I see your point. i clicked outside the window we see when running the program (and making the window bigger so we can click outside of the square... he we get the print...)

                                        but this is obviously not what I need. i have trouble understanding where i should put this event so i can get it even when i am on the canvas...

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

                                        @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

                                        when i am on the canvas...

                                        You would need that when you click on the canvas then. But as I wrote at the start I know nothing about how that OpenGL canvas or whatever it is works with Qt. I am not sure whether you would get, or why you would even want, main window mouse click events if you are clicking on some other widget/canvas/outside the main window --- I would expect to handle mouse events there.

                                        P 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @Pianissimo89 said in How to integrate a cursor in a QtWidgets app:

                                          when i am on the canvas...

                                          You would need that when you click on the canvas then. But as I wrote at the start I know nothing about how that OpenGL canvas or whatever it is works with Qt. I am not sure whether you would get, or why you would even want, main window mouse click events if you are clicking on some other widget/canvas/outside the main window --- I would expect to handle mouse events there.

                                          P Offline
                                          P Offline
                                          Pianissimo89
                                          wrote on last edited by
                                          #19

                                          @JonB thanks again for the tips. i think i solved the problem by creating a dedicated class

                                          class my_canvas(scene.SceneCanvas):
                                              def __init__(self):
                                                  super().__init__(keys="interactive")
                                          
                                              def on_mouse_press(self, event):
                                                  print("hello")
                                          
                                          JonBJ jsulmJ 2 Replies Last reply
                                          0
                                          • P Pianissimo89

                                            @JonB thanks again for the tips. i think i solved the problem by creating a dedicated class

                                            class my_canvas(scene.SceneCanvas):
                                                def __init__(self):
                                                    super().__init__(keys="interactive")
                                            
                                                def on_mouse_press(self, event):
                                                    print("hello")
                                            
                                            JonBJ Offline
                                            JonBJ Offline
                                            JonB
                                            wrote on last edited by
                                            #20

                                            @Pianissimo89
                                            So it looks like the mouse press you want to capture has nothing to do with Qt and its windows, it's just within the canvas. Which makes sense.

                                            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