PDA

View Full Version : Adding mouse event tracking to a QLabel contained in a main window



dmginc
17th January 2010, 08:41
I have created a main window with Qt Creator containing a QLabel named tracking_frame ...

How would I add mouse tracking to this QLabel only? - for example, outputting the coordinates (with respect to the QLabel) of a mouse click within the label.

I realise that I need to overwrite the protected member function: void mousePressEvent(QMouseEvent *event) inside tracking_frame and then create a function like so:


void ??? ::mousePressEvent(QMouseEvent *event)
{
fprintf(stderr,"(x,y)=(%d,%d)\n",event->pos().x(),event->pos().y());
} // mousePressEvent()

I am just unsure of where to place the


protected:
void mousePressEvent(QMouseEvent *event);

(as the QLabel class definition is inside "ui_MainWindow.h" as it was all created in Qt Creator):

If I place it in the MainWin class definition in MainWindow.h the mouse tracking works but of course for anywhere in the main window...

What do I have to do?

I hope someone can help me out with this!
Hopefully it is a fairly straightforward procedure :)

Cheers!

faldzip
17th January 2010, 12:53
If you want to catch mouse events from some widget you have to override mousePressEvent() in this widget:


class MyLabel : public QLabel
{
// ...
void mousePressEvent(QMouseEvent *event);
};

void MyLabel::mousePressEvent(QMouseEvent *event)
{
// here you get event which happend on this label
}

but if you don't want to subclass label you can use [qtclass]QObject::eventFilter()[qtclass] to filter events from your label. You can find examples and good explanation in QtAssistant.

dmginc
17th January 2010, 13:10
I see! :)

Would I put this new class declaration for my label within the MainWin class declaration in "MainWindow.h" to subclass it?

That's the part I'm unsure of...

Thanks again!

aamer4yu
18th January 2010, 04:48
You can either add the tracking functionality to label, and use those label in your main window,

Second option is - use event filters in main window, and check if the event is for the intended widget
say if(mouseEvent->widget == tracking_frame ) and output the cordinates in the main window itself.

Using filter in mainwindow will be better because in case you need to track mouse events for some other widgets which are not labels, you wont have to subclass them.

dmginc
18th January 2010, 06:14
Thanks for the reply! :)

I think I will go with the sub-classing option because with event filtering, if I click on the QLabel, the coordinates will be relative to the entire main window, right? (not to the size of the QLabel)

My real question is how do I subclass when the QLabel declaration is inside the header created by Qt Designer?

In the UI file created with Designer, the QLabel object I want to modify is a public member of Ui_MainWindow:


class Ui_MainWindow
{
public:
//
QLabel *tracking_frame;
//
//
}

So my question is, how do I overwrite the mousePressEvent protected member?
As in, what code do I insert into MainWindow.h? (or do I have to create a new header and source file to do this?)

I tried just adding this above the MainWin class declaration in MainWindow.h:

class tracking_frame : public QLabel, private Ui::MainWindow::tracking_frame
{
protected:
void mousePressEvent(QMouseEvent *event);
};

But this doesn't work...

Thanks so much!

nikhilqt
18th January 2010, 06:52
or you can use installEventFilter(...)

using which you can catch Qlabel mouse events in your mainwindow class

something like this,


label.installEventFilter(mainwindow);

dmginc
18th January 2010, 13:24
Thanks to all the replies thus far!

I've successfully got the mouse tracking working in only the QLabel widget in the main window - I created a subclass of QLabel and just overwrote the protected member function mousePressEvent() :D

The only weird thing is:

A small portion (rectangular-shaped) OUTSIDE the QLabel at the top left corner ALSO responds to mouse events whereas EVERYWHERE else outside the QLabel (correctly) does not... This area is clearly not a part of the QLabel...

I can't work out why either - anyone have any ideas?

Much appreciated!

ready
25th March 2011, 14:20
dmginc
please post your detail code as soon as possible ...
I am also having same problem as your

FelixB
25th March 2011, 14:26
please post your detail code as soon as possible ...
I am also having same problem as your

whats wrong with the solutions you were given in the thread you created by yourself?

edit: ok, I see the new posts now... strange. I looked into the thread before positing here.

siddyQT
1st April 2011, 13:17
i had the same probelm...i went for subclassing....thanks

Aayush
16th July 2011, 12:29
Hello All!!

I am building a camera app using QT and openCV..... I need to create a mouse event on a QLabel inside a main window.... Sub Classing is not feasible for me.. so I went with using installEventFilter() ..... I checked and the mouse event is working fine but the image in the label is not visible.... can you tell me how to fix this problem??

Thanks!!