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)
        {
            style->unpolish(this);
            style->polish(this);
        }
    }
    

    The mouse release event handler is:

    void MyWidget::mouseReleaseEvent(QMouseEvent * event)
    {
        if (isEnabled())
        {
            setPressed(false);
            if (m_canBeToggled)
            {
                setChecked(!m_checked);
            }
            else
            {
                emit clicked();
            }
            repolish();
        }
    }
    

    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

    Hi,

    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.