将 pyqtgraph 图嵌入到 QT 中。ui?

Embed a pyqtgraph plot into a QT .ui?

首先,我希望您能给我一些耐心,因为我是这类项目的新手,我也希望不要问愚蠢的问题。话虽这么说,我的主要 objective 是为 raspberry pi 3 创建一个 UI,它将感测来自电池和太阳能电池板的电压、电流等。

由于我正在研究树莓派并且对 Python3 有一些了解,所以我决定使用 QTCreator,据我所知可以通过 pyqt 将其翻译成 python3 (https://nikolak.com/pyqt-qt-designer-getting-started/ ).我将它安装在我的 raspberry pi 上并做了以下 UI:

在有了基本的 UI 之后,我使用 pyuic5 命令将 .ui 文件转换为 .py 并且我能够打开 UI 与 "python3 main.py" 一切似乎都正确:

UI 打开 main.py 文件后的样子

现在,我想在 UI 上绘制几张图(如电压随时间变化等)。我正在使用以下内容进行测试:

import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
win = pg.GraphicsWindow()
win.setWindowTitle('pyqtgraph example: Scrolling Plots')


p1 = win.addPlot(labels =  {'left':'Voltage', 'bottom':'Time'})
data1 = np.random.normal(size=10)
data2 = np.random.normal(size=10)
curve1 = p1.plot(data1, pen=(3,3))
curve2 = p1.plot(data2, pen=(2,3))
ptr1 = 0

def update1():
    global data1, curve1, data2, ptr1

    data1[:-1] = data1[1:]  # shift data in the array one sample left
                            # (see also: np.roll)
    data1[-1] = np.random.normal()
    ptr1 += 1

    curve1.setData(data1)
    curve1.setPos(ptr1,0)



    data2[:-1] = data2[1:]  # shift data in the array one sample left
                            # (see also: np.roll)
    data2[-1] = np.random.normal()
    curve2.setData(data2)
    curve2.setPos(ptr1,0)


def update():
    update1()

timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(2000) # number of seconds (every 1000) for next update



if __name__ == '__main__':
    QtGui.QApplication.instance().exec_()

是否可以将该图嵌入到我的 main.py 文件中?如果我理解正确的话,我应该使用 QTCreator 上的推广小部件功能。提前谢谢大家!

通过Qt Designer提升的是一个Widget,即一个class,所以不能直接提升,我们必须把它放在一个class里面,如下图:

Plotter.py

class  CustomWidget(pg.GraphicsWindow):
    pg.setConfigOption('background', 'w')
    pg.setConfigOption('foreground', 'k')
    ptr1 = 0
    def __init__(self, parent=None, **kargs):
        pg.GraphicsWindow.__init__(self, **kargs)
        self.setParent(parent)
        self.setWindowTitle('pyqtgraph example: Scrolling Plots')
        p1 = self.addPlot(labels =  {'left':'Voltage', 'bottom':'Time'})
        self.data1 = np.random.normal(size=10)
        self.data2 = np.random.normal(size=10)
        self.curve1 = p1.plot(self.data1, pen=(3,3))
        self.curve2 = p1.plot(self.data2, pen=(2,3))

        timer = pg.QtCore.QTimer(self)
        timer.timeout.connect(self.update)
        timer.start(2000) # number of seconds (every 1000) for next update

    def update(self):
        self.data1[:-1] = self.data1[1:]  # shift data in the array one sample left
                            # (see also: np.roll)
        self.data1[-1] = np.random.normal()
        self.ptr1 += 1
        self.curve1.setData(self.data1)
        self.curve1.setPos(self.ptr1, 0)
        self.data2[:-1] = self.data2[1:]  # shift data in the array one sample left
                            # (see also: np.roll)
        self.data2[-1] = np.random.normal()
        self.curve2.setData(self.data2)
        self.curve2.setPos(self.ptr1,0)

if __name__ == '__main__':
    w = CustomWidget()
    w.show()
    QtGui.QApplication.instance().exec_()

在继续之前,我假设文件具有以下结构:

.
├── main.py
└── Plotter.py
  1. 首先要做的是选择小部件:

  1. 然后我们右键单击它并选择提升到..:

  1. 在对话框中我们将 CustomWidget 放在 Promoted Class NamePlotter.h 头文件 中,然后按 添加 提升 按钮。

  1. 然后我们将 .ui 文件转换为 .py