我有 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 的默认图标 default-icons
但有些图标看起来不太好,特别是另存为
和另存为
。
那么除了上面的 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
The QToolButton has no menu. In this case, the QToolButton is styled exactly like QPushButton.
关于qt - 如何在 Qt 中从 QToolBar 自定义 QToolButtons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71554558/