When I create a simple application with the below code, I get the additional errors/messages (all starting with QPainter) in the command line output in some scenarios (detailed in table below).
It looks like a bug with PyQt6's latest version (6.9.0) but there is no way to debug since none of my code stop points can catch those items.
I can't debug effectively since Pycharm doesn't show error messages in any of the runs. Any guidance on debugging further?
P.S. After the program goes into app execution, I hit windows right on the window to clip the window to the right side of my window (to trigger resize event manually).
Code
import sys
from PyQt6.QtWidgets import (
QApplication,
QMainWindow,
QWidget,
QVBoxLayout,
QLabel,
)
from PyQt6.QtGui import QPainter, QColor
from PyQt6.QtCore import Qt
class MyWidget(QWidget):
def __init__(self):
print('--------MyWidget Init Start ')
super().__init__()
self.init_ui()
print('--------MyWidget Init End ')
def init_ui(self):
print('--------MyWidget Init UI Start ')
self.setGeometry(100, 100, 300, 200)
layout = QVBoxLayout()
self.setLayout(layout)
label = QLabel("This is a simple Qt application ")
layout.addWidget(label)
print('--------MyWidget Init UI End ')
def paintEvent(self, event):
print('--------MyWidget Paint Start ')
painter = QPainter(self)
painter.setPen(Qt.GlobalColor.blue)
painter.drawLine(0, 0, 200, 100) # Example line drawing
print('--------MyWidget Paint End ')
class MainWindow(QMainWindow):
def __init__(self):
print('----MainWindow Init Start ')
super().__init__()
self.init_ui()
print('----MainWindow Init End ')
def init_ui(self):
print('----MainWindow Init UI Start ')
self.setWindowTitle("Simple Qt Example ")
self.setGeometry(100, 100, 600, 400)
widget = MyWidget()
self.setCentralWidget(widget)
print('----MainWindow Init UI End ')
if __name__ == "__main__":
print('Initialize Application ')
app = QApplication([])
print('Initialize MainWindow ')
window = MainWindow()
print('Show MainWindow ')
window.show()
print('Going for App execution ')
sys.exit(app.exec())
PyCharm/DOS Command prompt | Version of PyQt6 | Error messages appear |
---|---|---|
DOS Prompt | 6.9.0 | Yes |
PyCharm | 6.9.0 | No |
DOS Prompt | 6.8.1 | No |
PyCharm | 6.8.1 | No |
DOS Prompt | 6.8.0 | No |
PyCharm | 6.8.0 | No |
"Error Messages Appear" = "No" scenario Output
Initialize Application
Initialize MainWindow
----MainWindow Init Start
----MainWindow Init UI Start
--------MyWidget Init Start
--------MyWidget Init UI Start
--------MyWidget Init UI End
--------MyWidget Init End
----MainWindow Init UI End
----MainWindow Init End
Show MainWindow
Going for App execution
--------MyWidget Paint Start
--------MyWidget Paint End
"Error Messages Appear" = "Yes" scenario Output
Initialize Application
Initialize MainWindow
----MainWindow Init Start
----MainWindow Init UI Start
--------MyWidget Init Start
--------MyWidget Init UI Start
--------MyWidget Init UI End
--------MyWidget Init End
----MainWindow Init UI End
----MainWindow Init End
Show MainWindow
Going for App execution
--------MyWidget Paint Start
--------MyWidget Paint End
QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::setCompositionMode: Painter not active
QPainter::fillRect: Painter not active
QPainter::setCompositionMode: Painter not active
QPainter::setBrush: Painter not active
QPainter::setPen: Painter not active
QPainter::drawRects: Painter not active
QPainter::setPen: Painter not active
QPainter::setFont: Painter not active
QPainter::setFont: Painter not active
QPainter::setBrush: Painter not active
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted
QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::setCompositionMode: Painter not active
QPainter::fillRect: Painter not active
QPainter::setCompositionMode: Painter not active
QPainter::setBrush: Painter not active
QPainter::setPen: Painter not active
QPainter::drawRects: Painter not active
QPainter::setPen: Painter not active
QPainter::setFont: Painter not active
QPainter::setFont: Painter not active
QPainter::setBrush: Painter not active
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted
--------MyWidget Paint Start
--------MyWidget Paint End
--------MyWidget Paint Start
--------MyWidget Paint End
--------MyWidget Paint Start
--------MyWidget Paint End
--------MyWidget Paint Start
--------MyWidget Paint End
--------MyWidget Paint Start
--------MyWidget Paint End
The main issue is caused by a bug (possibly) introduced since Qt 6.9.0: see QTBUG-135844.
As a recent update to the bug reports, it should have now been fixed, but starting with Qt 6.9.2 (which is planned to be released in mid august 2025).
If, despite the warnings, you don't see any issue with your program, you can keep using the version you're using; otherwise downgrade to a previous version that doesn't have that issue (the report says that the bug only affects the 6.9 branch, therefore the previous 6.8.3 version could theoretically be fine, but I cannot guarantee it) as long as it's compatible with your needs, and wait for the official release of 6.9.2.
Note: PyQt versions don't always follow the Qt version numbering; specifically, the "patch" number (the number after the second dot) is often inconsistent, because it may refer to a "patch update" necessary for PyQt, but not caused by a patch version bump in the Qt library.
The reason for which you're able to see those warnings only in the command prompt is that Python IDEs like PyCharm are normally able to display errors/warnings only when coming from the Python interpreter. PyQt is a binding around a C++ written library (Qt), therefore you can't see any output that doesn't come from Python itself.
In reality, the "error" occurs no matter what, you're only able to see it under proper circumstances.
Such IDEs need to be properly configured in order to display such "external" output (for example, see this post for PyCharm), yet it's still good practice to always test your programs outside the IDE.
An unrelated suggestion: as a rule of thumb, you should always be careful in installing newly released major and minor versions, unless you do know what you're doing and you're aware of the possible consequences.
Always consider the available versions and when they were released. For instance, Qt 6.9.0 is a new minor version, meaning that, while it could add new features, it could also possibly introduce new bugs caused by those changes. The previous released version was 6.8.3, published less than 3 months ago, so it's potentially safer to use: unless you really need aspects introduced in 6.9 (including both new features or fixes you know about), you should prefer the older one.