如何在Qt中从QToolBar自定义QToolButtons



我有一个QToolBar,上面有各种工具按钮。我想用一些简单的效果来定制这些按钮,比如,应该可以看到按钮被按下了,按下后应该更改其图标颜色或背景颜色等。
我试过了,但没有成功。

_toolbar = new QToolBar;
_toolbar->setIconSize(QSize(35,35));
_toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);        

void createIcons()
{
_zoomInIcon = QIcon::fromTheme("zoom-in");
_zoomIn = new QAction(_zoomInIcon, "Zoom in", this);      
// code for other icons    

_toolbar->addAction(_zoomIn);
}


void myClass::ZoomIn()
{
_zoomIn->setCheckable(true);
//QToolButton:_zoomInIcon {background-color: red; }
//setStyleSheet('background-color: red;');
// other logic  
}

此外,我使用的是Qt的默认图标,而不是默认图标
但有些图标看起来不太好,特别是save insave in as
那么,除了Qt中的上述链接之外,还有人知道更多默认图标吗?

有人能帮我吗?

试试下面的(未测试(

//Get the tool button using the action
QToolButton* zoomInButton = mytoolbar->widgetForAction(_zoomIn);
//Set the style you want.
zoomInButton->setStyleSheet("QToolButton:pressed"
"{"
"background-color : red;"
"}"
);

如果工具按钮没有菜单,则可以使用所有QPushButton样式。

https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qtoolbutton

QToolButton没有菜单。在这种情况下,QToolButton的样式与QPushButton完全相同。