[Python] PyQt6 6.9.0 QPainter errors/warnings in command prompt but not in PyCharm

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    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('Resizing Window ')
    window.resizeEvent(None)
    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
    Resizing Window
    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
    Resizing Window
    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
    ​

    Continue reading...

Compartilhe esta Página