How to receive QMainWindow mouseClick events in a parentless QWidget? - python

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

Related

Tracking the mouse position on the QGraphicsItem

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.

PyQt4 - detect global mouse click

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.

Qt - Temporarily disable all events or window functionality?

I have a Qt program with many buttons, user-interactable widgets, etc.
At one stage in the program, I would like all the widgets to temporarily 'stop working'; stop behaving to mouse clicks and instead pass the event on to one function.
(This is so the User can select a widget to perform meta operations. Part explanation here: Get variable name of Qt Widget (for use in Stylesheet)? )
The User would pick a widget (to do stuff with) by clicking it, and of course clicking a button must not cause the button's bound function to run.
What is the correct (most abstracted, sensible) method of doing this?
(which doesn't involve too much new code. ie; not subclassing every widget)
Is there anything in Qt designed for this?
So far, I am able to retrieve a list of all the widgets in the program (by calling
QObject.findChildren(QtGui.QWidget)
so the solution can incorporate this.
My current horrible ideas are;
Some how dealing with all the applications events all the time in one
function and not letting through the events when I need the
application to be dormant.
When I need dormancy, make a new transparent widget which recieves
mouse clicks and stretch it over the entire window. Take coordinates
of click and figure out the widget underneath.
Somehow create a new 'shell' instance of the window.
THANKS!
(Sorry for the terrible write-up; in a slight rush)
python 2.7.2
PyQt4
Windows 7
You can intercept events send to specific widgets with QObject::installEventFilter.
graphite answered this one first so give credit where credit is due.
For an actual example in PySide, here's an example you might draw some useful code from:
my_app.py
from KeyPressEater import KeyPressEater
if __name__ == "__main__":
app = QApplication(sys.argv)
eater = KeyPressEater()
app.installEventFilter(eater)
KeyPressEater.py
class KeyPressEater(QObject):
# subclassing for eventFilter
def eventFilter(self, obj, event):
if self.ignore_input:
# swallow events
pass
else:
# bubble events
return QObject.eventFilter(self,obj,event)

Qt Designer: how to add custom slot and code to a button

I use Qt4 Designer and I want that when I click on the "yes" button, some code will execute. And when I click on the "no", some other code will be execute. How can I do it?
Click on the Edit Signal/Slots tool.
Create a connection for your button. For this, select your button in the designer by pressing on it with the left button of the mouse. Move the mouse to some place in the main window to create a connection with the main window (it is like a red line with a earth connection).
When you release the mouse button, the Configure Connection dialog appears.
In this dialog select a signal in the left text control (the sender), for example, pressed().
Then press edit in the right text control (the receiver). A dialog for the Signals/Slots of MainWindow appears.
In the slot panel add a new slot (green cross). The text slot1() appears. Double click on it to edit the line and write instead the name of your function doit_when_yes_ispressed(). Accept.
Now in the Configure Connection dialog you will see your function in the right text control. Select and Accept.
In the designer now you can see the signal and your function in the widget.
Right-click on your widget
Select "Go to slot..."
Select a signal and click OK
Your custom slot declaration and definition for that signal will be added to *.cpp and *.h files. Its name will be generated automatically.
upd:
Sorry, I didn't notice that the question is about Python & QtDesigner itself, I was thinking of the designer mode in QtCreator IDE. However, this still may be useful for someone who is looking for Qt/C++ info, so I leave the answer.

Catching a drag exit in Qt?

I've got a custom widget descended from QWidget that I want to be able to drop onto, and while the drag is hovering over the widget I'd like to highlight it to provide a little visual feedback to the user. Seems to me the simplest way to do this would be to highlight when dragEnterEvent is called and unhighlight when the drag exits the widget, but how can I catch the drag exit? There doesn't seem to be a dragExitEvent event handler.
Open Assistant on the Index tab, type "drag*event" and you will find dragLeaveEvent. :)

Categories

Resources