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

Widget of QScrollArea event filter issue



  • See the code from qt source:

    void QScrollArea::setWidget(QWidget *widget)
    {
        Q_D(QScrollArea);
        if (widget == d->widget || !widget)
            return;
    
        delete d->widget;
        d->widget = 0;
        d->hbar->setValue(0);
        d->vbar->setValue(0);
        if (widget->parentWidget() != d->viewport)
            widget->setParent(d->viewport);
        if (!widget->testAttribute(Qt::WA_Resized))
            widget->resize(widget->sizeHint());
        d->widget = widget;
        d->widget->setAutoFillBackground(true);
        widget->installEventFilter(this);
        d->widgetSize = QSize();
        d->updateScrollBars();
        d->widget->show();
    
    }
    

    When someone uses QScrollArea as container, and maybe change the widget of QScrollArea to other; then, the code widget->installEventFilter(this);, who to break it? there maybe a big issue.

    I do it as follow:

    widget->removeEventFilter(widget->parent());
    widget->removeEventFilter(widget->parent() ? widget->parent()->parent() : nullptr);
    widget->setParent(nullptr);
    

    Maybe better way for you!



  • You are worried the eventfilter stays after the contained widget has been changed? But the line
    delete d->widget deletes the previously contained widget. And deleting the widget is enough to stop the filter. So I don't think there is an issue with the way QScrollArea::setWidget is coded.


  • Qt Champions 2019

    And what's you question/ problem here?



  • You are worried the eventfilter stays after the contained widget has been changed? But the line
    delete d->widget deletes the previously contained widget. And deleting the widget is enough to stop the filter. So I don't think there is an issue with the way QScrollArea::setWidget is coded.



  • @Andeol
    Thank you! It`s a important code and I ignored it.

    The container is not my coding, but it can`t delete the component, and then recreate it. Maybe needs a interface to take widget, so, I See:

    QWidget *QScrollArea::takeWidget()
    {
        Q_D(QScrollArea);
        QWidget *w = d->widget;
        d->widget = 0;
        if (w)
            w->setParent(0);
        return w;
    }
    

Log in to reply