Qt Style Sheet - Different styles for same type widgets

I need to assign different styles for the same typed widget instances. Specially for QActions. Following style sheet sets QActions' background images, actualy tool buttons'.

QToolButton
{
    background-image: url(bg.png);
}

But I need to assign different backgrounds for tool buttons like this.

QToolButton::actClose
{
    background-image: url(close.png);
}
QToolButton::actOpen
{
    background-image: url(open.png);
}

Is there any easy way like this or is it not possible?

Upvotes: 2

Views: 2094

Answers (2)

Martian Puss
Martian Puss

Reputation: 720

It helps if you can post c++ code that creates the QToolButton and associates with QActions.

Cite from QToolBar's reference "Toolbar buttons are added by adding actions, using addAction() or insertAction(). " and "QWidget * QToolBar widgetForAction (QAction *action )const Returns the widget associated with the specified action."

So if you are creating QToolBar and call QToolBar::addAction(QAction*) to fill it, it is possible to get pointer to the tool buttons. Try QToolBar::widgetForAction(), and call setObjectName("") and Blueman's method can be applied.

while applying Style Sheet to widgets, "#" is used after class name to specify object name, ":" is used after className of objectName indicating the object's status such like "enabled", "::" is used to specify the subcontrols such as "ListView::Item", unfortunately QAction is neither of QToolBar.

Upvotes: 1

Blueman
Blueman

Reputation: 811

You can set object name for instances of QToolButton

QToolButton *button = new QToolButton;
button->setObjectName("firstButton");

button = new QToolButton;
button->setObjectName("secondButton");

and next use them in Style Sheet

QToolButton#firstButton 
{ 
   background-color: gray 
}

QToolButton#secondButton 
{ 
   background-color: red 
}

Upvotes: 1

Related Questions