Important: Please read the Qt Code of Conduct -

QToolBar - real Icon Size / Minimum Size

  • Greetings!

    I'm trying to figure out the minimum toolbar size for all icons to be visible. Since I couldn't find any in-built functionality for this, I tried calculating the minimum size from the size of the icons.

    I've realized, that QToolBar draws an additional border around the icons, which breaks the minimum-size calculation using iconSize().

    My questions:

    • Is there any way to disable the additional QToolBar border around icons?
    • If not, is there any reliable way to query the size of icons including this border?
    • Is there an easy way to get the minimum size of a QToolBar, where all icons are visible?

  • Moderators

    The thing is that toolbars don't really display icons. They display widgets (that can have icons). By default when you add an action to a toolbar a QToolButton is created for it, but you are not limited to that and can add any widget e.g. an expanding line edit, combobox or a button with an icon and text. All of these can have different size policies, be expanding or have a custom stylesheet applied.
    All of this makes calculating such size not feasible because how would you calculate it if a widget can change its size.

    What I'm saying is that yours is a special, very specific case (with just icons), and as such you need to handle it yourself if you want to.

    To answer your questions:

    • It's not one thing that adds the space. There are couple of aspects that can contribute. You can control some(or all?) of them with stylesheets e.g. set padding of the toolbar and toolbuttons to 0 and margins and borders of the toolbuttons. By default all of these depend on a style and will vary across computers. You also need to be careful to consider the size of the toolbar handle (if it's movable) as its size depends on the active style. You also need to consider that if the icon is narrower than the iconSize then there's gonna by space left anyway.
    • nope, AFAIK it can vary from one item of the toolbar to another if you set it this way
    • nope and just out of curiosity - why do you need that? The bar will display an arrow button that will let you see the overflowing items. Also such calculated size would be useless as the window can be resized and thus the toolbar too (unless you're doing some really fancy layout).