[英]Qt 4.8: how to have a QPushButton that looks like a QLabel?
I want to draw a QPushButton
(clickable etc), but have it look like a QLabel
.我想画一个
QPushButton
(可点击等),但让它看起来像一个QLabel
。 (Another way to see this is I want a clickable label, ie a label that behaves like a button, but that seems more difficult.) (另一种查看方式是我想要一个可点击的标签,即行为类似于按钮的标签,但这似乎更困难。)
I have tried button.setStyleSheet("QLabel")
, doesn't work.我试过
button.setStyleSheet("QLabel")
,不起作用。 Is there a way to tell the button to use another style class?有没有办法告诉按钮使用另一个样式类? or "steal" the style from
QLabel
?还是“窃取”
QLabel
的样式?
If possible I would like to avoid redefining the whole style (I do not own the stylesheet, Qt is embedded inside a third-party application), unless this is trivial.如果可能,我想避免重新定义整个样式(我不拥有样式表,Qt 嵌入在第三方应用程序中),除非这是微不足道的。
Edit: I ended up using @musicamante's stylesheet solution, to which I added text-align: left
(buttons are centered, labels are not), and :hover/:pressed
colors (cyan/blue looks like programmer-art, but serves the purpose :):编辑:我最终使用了@musicamante 的样式表解决方案,我添加了
text-align: left
(按钮居中,标签不是)和:hover/:pressed
颜色(青色/蓝色看起来像程序员艺术,但服务于目的 :):
QPushButton {
border: none;
background: transparent;
text-align: left;
color: palette(window-text);
}
QPushButton:hover {
color: cyan;
}
QPushButton:pressed {
color: blue;
}
Styling a button like a label is not usually suggested as it wouldn't make it clear its state, especially when the mouse button is pressed and released outside the button rectangle.通常不建议将按钮样式设置为标签,因为它不会清除其状态,尤其是在按钮矩形外按下并释放鼠标按钮时。
In any case, it's enough to set the border
property to none
, which will also remove the background.在任何情况下,将
border
属性设置为none
就足够了,这也将删除背景。 Just to be safe and consistent, you could make the background transparent (some style might override it) and ensure that the WindowText
color role of the palette is used (instead of the ButtonText
role):为了安全和一致,您可以使背景透明(某些样式可能会覆盖它)并确保使用调色板的
WindowText
颜色角色(而不是ButtonText
角色):
button.setStyleSheet("""
border: none;
color: palette(window-text);
background: transparent;
""")
Making a clickable label is not that hard, though, and has the benefit of allowing usage of rich text content, something that a QPushButton cannot do.然而,制作一个可点击的标签并不难,它的好处是允许使用富文本内容,这是 QPushButton 无法做到的。
class ClickableLabel(QLabel):
clicked = Signal()
def mousePressEvent(self, event):
super(ClickableLabel, self).mousePressEvent(event)
if event.button() == Qt.LeftButton:
self.clicked.emit()
Or, to provide consistency with the behavior of a button (as described above, a "click" is considered valid if the mouse button is released within the boundaries of the button rectangle):或者,为了提供与按钮行为的一致性(如上所述,如果在按钮矩形的边界内释放鼠标按钮,则“单击”被认为是有效的):
class ClickableLabel(QLabel):
clicked = Signal()
def mouseReleaseEvent(self, event):
super(ClickableLabel, self).mouseReleaseEvent(self, event)
if event.button() == Qt.LeftButton and event.pos() in self.rect():
self.clicked.emit()
you can create a custom QLabel,first public inherit a QLabel,then overload the void mouseReleaseEvent(QMouseEvent *e)您可以创建自定义 QLabel,首先公开继承 QLabel,然后重载 void mouseReleaseEvent(QMouseEvent *e)
void mouseReleaseEvent(QMouseEvent *e){
if(e.button == Qt::LeftButton){
//emit a signal or you want to do when the label are clicked
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.