admin管理员组文章数量:1391977
I have written this code in 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()
but for some reason, it doesn't work. The text colour changes, but the background colour of the QPushButton
does not change when hovering over it, unless I define any border on QPushButton
. When I do, it works normally.
I tried using PyQt5 to see whether it works there and voila, it does, but I want to use PyQt6.
I have written this code in 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()
but for some reason, it doesn't work. The text colour changes, but the background colour of the QPushButton
does not change when hovering over it, unless I define any border on QPushButton
. When I do, it works normally.
I tried using PyQt5 to see whether it works there and voila, it does, but I want to use PyQt6.
Share Improve this question edited Mar 27 at 0:39 musicamante 48.7k8 gold badges41 silver badges74 bronze badges asked Mar 13 at 10:36 PersonPerson 35 bronze badges 11 | Show 6 more comments1 Answer
Reset to default 0It looks like the issue is related to a known Qt bug (QTBUG-134497), which affects how QPushButton hover styles are applied in PyQt6 when using certain styles (especially the default Windows 11 style).
try this style
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(u"""
QPushButton {
color: white;
background-color: #42baff;
border: 0.5px solid #42baff;
border-radius:5px;
}
QPushButton:hover {
color: white;
background-color: #3ca8e6;
border: 0.5px solid #3ca8e6;
border-radius:5px;
}
""")
# border: 2px solid black;
# border: 2px solid yellow;
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()
Qt provides several built-in styles that define how widgets are rendered. These styles affect the appearance and behavior of Qt applications across different platforms
on some systems the default Qt style may interfere with how stylesheets are applied especially for hover effects switching to the Fusion
style ensures that Qt applies styles more consistently as Fusion
is designed to work well with stylesheets
self.app.setStyle("Fusion")
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.app.setStyle("Fusion")
self.window = AppWindow()
self.window.show()
def run(self):
self.app.exec()
AppCore().run()
本文标签: pythonPyQt6 QPushButtonhover not changing background colourStack Overflow
版权声明:本文标题:python - PyQt6 QPushButton:hover not changing background colour - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744707332a2620918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pip install PyQt6
, that's the whole process. – Person Commented Mar 13 at 19:49QtCore.QT_VERSION_STR
. I cannot reproduce this on Linux, it may be style related (so, tryself.app.setStyle('fusion')
right after creating the application); be aware, though, that you're normally not expected to create widgets as direct children of the QMainWindow, and you should set central widget instead; you should also use layout managers, as using fixed geometries is always discouraged. These aspect may not be related to the issue, but they're still very important nonetheless. – musicamante Commented Mar 14 at 0:40self.app.setStyle('fusion')
, but it doesn't work again, unless I define a border on theQPushButton
. I tried only placing the QPushButton onto the main window using thesetCentralWidget
function, but there, the styling was still the same. – Person Commented Mar 14 at 7:07app.setStyle('fusion')
did not work in your case; yet, now you claim it does work, which makes a huge difference, but we (which includes you) must know about that context. That's how proper debugging is done. – musicamante Commented Mar 24 at 5:03