如何为 QPushButton 的工具提示设置背景?
How can I set background for a QPushButton's tooltip?
在 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;
}
字体大小和边框有效,但工具提示的背景图像与按钮的相同。
我也试过在工具提示中添加另一个背景图像,它也没有用。
如何更改工具提示的背景?
当您使用 setStyleSheet
设置 qss 时,您的样式表适用于对象的所有子对象。在您的情况下,您可以仅使用 QPushButton
的样式表来避免这种情况
button->setStyleSheet("QPushButton {background-image: url(pathToImage);}");
您必须指定 QWidget
应用 属性 的位置。如果您不这样做,它会将其应用于小部件的所有子项。
在您的情况下,为了避免工具提示中出现背景图像,您必须指定要将该样式应用于 QPushButton 小部件。文档说:
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 }");
在您提到的示例中,如果要修改工具提示和按钮的样式,请执行以下操作:
ui->pushButton->setStyleSheet(""
"QPushButton { background-image: url(me.png); }"
"QToolTip { color: #ffffff; background-color: #000000; border: 0px; }");
它会给你这样的东西
更新:
如果您想将它应用于单个对象而不是其他相同类型的小部件,文档说:
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 }");
所以在你的情况下:
ui->pushButton->setObjectName("awesomeButton");
ui->pushButton->setStyleSheet("QPushButton#awesomeButton { background-image: url(me.png); }");
在 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;
}
字体大小和边框有效,但工具提示的背景图像与按钮的相同。 我也试过在工具提示中添加另一个背景图像,它也没有用。
如何更改工具提示的背景?
当您使用 setStyleSheet
设置 qss 时,您的样式表适用于对象的所有子对象。在您的情况下,您可以仅使用 QPushButton
button->setStyleSheet("QPushButton {background-image: url(pathToImage);}");
您必须指定 QWidget
应用 属性 的位置。如果您不这样做,它会将其应用于小部件的所有子项。
在您的情况下,为了避免工具提示中出现背景图像,您必须指定要将该样式应用于 QPushButton 小部件。文档说:
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 }");
在您提到的示例中,如果要修改工具提示和按钮的样式,请执行以下操作:
ui->pushButton->setStyleSheet(""
"QPushButton { background-image: url(me.png); }"
"QToolTip { color: #ffffff; background-color: #000000; border: 0px; }");
它会给你这样的东西
更新:
如果您想将它应用于单个对象而不是其他相同类型的小部件,文档说:
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 }");
所以在你的情况下:
ui->pushButton->setObjectName("awesomeButton");
ui->pushButton->setStyleSheet("QPushButton#awesomeButton { background-image: url(me.png); }");