I have a subclass of QGraphicsItem and I want to track mouse position every time I hover the mouse on that Item alone and it should give the relative position to the QGraphicsItem. I know, how to handle this when there is QWidget (using QWidget.setMouseTracking) and QMainWindow (by installing event filters), but I cant figure out how to do it for QGraphicsItem. The mouse move event is only triggered, when there is mouse press event, but that's not what is want. I want a trigger whenever I hover on that QGraphicsItem. Please provide your suggestions
You will want to use my_item.setAcceptHoverEvents(True) to enable mouse move events without the mouse press event.
Note, this will not trigger a mouseMoveEvent but will instead trigger a hoverMoveEvent (along with hoverEnterEvent and hoverLeaveEvent when you initially move the mouse over or off the QGraphicsItem respectively. So make sure you override this method in your subclass of QGraphicsItem.
Related
I have a QMainWindow application that generates multiple QWidget popups. I would like to know how I can destroy the pop up QWidgets if the user clicks on the main QMainWindow. I am not sure how to proceed with the resolution of this problem. I do not want to interfere with any QMainWindow events, I just want to destroy the additional pop up windows by any mouse click outside of the pop up widgets area. Is it possible or recommended via signal slot mechanism or eventFilter? Is it possible without dealing with (x,y) coordinates?
Thanks for the suggestions.
I managed to solve my problem. I implemented a custom label (like this) that contains a mousePressedEvent. On clicking this label a Pop widget is generated. The mousePressEvent is not propagated any further from the label. Then I implemented a mousePressedEvent in the QMainWindow where I check for existence of popup windows. If they exist, I close them all. Note that If I do not stop mouse propagation in the label mousePressEvent, on clicking the label the pop window is generated but it is immediately closed by the QMainWindow mousePressEvent because (most likely) the event is propagating to QMainWindow. At least, that solves my problem.
Thanks
I'm using a QDialog with transparency enabled to select a region of the screen for a screen capture tool. When the user clicks inside the transparent widget I want to ignore the mouse event so that the system handles it. Is this possible?
I'm trying to achieve this on Linux.
Some things I have tried with no success:
QtWidgets.QWidget.setWindowFlags(QtCore.Qt.WindowTransparentForInput)
QtWidgets.QWidget.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
QtWidgets.QWidget.setMask(QtGui.QRegion(self.geometry()))
Subclassing mousePressEvent and ignoring the event
You have to use the flag X11BypassWindowManagerHint so that you omit the window manager next to WindowTransparentForInput so that the system knows that it only has to show the window but does not notify you about the inputs.
w.setWindowFlags(w.windowFlags() |
QtCore.Qt.WindowTransparentForInput |
QtCore.Qt.X11BypassWindowManagerHint)
Is it possible to trigger a function when the mouse stop moving?
For example, I have a scale, and I want to trigger a function when I still holding a button and stop dragging. I used 'ButtonRelease-1' but I had to release a mouse button to be able to trigger a function, it's not what I want.
Hope you guys can help me.
Thank you in advance.
No, you can't bind to "mouse stopped moving". However, you can bind to <B1-Motion>, and with each callback set a timer to run in the future. If the mouse hasn't moved within that time the function will be called. Within that function you could use event_generate to generate a custom <<MouseStopped>> event. If the mouse moves before the timer expires, reset the timer.
You get the exactly the same effect as if you bound to the mouse not moving.
A part of a small project I am working on involves 'calibrating' the coordinates of the screen of which to take a screen capture of.
By the 'screen', I refer to the entire desktop, not my GUI window.
The coordinates are calibrated when a QDialog window appears (which I've subclassed).
The user is prompted to click several locations on the screen.
I need the program to record the locations of all mouse clicks occuring anywhere on the screen - ones that don't natively trigger a QDialog mouseEvent, since they are outside this window.
Obviously overwriting the mouseEvent method does not work, since the QDialog doesn't recieve the clicks.
How can I capture global mouse clicks, so that an event is triggered and sent to the QDialog when any part of the screen is clicked?
(I'd prefer a Qt based solution, but am open to other libraries if need be).
Thanks!
There are some cross-platform examples of how to do this with http://pypi.python.org/pypi/autopy/0.51
I've assumed this isn't possible and am instead using pyHook,
letting Qt pump the messages.
I have a wx.toolbar with some buttons. One of the buttons makes pan left!
I want to click on the button and while I keep it pressed, the pan left is made.
For now I only saw that the wx.EVT_TOOL only works when mouse left is up.
Is there a way to do what I intend ?
In the toolbar button's event, you should be able to get the state of the mouse via wx.GetMouseState.
Alternatively, you can make your own toolbar with a panel and some wx.Buttons (or other button widgets).