Code related to eventFilter looks ok, what are you doing with label after this ? Maybe you just display wrong label in ui. Another thing is that I'm not really sure how your code looks like, is this all one header ? If yes then I'd suggest separate the implementation to .cpp files.
Just tested this code:
// app.h
#include <QObject>
{
Q_OBJECT
protected:
};
// app.h
#include <QObject>
class Mouse : public QObject
{
Q_OBJECT
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
To copy to clipboard, switch view to plain text mode
// app.cpp
#include "app.h"
#include <QtGui>
{
if (event
->type
() == QEvent::MouseButtonPress) { qDebug() << "mouse press";
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event
);
}
}
int main(int argc, char *argv[])
{
w->setLayout(l);
lbl->installEventFilter(new Mouse());
l->addWidget(lbl);
w->show();
return a.exec();
}
// app.cpp
#include "app.h"
#include <QtGui>
bool Mouse::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) {
qDebug() << "mouse press";
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget * w = new QWidget();
QVBoxLayout * l = new QVBoxLayout(w);
w->setLayout(l);
QLabel * lbl = new QLabel(w);
lbl->installEventFilter(new Mouse());
l->addWidget(lbl);
w->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
When clicking on the label I can see "mouse press" text in console window.
Bookmarks