我试图在pyqtgraph中不使用pg.PolyLineROI()来绘制多边形。我的目标是能够使用大型数据集代替"数据"在下面显示的代码中。我对PolyLineROI()的问题是我不需要句柄或事件,因此加载大量数据需要很长的时间和资源,而这些资源只是浪费在不需要的功能上。
我尝试过使用QPainter和QPen,但是我无法得到任何有效的东西,所以我被卡住了。有什么想法吗?
EDITED代码尝试合并来自segFaultCoder的示例
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import sys
class plotwindow(QtGui.QMainWindow):
def setupUi(self, MainWindow):
self.centralwidget = QtGui.QWidget(MainWindow)
MainWindow.resize(1900, 1000)
self.qt = pg.GraphicsView(MainWindow)
self.qt.setGeometry(QtCore.QRect(0,0, 1900, 1000))
self.qt2 = pg.GraphicsLayout()
self.qt.setCentralItem(self.qt2)
self.qt.show()
self.layout = self.qt2.addLayout()
self.qt3 = self.layout.addViewBox()
self.plot()
def plot(self): #This is looped for multiple data sets
data = [[6,6],[6,0],[0,6],[0,0]] #changes based on data import
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
self.points = []
for item in data:
point = QtCore.QPoint(item[0], item[1])
self.points.append(point)
p.drawPolygon(*self.points)
p.end()
self.qt3.addItem(p)
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = plotwindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:2)
我认为你和QPainter走在正确的轨道上,只要查看他们的示例文件夹,你就会看到customGraphicsItem.py。我基本上只是复制了它并用你的点替换了它们的“data”变量,然后使用QPainter.drawPolygon()重写了generatePicture()方法。希望我所有的缩进都是正确的,我不能完全了解SO上的格式。
import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui
# Create a subclass of GraphicsObject.
# The only required methods are paint() and boundingRect()
# (see QGraphicsItem documentation)
class customPolyItem(pg.GraphicsObject):
def __init__(self, data):
pg.GraphicsObject.__init__(self)
self.data = data
self.points = []
self.generatePicture()
def generatePicture(self):
# pre-computing a QPicture object allows paint() to run much more quickly,
# rather than re-drawing the shapes every time.
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
for item in self.data:
point = QtCore.QPoint(item[0],item[1])
self.points.append(point)
p.drawPolygon(*self.points)
p.end()
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
# boundingRect _must_ indicate the entire area that will be drawn on
# or else we will get artifacts and possibly crashing.
# (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
data = [[6,6],[6,0],[0,6],[0,0]]
item = customPolyItem(data)
plt = pg.plot()
plt.addItem(item)
plt.setWindowTitle('pyqtgraph example: customPolyItem')
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
编辑:关于你的新代码,用这个替换你的绘图方法(并删除绘画和边界):
def plot(self): #This is looped for multiple data sets
data = [[6,6],[6,0],[0,6],[0,0]] #changes based on data import
newCPI = customPolyItem(data) # create new instance with changed data
self.qt3.addItem(newCPI) # add the new instance to your viewbox
如果它位于单独的文件中,您将需要导入customPolyItem类,或者只需将整个类声明复制粘贴到当前plotwindow类下面。