如何在 Qt 中从 QToolBar 自定义 QToolButtons?

发布于 2025-01-15 14:47:38 字数 918 浏览 3 评论 0原文

我有 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 链接之外,还有人知道更多默认图标吗?

谁能帮助我吗?

I am having QToolBar with various tool buttons on it. I want to customize those buttons with some simple effects like, it should be seen that button is pressed, after pressing it should change its icon color or background color etc.
I tried but I could not succeed.

_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  
}
  

Moreover I am using Qt's default icons from this default-icons
But some of the icons are not looking good specially save in and save in as.
So does any one knows more default icons apart from above link in Qt ?

Can anyone help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2025-01-22 14:47:38

尝试如下所示(未测试)

//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 完全相同。

Try something like below (not tested)

//Get the tool button using the action
QToolButton* zoomInButton = mytoolbar->widgetForAction(_zoomIn);

//Set the style you want.
zoomInButton->setStyleSheet("QToolButton:pressed"
                             "{"
                             "background-color : red;"
                             "}"
                             );

And you can use all QPushButton styles, if your tool button don't have a menu.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文