我不明白为什么我得到 QPainter::begin: Widget Painting can only begin as a result of apaintEvent
我一直在查看有关此错误的其他 StackOverflow 问题(以及网络上的其他地方),但我不明白这些答案与我的代码有何关系。因此,我希望有一个对我有意义的固定示例,或者更好地解释事件如何以及何时发生。
下面的代码旨在计算出其运行的屏幕尺寸,调整大小并在中心绘制一个占据大部分可用屏幕空间的圆圈。它试图做更多的事情,但我已经把它剥离了——我希望足够了。现在它只是尝试画一个圆圈。
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Viewport(QGraphicsView):
def __init__(self, parent=None):
super(Viewport, self).__init__(parent)
self.scene = QGraphicsScene(self)
self.setScene(self.scene)
def paintEvent(self, event):
super(Viewport, self).paintEvent(event)
qp = QPainter()
qp.begin(self)
square = QRect(10, 10, 30, 30)
qp.drawEllipse(square)
qp.end()
class UI(QDialog):
def __init__(self, parent=None):
super(UI, self).__init__(parent)
self.view = Viewport(self)
gridLayout = QGridLayout()
gridLayout.addWidget(self.view, 0, 0, 1, 1)
self.setLayout(gridLayout)
def resizeEvent(self, event):
super(UI, self).resizeEvent(event)
self.view.setFrameShape(QFrame.NoFrame)
self.view.setSceneRect(0, 0, 400, 400)
self.view.setFixedSize(400, 400)
app = QApplication(sys.argv)
ui = UI()
ui.show()
sys.exit(app.exec_())
上面的代码被删除了,其中有一个移动的 SVG 项目,并且圆圈最初有渐变填充。 SVG 项目显示和移动正常,但圆圈从未出现。
当由 QGroupBox 的 PaintEvent 绘制时,渐变填充的圆圈在另一个程序中工作得很好,但我无法理解 QGraphicsScene 和 QGraphicsView 的工作原理。
更新
错误消息,正如我所看到的(遗憾的是没有行号):
$ ./StackOverflow.py
QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::end: Painter not active, aborted
I've been looking at other StackOverflow questions regarding this error (and elsewhere on the web) but I don't understanding how the answers relate to my code. So, I'm hoping for either a fixed example that makes sense to me, or a better explanation of how and when events occur.
The code below was intended to figure out the dimensions of the screen it's running on, resize to that and draw a circle in the center that occupies most of the available screen real estate. It tried to do a lot more, but I've stripped it down -- enough, I hope. Now it just tries to draw a circle.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Viewport(QGraphicsView):
def __init__(self, parent=None):
super(Viewport, self).__init__(parent)
self.scene = QGraphicsScene(self)
self.setScene(self.scene)
def paintEvent(self, event):
super(Viewport, self).paintEvent(event)
qp = QPainter()
qp.begin(self)
square = QRect(10, 10, 30, 30)
qp.drawEllipse(square)
qp.end()
class UI(QDialog):
def __init__(self, parent=None):
super(UI, self).__init__(parent)
self.view = Viewport(self)
gridLayout = QGridLayout()
gridLayout.addWidget(self.view, 0, 0, 1, 1)
self.setLayout(gridLayout)
def resizeEvent(self, event):
super(UI, self).resizeEvent(event)
self.view.setFrameShape(QFrame.NoFrame)
self.view.setSceneRect(0, 0, 400, 400)
self.view.setFixedSize(400, 400)
app = QApplication(sys.argv)
ui = UI()
ui.show()
sys.exit(app.exec_())
The above was stripped out of broken code that had a moving SVG item and the circle originally had a gradient fill. The SVG item was displaying and moving okay but the circle never showed up.
The gradient-filled circle worked fine in another program when it was in a drawn by a paintEvent for a QGroupBox, but I cannot grok how QGraphicsScene and QGraphicsView work.
UPDATED
The error message, exactly as I see it (sadly w/o line numbers):
$ ./StackOverflow.py
QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::end: Painter not active, aborted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
viewport()
上绘画:You need to paint on the
viewport()
: