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. How does the program get the current mouse position when the QPushButton button is pressed?
Qt 6.11 is out! See what's new in the release blog

How does the program get the current mouse position when the QPushButton button is pressed?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.3k 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.
  • Poor EnglishP Offline
    Poor EnglishP Offline
    Poor English
    wrote on last edited by
    #1

    I have a requirement like this:In my Qt application,there is a QPushButton component,when the user presses this button,the program needs to know the current mouse position;I tried a method that is to rewrite the mousePressEvent function,but this method failed:only when the user clicks on an unrelated area,my custom mousePressEvent function will do something,but when the user clicks on the real QPushButton buttons,this function will not do anything(maybe because in QPushButton,the incident was swallowed up)
    So come here to ask friends,is there any good way?

    I am sorry about my poor English!

    jsulmJ 1 Reply Last reply
    0
    • Poor EnglishP Poor English

      @jsulm said in How does the program get the current mouse position when the QPushButton button is pressed?:

      How exactly? You need to do it with your own button derived from QPushButton and use this button.

      No need to derive,I just use the QPushButton button,when the user presses this QPushButton,the program needs to know the position of the mouse。。。

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Poor-English said in How does the program get the current mouse position when the QPushButton button is pressed?:

      No need to derive

      Then how should it work with mousePressEvent? mousePressEvent is called for the widget which was clicked, if you did not subclass QPushButton and override mousePressEvent then how is it supposed to work?

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

      Poor EnglishP 1 Reply Last reply
      2
      • Poor EnglishP Poor English

        I have a requirement like this:In my Qt application,there is a QPushButton component,when the user presses this button,the program needs to know the current mouse position;I tried a method that is to rewrite the mousePressEvent function,but this method failed:only when the user clicks on an unrelated area,my custom mousePressEvent function will do something,but when the user clicks on the real QPushButton buttons,this function will not do anything(maybe because in QPushButton,the incident was swallowed up)
        So come here to ask friends,is there any good way?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Poor-English said in How does the program get the current mouse position when the QPushButton button is pressed?:

        I tried a method that is to rewrite the mousePressEvent function

        How exactly? You need to do it with your own button derived from QPushButton and use this button.

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

        Poor EnglishP 1 Reply Last reply
        1
        • jsulmJ jsulm

          @Poor-English said in How does the program get the current mouse position when the QPushButton button is pressed?:

          I tried a method that is to rewrite the mousePressEvent function

          How exactly? You need to do it with your own button derived from QPushButton and use this button.

          Poor EnglishP Offline
          Poor EnglishP Offline
          Poor English
          wrote on last edited by
          #3

          @jsulm said in How does the program get the current mouse position when the QPushButton button is pressed?:

          How exactly? You need to do it with your own button derived from QPushButton and use this button.

          No need to derive,I just use the QPushButton button,when the user presses this QPushButton,the program needs to know the position of the mouse。。。

          I am sorry about my poor English!

          jsulmJ 1 Reply Last reply
          0
          • Poor EnglishP Poor English

            @jsulm said in How does the program get the current mouse position when the QPushButton button is pressed?:

            How exactly? You need to do it with your own button derived from QPushButton and use this button.

            No need to derive,I just use the QPushButton button,when the user presses this QPushButton,the program needs to know the position of the mouse。。。

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Poor-English said in How does the program get the current mouse position when the QPushButton button is pressed?:

            No need to derive

            Then how should it work with mousePressEvent? mousePressEvent is called for the widget which was clicked, if you did not subclass QPushButton and override mousePressEvent then how is it supposed to work?

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

            Poor EnglishP 1 Reply Last reply
            2
            • jsulmJ jsulm

              @Poor-English said in How does the program get the current mouse position when the QPushButton button is pressed?:

              No need to derive

              Then how should it work with mousePressEvent? mousePressEvent is called for the widget which was clicked, if you did not subclass QPushButton and override mousePressEvent then how is it supposed to work?

              Poor EnglishP Offline
              Poor EnglishP Offline
              Poor English
              wrote on last edited by
              #5

              @jsulm
              Thank you,sir。it is like this,I am writing a small SUDOKU program,the application has many buttons,when the user presses a certain button,a small window will pop up,I want to be more humane and make this small window appear,the position changes according to the users current cursor position。

              Although the method you provided seems to be very effective,through my hard exploration,I found a seemingly faster method is to write 【this->cursor().pos()】code directly in the program,and the program can know the current cursor position。

              I am sorry about my poor English!

              1 Reply Last reply
              2

              • Login

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