Qt World Summit: Submit your Presentation

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
    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;
    	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