QWidget::style crashes with "Read Access Violation"

  • I have created a custom control (derived from QLabel), and overriden the mouseReleaseEvent where I am emitting certain signals and I need to repolish the widget so that some of the dynamic properties based QSS style gets reflected.

    I have a repolish method which is declared as:

    void MyWidget::repolish()
        auto style = this->style();
        if (style != nullptr)

    The mouse release event handler is:

    void MyWidget::mouseReleaseEvent(QMouseEvent * event)
        if (isEnabled())
            if (m_canBeToggled)
                emit clicked();

    I get a "Read Access Violation" right at the statement auto style = this->style();. Why is this the case?

    The attached slot is:
    QObject::connect(mywidget, &MyWidget::clicked, [...]{ ... });

  • Moderators

    @ajaxcrypto Do you mean you get this error at runtime?

  • Yes at runtime, I figured out the runtime error part. The parent widget was being deleted on the clicked signal emission. Because repolishing happens after the signal, so a repolish on an invalid object was being called.

    P.S. - The forum is very prompt!

  • Lifetime Qt Champion


    Why was the parent deleted ?

  • It was because of how the slot was written, which calls some functions which ultimately leads to the deletion. I am now repolishing before emitting the signal because that behavior is required.

  • Qt Champions 2016

    You should use QObject::deleteLater in this case which will take care of the deletion in a safe manner.

Log in to reply

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