1

I created a simple 10x10 black box and added it to a QToolButton as follows:

QIcon minIcon;
minIcon.addFile("c:/tmp/black10x10.png");
minButton = new QToolButton;
minButton->setIcon(minIcon);

However, it appears on screen shifted left (image enlarged for convenience):

a reasonably misaligned icon

Some squinting in Gimp told me that grey area to the left is 56 pixels zoomed and grey area to the right is 68. This misalignment is very noticeable even without zoom - that was how I spotted it in the first place. So, how do I center this icon?

P.S. Tried using a QPushButton without text. Same effect.

sigil
  • 815
  • 8
  • 16
  • It's probably saving space for a label. Have you tried a negative left-side margin? – Nicolas Holthaus Feb 02 '17 at 15:26
  • It looks as if you're trying to centre a 10x10 image in a 25x25(ish) rectangle. That being the case there's always going to be a half pixel jitter either way. – G.M. Feb 02 '17 at 15:28
  • @Nicolas Holthaus `minButton->setContentsMargins()` does not seem to have any effect regardless of what parameters I use. Furthermore, it would be hardcoding I'd have to test on every platform and version. – sigil Feb 02 '17 at 15:38
  • @G.M. Suggest a size I should test, then? Initially I was using `QApplication::style()->standardIcon(QStyle::SP_TitleBarMinButton)` and yes, it was off. – sigil Feb 02 '17 at 15:40
  • @sigil It wouldn't be a scalable solution anyway, I was just curious whether margins had an effect. – Nicolas Holthaus Feb 02 '17 at 15:40
  • How does it appear if you try `QToolBar::setIconSize` with even size? – ilotXXI Feb 02 '17 at 16:33
  • @ ilotXXI I'm not using `QToolBar` here. This button (along with lots of other stuff) goes into a `QGridLayout`. – sigil Feb 02 '17 at 16:45
  • 1
    Nothing I tried had any effect on the icon position within the button. The only remaining suggestion I have is to subclass QToolButton and override its "paint" method and control the icon painting yourself so that you can tweak it to get it centered properly. I've noticed this effect as well, but I haven't tried to do anything about it. If you write your own paint, I think you would not use the setIcon method, but rather call QToolButton::paint, and then paint the icon yourself at the desired location. If you use setIcon, the base method will paint it, which you don't want. – goug Feb 03 '17 at 01:19
  • @goug Indeed, I came to the same conclusion: it seems to be just another Qt feature I have to rewrite. – sigil Feb 03 '17 at 12:44
  • Please try `setToolButtonStyle(Qt::ToolButtonIconOnly)` to check the label is not influencing the centering of the icon. – cbuchart Feb 17 '17 at 15:06
  • @cbuchart No difference. – sigil Feb 19 '17 at 15:35

1 Answers1

0

It's probably a bit late now, but I stumbled across the same issue and found the following code snippet in QTs qstylesheet.cpp

case CT_ToolButton:
        if (rule.hasBox() || !rule.hasNativeBorder() || !rule.baseStyleCanDraw())
            sz += QSize(3, 3); // ### broken QToolButton

This would increase your even sized icon to be odd sized and therefor not centered. I'm not sure why there's an addition of 3 but the comment suggest it's a fix for something... Unfortunately this doesn't fix the issue, it just kind of explains the source of it. But it might help someone to find a better solution than "make all your icons odd sized".