【发布时间】:2016-01-28 02:57:14
【问题描述】:
我正在寻找一种在 QT 应用程序主窗口内的小部件上绘制线条的简单方法。 我用 QtDesigner 设计了一个带有 QGraphicsview 和一个按钮的主窗口。按下按钮时,应在图形视图中绘制一条线。没有一个简单的方法可以做到这一点。 QGraphicsview 是绘图的正确选择吗?我现在看了几十篇文章,真的没有找到解决办法。 简而言之,这是我的代码:
import sys
import mainwindowui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MainWindow(QMainWindow, mainwindowui.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.connect(self.pushButton1, SIGNAL("clicked()"), self.pb1_pressed)
def pb1_pressed(self):
# here I want to draw a line on the QGraphicswidget:
# something simple like: graphview.line(x1,y1,x2,y2)
我看到了很多看起来非常复杂的解决方案,我尝试了很多,例如使用 QPainter 覆盖paintevent,但出现“QPainter::begin: Paint device returned engine == 0, type: 1”之类的错误,我不知道这是否是正确的方法。 有人可以给我一个非常简单的解决方案吗? 提前谢谢你。
迪特
【问题讨论】: