Good day :)

There are QToolBar and QAction for which the icon is installed, but the text is not installed. The size of the icon used is known to me (16 or 32) depending on the screen resolution. The created QAction added to the QToolBar using the addAction method. A button is created with an icon. I need to make buttons of the same size. How does QT determine the size of the resulting button?

At the moment I think this is iconSize + indent from the icon. How to get the indent value? I will be glad to any considerations on this topic :)

Update

It turns out that to create a button of the same size as on the toolbar, should I add 2 ButtonMargin sizes (up and down) to the content size?

 int buttonMargin = QApplication::style()->pixelMetric( QStyle::PM_ButtonMargin ); int buttonSize = ButtonMargin * 2 + contentSize; 

I understood correctly?

Judging by the fact that I checked QApplication::style()->pixelMetric(QStyle::PM_ButtonMargin); returns 6 on all platforms: (Although Padding on Windows is only 1 pixel, respectively Margin should be 3

    1 answer 1

    For each QAction, a QToolButton is created (on each of the bars in which it is included). You can get it, and then fix anything you want ..

     QToolBar bar; dynamic_cast<QToolButton*>( bar.widgetForAction(action) )-> ... 

    Indents between the picture and the borders of the button are determined by the algorithm described here: http://doc.crossplatform.ru/qt/4.5.0/stylesheet-customizing.html#box-model

    If you need to change something for all the buttons on the panel, the easiest way to do this is using qss:

     bar.setStyleSheet( " QToolBar { spacing: 0px; } " " QToolButton { border: 0px; } " ); 

    You can ask for the size of the item with the current modifications from qss, like this:

     QToolButton tmpToolButton; // Этот элемент не показывается. Но если вместо него задать null, ответ будет неправильный (без учета qss). QSize result=QApplication::style()->sizeFromContents( QStyle::CT_ToolButton, 0, bar.iconSize(), &tmpToolButton ); 
    • Thank you very much for the tip. Updated the question. - FalseSky