Can QObject use type to filter ?



  • QObject->type() == QWidget::QPushButton

    bool TitleBar::eventFilter(QObject *obj, QEvent *event)
    {
        if ( obj->type() == QWidget::QPushButton && event->type() == QEvent::MouseMove ) {
            return true;    // filter
        } else {
            return false;
        }
        // pass the event on to the parent class
        return QWidget::eventFilter(obj, event);
    }
    


  • Hi,
    You can look at the class name::

    obj->metaObject()->className();
    

    return the class Name "QPushButton"

    or using a dynamic cast

    QPushButton* button=qobject_cast<QPushButton*>(obj)
    

    button == nullptr if obj is not a QPushButton

    Be aware that subclass like QCommandLinkButton, in that case, are QPushButton too.



  • @mpergand Thank you !

    if ( QString(obj->metaObject()->className()) == "QPushButton" && event->type() == QEvent::MouseMove ) {
        ...
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.