Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set QToolBar color without losing the blue QToolButton hover effect



  • Everytime I try to set the background color of my toolbar I lose the blue hover effect of the Actions/ToolButtons.
    I tried with setting a new stylesheet and also with appending to the existing one.

    When using one of the following, I lose the hover effect:

    ui->toolBar->setStyleSheet("background-color: rgba(200, 200, 200, 255);");
    //or
    ui->toolBar->setStyleSheet(ui->toolBar->styleSheet().append("background-color: rgba(200, 200, 200, 255);"));
    

    When adding the hover part, I use the background color of the Toolbar instead.

    ui->toolBar->setStyleSheet("background-color: rgba(200, 200, 200, 255); QToolButton:hover {background-color: blue};");
    //or
    ui->toolBar->setStyleSheet(ui->toolBar->styleSheet().append("background-color: rgba(200, 200, 200, 255); QToolButton:hover {background-color: blue};"));
    

    I tried playing with Palette in Designer but that did not work at all.

    What is the right way of doing this?



  • @Megamouse said in How to set QToolBar color without losing the blue QToolButton hover effect:

    ui->toolBar->setStyleSheet("background-color: rgba(200, 200, 200, 255);");

    Use

    ui->toolBar->setStyleSheet("QToolBar { background-color: rgba(200, 200, 200, 255); }");
    

    maybe that helps.


  • Lifetime Qt Champion

    Hi
    Style sheet are cascading
    so when u set on ToolBar with no Type specifier/name it also affects buttons
    So what @Fuel suggest, should work as its using typename.



  • amazing !

    it works, thank you @Fuel @mrjj

    I did not know I could set a style for only the target object by using its own typename.

    thanks again


  • Lifetime Qt Champion

    @Megamouse
    Super :)
    And to the typename, you can also add instance name (object name)
    QPushButton#evilButton { background-color: red }
    Type QPushButton but only named evilButton
    http://doc.qt.io/qt-5/stylesheet-examples.html

    You can apply stylesheet to QApplication.
    That is the highest parent you can get and it will affect most.


Log in to reply