How can I customize the appearance of the actions in my QToolBar?

南笙酒味 提交于 2020-01-16 05:40:44

问题


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()?


回答1:


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.



回答2:


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.



来源:https://stackoverflow.com/questions/12283789/how-can-i-customize-the-appearance-of-the-actions-in-my-qtoolbar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!