How does the program get the current mouse position when the QPushButton button is pressed?
-
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? -
@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-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.
-
@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。。。
-
@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?
-
@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。