[Qt-interest] finding on what widget i am clicking...

Tom Panning lurchvt at gmail.com
Fri Jun 5 14:11:02 CEST 2009


You want to install an event filter on your application and output the
widget associated with any MouseButtonPress events. This involves
tying several pieces of Qt together, but it's not difficult (you just
need to know which pieces).

First, read up on event filters:
http://doc.qtsoftware.com/4.5/qobject.html#installEventFilter

As described in option #3 of the documentation on
QApplication::notify()
(http://doc.qtsoftware.com/4.5/qcoreapplication.html#notify), you want
to install an event filter on your qApp (note that you're not going to
do anything with QApplication::notify(), that's just where this
technique is documented). Remember to return QObject::eventFilter() on
all of the events, otherwise they won't be processed (see the event
filter documentation again if you don't understand).

In the event filter, you want to check for any QEvents of type
MouseButtonPress. The QObject* that is passed into the event filter is
the object (probably a QWidget) that the click was being sent to. If
you do:
qDebug() << objectPtr;
then Qt will automagically output the type of the pointer (i.e.,
whether its a QLabel or QPushButton). You need to #include <QtDebug>
in order to use qDebug() as a stream like this.

Hope that helps,
Tom

On Fri, Jun 5, 2009 at 6:33 AM, Francesco Lamonica
<alienpenguin at gmail.com> wrote:
> Hi all, i've got a weird problem...
> I am inspecting some code with a window full of widgets (not created
> with designer) all placed manually with move(), setGeometry() etc.
> i have a label (placed at 10,0) on which i click and the mouse press
> event is recognized only if y()>30.
> If i move the label to (80,0) the click is recognized over all the
> label so i thought there might be some other widget over it and tried
> raise() as the last statement of the ctor but with no luck.
>
> What might it be? if it is indeed a widget (created or moved somewhere
> else in the code flow) how can i find which one it is?
> thanks a lot.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>



More information about the Qt-interest-old mailing list