1

I have just changed some toolbars from Q3ToolBars (with QToolButtons explicitly added to them) into Q4 toolbars (with actions added to them straight away instead.)

The old tool buttons had a nice outline around them, but this is not displayed in the new version; the QActions in the Q4 toolbar just look like a line of icons. Is there a way to change the 'button' style in the new version (assuming these actions can be considered as such) and give them the outline? I've looked through the QToolBar reference, but the toolButtonStyle() function only appears to work with whether you want to display icon, text, etc.

...Or will I have to just make actual tool buttons and/or QPushButtons and use addWidget()?

norman
  • 5,128
  • 13
  • 44
  • 75

2 Answers2

1

The widget associated with a given action is accessible through QToolBar::widgetForAction (since Qt 4.2). So, you can pass your actions to this method, get the QWidgets returned by it, convert them to QToolBar, and handle them like you normally would (code not tested):

// ...

auto toolButton =
    static_cast<QToolButton *>(
        m_ui.toolbar->widgetForAction(m_ui.my_Action));

// Will make the toolButton always appear raised:
toolButton->setAutoRaise(false);

// ...

As far as I've been testing, some methods might not work (i.e., QWidget::hide), so do your own testing.

Romário
  • 1,664
  • 1
  • 20
  • 30
  • This is is the best answer to "How can I customize the appearance of the actions in my QToolBar?" It requires the least effort and it appears to be officially supplied approach. – Michael Hamilton Oct 23 '21 at 22:06
0

Yes, of course you can edit look of QToolButtons in two different ways:

  1. You can set it style sheet using void QWidget::setStyleSheet(const QString &)
  2. You can reimplement QToolButtons class with new paintEvent function where you will be able to exactly set how your button should looks like.
Blood
  • 4,126
  • 3
  • 27
  • 37
  • I looked through the style sheet documentation and didn't see any way to specifically do outlines, unless I'm just not looking hard enough. As for paintEvent...well, I'll look into that if I have to. Wish there was an easy fix, though :/ – norman Sep 05 '12 at 23:02