You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt6中QPushButton:hover无法改变背景颜色的问题求助

PyQt6中QPushButton:hover无法改变背景颜色的问题求助

我用PyQt6写了这段代码:

from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton
from PyQt6.QtCore import QSize, QPoint

class AppWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        
        self.setWindowTitle('METAR Tool')
        self.setFixedSize(QSize(500, 300))
        
        self.button = QPushButton(parent=self, text='Fetch METAR')
        self.button.setStyleSheet("""
            QPushButton {
                color: white;
                background-color: #42baff;
            }
            QPushButton:hover {
                color: white;
                background-color: #3ca8e6;
            }
        """)
        self.button.setFixedSize(QSize(400, 60))
        self.button.move(QPoint(50, 220))

class AppCore():
    def __init__(self):
        self.app = QApplication([])

        self.window = AppWindow()
        self.window.show()

    def run(self):
        self.app.exec()

AppCore().run()

但不知道为什么它不生效:按钮的文本颜色能正常变化,但鼠标悬停时QPushButton的背景颜色就是不变——除非我给QPushButton定义任何边框样式,这时候hover的背景色就正常工作了。

我试过用PyQt5运行这段代码,没问题,但我想继续用PyQt6解决这个问题。

备注:内容来源于stack exchange,提问作者Person

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:53:11