hansolo
hansolo

Reputation: 160

How can I set background for a QPushButton's tooltip?

In Qt I set a background image for a QPushButton, then I added a tooltip for this button and the tooltip has the same background image as the button and I couldn't change it with stylesheet, what am I missing?

In my code I have:

button->setStyleSheet("background-image: url(pathToImage);");
button->setToolTip("Click here");

In my style.qss I have:

QToolTip{
    background-image: none;
    background-color: white;
    font-size: 20px;
    border: 1px solid black;
}

The font-size and the border works, but the tooltip's background-image is the same as the button's. I also tried adding another background-image to the tooltip, it didn't worked either.

How can I change the tooltip's background?

Upvotes: 0

Views: 3968

Answers (2)

mohabouje
mohabouje

Reputation: 4050

You have to specify the QWidget where to apply the property. If you dont do so, it will apply it to all the childrens of the widget.

In your case, to avoid the background image in the tooltip you have to specify that you want to apply that style to a QPushButton widget. The documentation says:

If we want the property to apply only to the QLineEdits that are children (or grandchildren or grand-grandchildren) of a specific dialog, we would rather do this:

myDialog->setStyleSheet("QLineEdit { background-color: yellow }");

In the example you mention, if you want to modify the style of the tooltip and the button, do something like this:

ui->pushButton->setStyleSheet(""
    "QPushButton { background-image: url(me.png); }"
    "QToolTip { color: #ffffff; background-color: #000000; border: 0px; }");

It will give you something like this

enter image description here

Update:

If you want to apply it to a single object and not the rest of the widgets of the same type, the documentation says:

If we want the property to apply only to one specific QLineEdit, we can give it a name using QObject::setObjectName() and use an ID Selector to refer to it:

myDialog->setStyleSheet("QLineEdit#nameEdit { background-color: yellow }");

So in your case:

ui->pushButton->setObjectName("awesomeButton");
ui->pushButton->setStyleSheet("QPushButton#awesomeButton { background-image: url(me.png); }");

Upvotes: 4

Serhiy Kulish
Serhiy Kulish

Reputation: 1122

When you set qss with setStyleSheet your stylesheet applies for all children of object. In your case you can avoid this using stylesheet for QPushButton only

button->setStyleSheet("QPushButton {background-image: url(pathToImage);}");

Upvotes: 0

Related Questions