python - "QPainter::begin: Paint device returned engine == 0, type: 1"

标签 python pyqt pyside qpainter

我有以下测试代码:

import sys
from PySide.QtGui import *

app = QApplication(sys.argv)
widget = QWidget()
painter = QPainter(widget)

在创建 QPainter 对象时,我收到错误消息:

QPainter::begin: Paint device returned engine == 0, type: 1

为什么?

最佳答案

如果你想在一个widget里面画东西,你需要使用widget的paintEvent来定义一个QPainter。此方法允许为立即绘画声明一个 Qpainter,顺便说一句,它避免调用 Qpainter.begin()Qpainter.end().

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawLine(0, 0, 100, 100)

app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())

http://doc.qt.io/qt-5/qpainter.html#details

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent().

关于python - "QPainter::begin: Paint device returned engine == 0, type: 1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45458342/

相关文章:

python - 谐音替换密码 Python

python - 使用 bash 或 python 或其他一些 linux 命令行工具创建一个 dovecot SHA1 摘要

python - 安装 python 库时需要 RuntimeError : Python version >= 3. 5

python - 提高在 matplotlib 中重绘等高线图的速度

python - 如何创建正确跨越多个页面的可打印列表

python - pyside:QFileDialog 返回一个空列表

python - 区分 PySide 中的信号源

python - 如何通过QFileDialog获取pyqt6中的目录路径?

python - PyQt 从 excel 填充 QTableWidget

javascript - 使用 PySide QtCore.Slot 装饰器的多个参数