简体   繁体   English

Qt 4.8:如何拥有一个看起来像 QLabel 的 QPushButton?

[英]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.

相关问题 如何添加QVBoxLayout。 Qlabel和Qpushbutton周围? pyqt5 - How to add QVBoxLayout. around Qlabel and Qpushbutton ? pyqt5 如何在 PyQt 中获取按钮或标签(QPushButton、QLabel)的背景颜色 - How to get the background color of a button or label (QPushButton, QLabel) in PyQt "如何通过单击 QPushButton 在 QLineEdit 中输入输入,然后在 QLabel 中打印结果?" - How to get input entered in QLineEdit by clicking QPushButton and then print result in QLabel? PyQt:显示类似于QComboBox中的向下箭头按钮的QPushButton - PyQt: display QPushButton that looks like the down-arrow button in QComboBox 使用QDialog中的QPushButton更新QLabel - Update a QLabel , using a QPushButton in a QDialog 自动更改 QLabel 和 QPushButton 可见 - change QLabel and QPushButton visible automatically 在 PyQt5 中找到多个随机创建的小部件(如 QLabel、QPushButton 等)的 mousepress 事件的父级 - find parent of mousepress event of multiple randomly created widgets(like QLabel,QPushButton etc.) in PyQt5 如何在Qt中设置QPushButton的背景色的动画? - How to animate the background color of a QPushButton in qt? Qt - 如何获取 QLabel 中字符串的像素长度? - Qt - How to get the pixel length of a string in a QLabel? 如何将QImage(或类似的东西)插入QLabel? - How to insert QImage(or sth like that) into QLabel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM