Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The MouseEvent of Qlabel doesn't work, after an advanced click on the label.
QtWS25 Last Chance

The MouseEvent of Qlabel doesn't work, after an advanced click on the label.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 800 Views
  • 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.
  • Z Offline
    Z Offline
    ZhongQL
    wrote on last edited by ZhongQL
    #1

    I' m using PyQt5 and QtDesigner on Ubuntu platform to develop an application and confronted with a problem on Qlabel.MouseEvent. To illustrate the problem, I make a simple UI example ( Fig. 1) in QtDesigner and then convert the .ui to python file for further development.
    DeepinScreenshot_select-area_20211230235148.png
    Fig.1 Making an simple example in QtDesigner.

    The .ui file is here and the modified code is below, where ui_test is generated by pyuic5 command:

    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow
    from PyQt5.QtCore import QEvent
    
    from ui_test import Ui_MainWindow
    
    
    class MyMainWindow(QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MyMainWindow, self).__init__()
    
            self.setupUi(self)
    
            # Connect slots:
            self.pushButton.clicked.connect(self.btn_callback)
    
        def pressed_callback(self, event):
            # Just print the pixel location of cursor in the textLabel
            self.textLabel.setText("(px: " + str(event.pos().x()) + "  ,  " +
                                   "py: " + str(event.pos().y()) + ")")
            # print((event.pos().x(), event.pos().y()))
    
        def btn_callback(self):
            # Push to enable print the pixel loction of mouse cursor on the mainLabel
            self.mainLabel.mousePressEvent = self.pressed_callback
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MyMainWindow()
        win.show()
        sys.exit(app.exec_())
    
    

    I can click the button to enable print the pixel loction of mouse cursor when I press the mainLabel arrea (Fig.2). The app works well if I press the button at first. But, the self.mainLabel.mousePressEvent won't work at all if I do a click on the mainLabel before I click the button. Then nothing will happen if I press the mainLabel. How can I fix it?

    DeepinScreenshot_select-area_20211231002809.png

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

      Hi and welcome to devnet,

      @ZhongQL said in The MouseEvent of Qlabel doesn't work, after an advanced click on the label.:

      def btn_callback(self):
      # Push to enable print the pixel loction of mouse cursor on the mainLabel
      self.mainLabel.mousePressEvent = self.pressed_callback

      Why are you replacing mousePressEvent with pressed_calldback ? You are nuking the original functionality by doing that.

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

      Z 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        @ZhongQL said in The MouseEvent of Qlabel doesn't work, after an advanced click on the label.:

        def btn_callback(self):
        # Push to enable print the pixel loction of mouse cursor on the mainLabel
        self.mainLabel.mousePressEvent = self.pressed_callback

        Why are you replacing mousePressEvent with pressed_calldback ? You are nuking the original functionality by doing that.

        Z Offline
        Z Offline
        ZhongQL
        wrote on last edited by
        #3

        @SGaist I want to change the reaction of MouseEvent by pressing a button, what' s the right way to do that?

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          @ZhongQL said in The MouseEvent of Qlabel doesn't work, after an advanced click on the label.:

          def btn_callback(self):
          # Push to enable print the pixel loction of mouse cursor on the mainLabel
          self.mainLabel.mousePressEvent = self.pressed_callback

          Why are you replacing mousePressEvent with pressed_calldback ? You are nuking the original functionality by doing that.

          Z Offline
          Z Offline
          ZhongQL
          wrote on last edited by
          #4

          @SGaist By the way, how should I define the MouseEvent function of a label generated by pyuic?

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

            What exactly do you want to happen when clicking on that QLabel ?

            The proper way is to create a subclass of QLabel and re-implement mousePressEvent however you can do things a bit differently since you are using Python.

            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

            • Login

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