简体   繁体   English

pyqt5 QPushButtone hover 风格,当它是平坦的=真

[英]pyqt5 QPushButtone hover style when it's flat=True

I want to make a hover style of a QPushButton When it is flat, is it possible?.我想制作一个 QPushButton 的 hover 样式当它是扁平的时,有可能吗? Seems hover is not showing with flat.似乎 hover 没有显示平坦。 If not, how can I make a similar flat style with a QPushButton with hover.如果没有,我如何使用带有 hover 的 QPushButton 制作类似的平面样式。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()
    
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        
        self.button = QPushButton('PyQt5 button', self, flat=True)
        self.button.setStyleSheet(""" QPushButton {
                                                                background-color: yellow;
                                                                }
                                                                
                                                                QPushButton#pushButton:hover {
                                                                    background-color: blue;
                                                                }
                                                                
                                                                QPushButton#pushButton:pressed {
                                                                    background-color: orange;     
                                                                }"""
                                                            )
        self.button.move(100,70)
        self.button.clicked.connect(self.on_click)
        
        self.show()

    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Here is a simple example how to make flat button with hover这是一个简单的例子,如何用 hover 制作平面按钮

self.button = QPushButton('PyQt5 button',self)
self.button.setStyleSheet(open('style.css').read())

And style.css和款式.css

QPushButton {
    padding: 5px;
    border-color: black;
    border-style: outset;
    border-width: 2px;
    color: black;
    background-color: yellow;
}
QPushButton:hover {
    background-color: blue;
}
QPushButton:pressed {
    background-color: orange;     
}

Link to documentation to read more options.链接到文档以阅读更多选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM