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
  • Please provide a more comprehensive minimal reproducible example and also include exact details about your OS and Qt (not PyQt) version. – musicamante Commented Mar 13 at 18:30
  • @musicamante Hello, thank you for responding. I've added the whole code and I'm on Windows 11 Pro, build 26100. I'm sorry, but I don't know how to find out what Qt version that is, I just installed PyQt6 with pip install PyQt6, that's the whole process. – Person Commented Mar 13 at 19:49
  • A simple search for "get pyqt version" would've sufficed. Get the value of QtCore.QT_VERSION_STR. I cannot reproduce this on Linux, it may be style related (so, try self.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:40
  • @musicamante It's 6.8.2. I tried the self.app.setStyle('fusion'), but it doesn't work again, unless I define a border on the QPushButton. I tried only placing the QPushButton onto the main window using the setCentralWidget function, but there, the styling was still the same. – Person Commented Mar 14 at 7:07
  • 1 You're not "stupid or something", but you have to be more careful about: 1. what's being suggested to you; 2. trying to follow those suggestions as accurately as possible; 3. providing appropriate response about the results of those suggestions, including what you actually did. You previously wrote "I tried [...] but it doesn't work again", which led us to think that doing app.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
 |  Show 6 more comments

1 Answer 1

Reset to default 0

It 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