PDA

View Full Version : using installEventFilter() on a QLable hides the image in the label.



Aayush
16th July 2011, 12:32
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!!

stampede
16th July 2011, 15:44
Whats wrong with subclassing ?
It will be easier to help if you post eventFilter code.

Aayush
16th July 2011, 18:26
The problem with subclassing is that -- I have put the QLabel via the UI creator and then I have made a QLabel pointer in my code to point it to the ui element (for manual access). Now making a subclass of QLabel, I couldnt quite figure out how to point the derived class's pointer to the QLabel object in the UI. ( I tried using cast operators.. didnt work).

My eventFilter() code has basically nothing... I'v just put a couple of qDebug messages with conditions. I'm quite sure there's nothing wrong with the eventFilter(). The only problem is .. by doing

imageLabel->installEventFilter(this); (imageLabel is the name of the QLabel object)

the image that was supposed to be in that Label is not visible.

stampede
16th July 2011, 18:45
I'm quite sure there's nothing wrong with the eventFilter()
Thats why I asked for implementation, if installing the event filter prevents the image from being displayed, then there obviously is something wrong with it ;) What is the return value of your event filter ?

I couldnt quite figure out how to point the derived class's pointer to the QLabel object in the UI
Place QLabel on the form, right click on it and select "Promote to" option, then fill the class name and header file of your custom Label class.

Aayush
16th July 2011, 18:57
my eventFilter functuion is::

bool MyAppName::eventFilter(QObject *obj, QEvent *event)
{
if (obj == imageLabel) {
if (event->type() == QEvent::MouseButtonPress) {
qDebug("Mouse has been pressed\n");

}
return true;
} else {
// pass the event on to the parent class
return false;
//return QMainWindow::eventFilter(obj, event);
}
}

*The qDebug messange is being displayed when I click on the label.


But thanks for the second tip about the sub class.. I'll try that and see what happens.

Really appreciate your help. Thanks*A Million! :)

stampede
16th July 2011, 19:40
Look, after proper code formatting the problem is obvious:


bool MyAppName::eventFilter(QObject *obj, QEvent *event)
{
if (obj == imageLabel) {
if (event->type() == QEvent::MouseButtonPress) {
qDebug("Mouse has been pressed\n");
// return true here, because ...
}
return true; // ... this will filter out ALL events for label ! including paint events;)
} else {
// pass the event on to the parent class
return false;
//return QMainWindow::eventFilter(obj, event);
}
}

Aayush
17th July 2011, 02:06
Gee... Thanks!! It worked perfectly!! I feel embarassed now!! Such a stupid mistake.... :D