what is is right way to call the Qstyle::polish function?
Unsolved
General and Desktop
-
Hello.
I have one
QLabel
, and I am apply style toQLabel
from.css
style sheet. immediately. I calllabel->sizeHint()
, but size hint function return very small size.To solve this issue, I use
QEvent::Polish
event, and polish the widget with help ofqApp->style()->polish(label);
Now,sizeHint()
function is return correct value.My main question about Polish function, If I want to use
Qstyle::polish
then is it any requirement to call any other function before callingQstyle::polish
?what is right way to call
Qstyle::polish
among 3?auto label = OBJ_NAME(new QLabel, "element-builder"); QPixmap pixmap = elem->GetImage(); /*auto imageLblSizeHint = label->sizeHint() * 0.95; if ((pixmap.height() > imageLblSizeHint.height()) || (pixmap.width() > imageLblSizeHint.width())) { pixmap = pixmap.scaled(imageLblSizeHint, Qt::KeepAspectRatio, Qt::SmoothTransformation); } label->setPixmap(pixmap);*/ //label->setPixmap(elem->GetImage()); label->installEventFilter(new UniversalEventFilter(label, [=](QObject *obj, QEvent *e) { auto &_elem(elem); if (e->type() == QEvent::Polish) { label->ensurePolished(); qApp->style()->unpolish(label); qApp->style()->polish(label); auto imageLblSizeHint = label->sizeHint() * 0.95; if ((pixmap.height() > imageLblSizeHint.height()) || (pixmap.width() > imageLblSizeHint.width())) { label->setPixmap(pixmap.scaled(imageLblSizeHint, Qt::KeepAspectRatio, Qt::SmoothTransformation)); } return false; } });
auto label = OBJ_NAME(new QLabel, "element-builder"); QPixmap pixmap = elem->GetImage(); /*auto imageLblSizeHint = label->sizeHint() * 0.95; if ((pixmap.height() > imageLblSizeHint.height()) || (pixmap.width() > imageLblSizeHint.width())) { pixmap = pixmap.scaled(imageLblSizeHint, Qt::KeepAspectRatio, Qt::SmoothTransformation); } label->setPixmap(pixmap);*/ //label->setPixmap(elem->GetImage()); label->installEventFilter(new UniversalEventFilter(label, [=](QObject *obj, QEvent *e) { auto &_elem(elem); if (e->type() == QEvent::Polish) { qApp->style()->unpolish(label); qApp->style()->polish(label); auto imageLblSizeHint = label->sizeHint() * 0.95; if ((pixmap.height() > imageLblSizeHint.height()) || (pixmap.width() > imageLblSizeHint.width())) { label->setPixmap(pixmap.scaled(imageLblSizeHint, Qt::KeepAspectRatio, Qt::SmoothTransformation)); } return false; } });
auto label = OBJ_NAME(new QLabel, "element-builder"); QPixmap pixmap = elem->GetImage(); /*auto imageLblSizeHint = label->sizeHint() * 0.95; if ((pixmap.height() > imageLblSizeHint.height()) || (pixmap.width() > imageLblSizeHint.width())) { pixmap = pixmap.scaled(imageLblSizeHint, Qt::KeepAspectRatio, Qt::SmoothTransformation); } label->setPixmap(pixmap);*/ //label->setPixmap(elem->GetImage()); label->installEventFilter(new UniversalEventFilter(label, [=](QObject *obj, QEvent *e) { auto &_elem(elem); if (e->type() == QEvent::Polish) { qApp->style()->polish(label); auto imageLblSizeHint = label->sizeHint() * 0.95; if ((pixmap.height() > imageLblSizeHint.height()) || (pixmap.width() > imageLblSizeHint.width())) { label->setPixmap(pixmap.scaled(imageLblSizeHint, Qt::KeepAspectRatio, Qt::SmoothTransformation)); } return false; } });
Please correct me if I am in wrong direction. By this code I am changing the size of image with respect to the screen resolution.