如何设置QPushButton工具提示的背景?

问题描述 投票:0回答:2

在Qt中我为QPushButton设置了一个背景图像,然后我为这个按钮添加了一个工具提示,工具提示与按钮的背景图像相同,我无法用样式表更改它,我缺少什么?

在我的代码中,我有:

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

在我的style.qss我有:

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

字体大小和边框有效,但工具提示的背景图像与按钮的相同。我也尝试在工具提示中添加另一个背景图像,它也没有用。

如何更改工具提示背景?

c++ qt tooltip qtstylesheets qpushbutton
2个回答
1
投票

您必须指定QWidget应用该属性的位置。如果你不这样做,它会将它应用于小部件的所有孩子。

在您的情况下,要避免工具提示中的背景图像,您必须指定要将该样式应用于QPushButton小部件。文件说:

如果我们希望该属性仅应用于特定对话框的子(或孙子或孙子)的QLineEdits,我们宁愿这样做:

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

在您提到的示例中,如果要修改工具提示和按钮的样式,请执行以下操作:

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

它会给你这样的东西

enter image description here

Update:

如果要将其应用于单个对象而不是其他相同类型的小部件,则文档说:

如果我们希望该属性仅应用于一个特定的QLineEdit,我们可以使用QObject :: setObjectName()为其命名,并使用ID Selector来引用它:

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

所以在你的情况下:

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

0
投票

使用setStyleSheet设置qss时,样式表适用于对象的所有子项。在您的情况下,您可以使用QPushButton的样式表来避免这种情况

button->setStyleSheet("QPushButton {background-image: url(pathToImage);}");
© www.soinside.com 2019 - 2024. All rights reserved.