Home > Software engineering >  How to customize QToolButtons from QToolBar in Qt?
How to customize QToolButtons from QToolBar in Qt?

Time:03-23

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 ?

CodePudding user response:

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.

  • Related