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

what is is right way to call the Qstyle::polish function?



  • Hello.

    I have one QLabel, and I am apply style to QLabel from .css style sheet. immediately. I call label->sizeHint(), but size hint function return very small size.

    To solve this issue, I use QEvent::Polish event, and polish the widget with help of qApp->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 calling Qstyle::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.


Log in to reply