EventFilter detecting object
Unsolved
C++ Gurus
-
I am using the eventFilter to detect the object clicked.
I have two kind of differents object
QLabel and QPushButton.
I am using this piece of code:
bool minesweeper::eventFilter(QObject watched, QEvent event) {
if (event->type() == QEvent::MouseButtonPress)
{
const QMouseEvent const me = static_cast<const QMouseEvent>(event);
const QPoint p = me->pos(); //...or ->globalPos();
QWidget * const widget = childAt(p);const QLabel * const label = qobject_cast<QLabel *>(widget); const QPushButton * const label2 = qobject_cast<QPushButton *>(widget); if(label == null){ cout<< "it is Pushbutton" <<endl; }else{ cout<< "it is Pushbutton" <<endl; }
}
Are there any way more elegance to do it?
Is dangerous the condition label == null? Because you are doing n static cast and could be not always is null.
-
@xcalzada You could also use https://doc.qt.io/qt-5/qobject.html#inherits