Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

EventFilter detecting object



  • 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.


  • Lifetime Qt Champion


Log in to reply