我试图在PyQt应用程序中嵌入小部件
这是我的代码。
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import numpy as np
import pyqtgraph as pg
from PyQt4 import QtCore
class PlotDashboard(QWidget):
def __init__(self, title, parent=None):
QWidget.__init__(self, parent)
self.layout = QVBoxLayout(self)
self.setLayout(self.layout)
self.label = QLabel('Right', self)
# Create a plot with a date-time axis
self.p1 = pg.PlotWidget(name='Plot1', background=QColor(5, 27, 105, 255), axisItems = {'bottom': pg.DateAxisItem()}) ## giving the plots names allows us to link their axes together
self.p1.setTitle("<strong>" + title + "</strong>")
self.p1.getAxis("left").setPen(pg.mkPen(color='w', width=2))
self.p1.getAxis("bottom").setPen(pg.mkPen(color='w', width=2))
self.p1.showGrid(x=True, y=True, alpha = 0.3)
self.p1.setLabel('left', 'Value')
self.p1.setLabel('bottom', 'Time')
self.p1.addLegend()
self.layout.addWidget(self.label)
self.label.setAlignment(QtCore.Qt.AlignRight)
self.layout.addWidget(self.p1)
self.btnClear = QPushButton(text = 'Clear All')
self.btnClear.setIcon(QIcon(QPixmap('/usr/local/lib/python2.7/dist-packages/taurus/qt/qtgui/icon/Tango/scalable/actions/editclear.svg')))
self.btnClear.clicked.connect(self.clearAll)
self.layout.addWidget(self.btnClear)
# Data holders for cross hair- Plot max 5 data sets
self.data1 = [[], []] # x and y in separate lists
self.data2 = [[], []]
self.data3 = [[], []]
self.data4 = [[], []]
self.data5 = [[], []]
self.curvesData = [self.data1, self.data2, self.data3, self.data4, self.data5]
self.numCurves = 0 # number of curves plotted, runs from 0 to 3
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
self.p1.addItem(vLine, ignoreBounds=True)
self.p1.addItem(hLine, ignoreBounds=True)
vb = self.p1.getViewBox()
def mouseMoved(evt):
pos = evt[0]
if self.p1.sceneBoundingRect().contains(pos):
mousePoint = vb.mapSceneToView(pos)
self.label.setText("x: %d \t y: %d" % (mousePoint.x(), mousePoint.y()))
vLine.setPos(mousePoint.x())
hLine.setPos(mousePoint.y())
self.p1.getViewBox().setAutoVisible(y=True)
proxy = pg.SignalProxy(self.p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
self.p1.proxy = proxy
def plotCurve(self, data, curve_name):
if len(data[0]) == 0 or len(data[1]) == 0: return
if len(data[0]) != len(data[1]): return
if self.numCurves > 4: return # support plotting max 5 curves, need to clear all first
self.curvesData[self.numCurves] = data
self.p1.addLegend()
if self.numCurves == 0:
_pen=(255,0,255)
self.data1 = [data[0], data[1]]
elif self.numCurves == 1:
_pen=(0,255,0)
self.data2 = [data[0], data[1]]
elif self.numCurves == 2:
_pen=(255,100,0)
self.data3 = [data[0], data[1]]
elif self.numCurves == 3:
_pen=(255,255,255)
self.data4 = [data[0], data[1]]
else:
_pen=(255,255,0)
self.data5 = [data[0], data[1]]
self.p1.plot(data[0], data[1], pen=_pen, symbolBrush=_pen, symbolPen='w', symbol='o', symbolSize=14, name=curve_name)
self.numCurves += 1
def clearAll(self):
self.p1.clear()
self.label.clear()
self.data1 = [[], []]
self.data2 = [[], []]
self.data3 = [[], []]
self.data4 = [[], []]
self.data5 = [[], []]
self.curvesData = [self.data1, self.data2, self.data3, self.data4, self.data5]
self.numCurves = 0
if __name__ == "__main__":
app = QApplication(sys.argv)
gui = PlotDashboard("PLOT 1")
gui.show()
sys.exit(app.exec_())
该示例通过将LabelItem添加到GraphicsWindow中来工作,如下所示:
win = pg.GraphicsWindow()
label = pg.LabelItem(justify='right')
win.addItem(label)
p1 = win.addPlot(row=1, col=0)
但我没有GraphicsWindow,只有一个普通的Qt窗口(使用Designer构建),其中包含PlotWidget。我似乎无法将LabelItem或TextItem添加到PlotWidget。我确信一定有一个“标准”的方法来做这件事,但我不明白,谷歌似乎也不知道。有什么想法吗
目前没有回答
相关问题 更多 >
编程相关推荐