QWidget::style crashes with "Read Access Violation"
-
I have created a custom control (derived from
QLabel
), and overriden themouseReleaseEvent
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, [...]{ ... });
-
@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!
-
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.
-
You should use
QObject::deleteLater
in this case which will take care of the deletion in a safe manner.