I'm writing my own class, very similar to a toolbar, which will contain widgets under a horizontal box layout object inside of it.
I have a widget class serving as a container I have a widget class deriving from QPushButton, for custom painting (really showing a loaded image at pos 0,0).
I initialize a layout object:
this->buttonLayout = new QHBoxLayout(this);
this->buttonLayout->setContentsMargins(0, 0, 0, 0);
this->buttonLayout->setSpacing(0);
this->buttonLayout->setObjectName("buttonsLayout");
and then i start initializing widgets and "adding" them onto the layout similar to this:
// once button is created.. add it...
this->buttonLayout->addWidget(button);
In the end, I create a spacer object to make sure all my toolbar buttons are aligned to the left.
QSpacerItem * spacerItem = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);
this->buttonLayout->addItem(spacerItem);
My button class is defining a "setMinimumSize" in the constructor which is correct under a debugger. It's also redefining "hintSize" which is called during runtime and the values are correct.
My problem is this. I load 3 "buttons" onto the toolbar. All have variable sizes. if the Spacing on the layout is 0 the first two buttons will be truncated by 9 pixels exactly!, but the third one will be okay. if the layout spacing is -1 the buttons will show nicely with their variable size, but will have a two pixel space between them (I don't want any space). Lastly, if the layout spacing is "9", Everything will show up fine.
Why is spacing "0" messing up the button layout causing an overlay? Where did the number "9" come from? why are the first two buttons (e.g. everything but the last button) truncated by exactly 9 pixels when spacing is "0"?
I couldn't find an answer anywhere.
ADDITIONAL INFO:
I've narrowed it down to this:
For every button class, I load up a QImage. The hintSize returns its size, it's correct under debugger. The returned size matches what photoshop says about the image, and what the properties of the image say about it...
however, on the toolbar, after the layout, it is always given its width "- 9" !!! so the following button to the right is always painted over it, hiding 9 x height pixels of the button to the left.
if i manually add a "+9" to the sizeHint in my button class, it works perfectly.
I can't understand where the problem comes from. somewhere between the layout and the horizontal spacer, pixels are "stolen" and i don't know what the heck's going on here.
has anyone encountered something like that? why do I have to "pre-steal" 9 pixels from each button so that the layout is perfect?