Question

This is something i´ve been struggling with since i started using stylesheets on QT.

I have this style:

QPushButton#btnNewCamera
{
    border-radius: 5px;
    min-width: 20px;
    min-height: 20px;
    background-position: center;
    background-repeat: no-repeat;
}

This is the code I had to set to make it work.

createNewCameraBtn = new QPushButton();
createNewCameraBtn->setObjectName( tr("btnNewCamera") );      
createNewCameraBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
createNewCameraBtn->setFixedWidth(20);
  

Does it make sense?. Telling the size twice?. This buttons are always the same size. If i set the fixed size less than 20, it works anyway (because it keeps the minimum size from the stylesheet... so that makes me guess that stylesheet sizes are prioritary...)

But why not just tell the size inside the stylesheet. Why add those other lines?

Could somebody explain me what happens with this?. Why min-width on the styletheet to make it work?. Why not just give it the info about the background and make it work like the old style buttons?

Was it helpful?

Solution

stylesheets will change your widget properties, so changing same thing in code and in stylesheet is not necessary. If you want to make widget fixed size with CSS you need to set both min and max size. Otherwise widget may be resized. Try to keep all styling stuff in one place to keep everything clear, as sometimes properties will mix up with those in code and in CSS.

Remember that properties will be changed in order they are applied. So if you apply CSS on application first, then change properties in code, property will keep value from code. If you change property in code and then apply CSS, CSS value will be kept

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top