How to integrate a cursor in a QtWidgets app
-
@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")@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/fRightButtonnobody can know whether your code is correct. Start by putting in an unconditionalprint()to see you are getting here, after that deal with the button code. -
@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/fRightButtonnobody can know whether your code is correct. Start by putting in an unconditionalprint()to see you are getting here, after that deal with the button code.@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
-
@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
@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 aQMainWindow. -
@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 aQMainWindow.@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...
-
@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...
@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.
-
@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.
@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") -
@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")@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. -
@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") -
@jsulm
I believe you are mistaking the issue! :) The OP says this code works as-is. He puts this code in a class derived fromscene.SceneCanvas, which is some OpenGL class, not Qt. I believe the mouse events are being handled inside that, not Qt, so if that wants aon_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!
-
@jsulm
I believe you are mistaking the issue! :) The OP says this code works as-is. He puts this code in a class derived fromscene.SceneCanvas, which is some OpenGL class, not Qt. I believe the mouse events are being handled inside that, not Qt, so if that wants aon_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!
@JonB you understood me right. i have to follow this code because it is going to be integrated in another code where I need it to be OpenGL.
and yes i confirm what you say about the mouse events. seems that inside the opengl canvas we need dedicated mouse events . outside of the windows is Qt where mousePressEvent works. these are two different frameworks.I thank all the contributors for helping me. As I am new to QtWidgets.
now i am trying to move the cursor i created. I set now the question as resolved as it does not involve Qt anymore