简体   繁体   English

将 pyqtgraph 图嵌入到 QT .ui 中?

[英]Embed a pyqtgraph plot into a QT .ui?

first of all I hope you have me some patience since I'm new on these kind of projects and I also hope not to be asking dumb questions.首先,我希望你对我有一些耐心,因为我是这些项目的新手,我也希望不要问愚蠢的问题。 That being said, my main objective is to create a UI for a raspberry pi 3 which will sense voltage, current, etc from a battery and from a solar panel.话虽如此,我的主要目标是为 raspberry pi 3 创建一个 UI,它将检测来自电池和太阳能电池板的电压、电流等。

Since I'm working on a raspberry and have some knowledge on Python3, I decided to use QTCreator which as I understand can be translated into python3 through pyqt ( https://nikolak.com/pyqt-qt-designer-getting-started/ ).由于我正在研究树莓并且对 Python3 有一些了解,因此我决定使用 QTCreator,据我所知,它可以通过 pyqt 转换为 python3( https://nikolak.com/pyqt-qt-designer-getting-started/ )。 I installed it on my raspberry pi and made the following UI:我将它安装在我的树莓派上并制作了以下 UI:

after having a basic UI, I converted the .ui file into .py with the pyuic5 command and I'm able to open the UI with "python3 main.py" and everything seems right:有了基本的 UI 后,我使用 pyuic5 命令将 .ui 文件转换为 .py,然后我可以使用“python3 main.py”打开 UI,一切似乎都正确:

how the UI looks after opening the main.py file打开 main.py 文件后 UI 的外观

Now, I want to have a several plots (like voltage against time, etc) on the UI.现在,我想在 UI 上有几个图(如电压对时间等)。 I'm using the following for testing:我正在使用以下内容进行测试:

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_()

Is it possible to embbed that plot into my main.py file?是否可以将该图嵌入到我的 main.py 文件中? If I understood correctly, I'm supposed to use the promoting widget functionality on QTCreator.如果我理解正确,我应该使用 QTCreator 上的推广小部件功能。 Thanks in advance guys!在此先感谢各位!

What is promoted through Qt Designer is a Widget, ie a class, so it can not be directly promoted, what we must do is place it inside a class as shown below:通过Qt Designer提升的是一个Widget,即一个类,所以不能直接提升,我们要做的就是把它放在一个类里面,如下图:

Plotter.py绘图仪.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_()

Before proceeding I will assume that the files have the following structure:在继续之前,我将假设文件具有以下结构:

.
├── main.py
└── Plotter.py
  1. The first thing to do is choose the widget:首先要做的是选择小部件:

在此处输入图片说明

  1. Then we right click on this and choose the option to promote to.. :然后我们右键单击它并选择升级到..的选项:

在此处输入图片说明

  1. In the dialog box we place CustomWidget in Promoted Class Name and Plotter.h in Header File , then press the Add and Promote button.在对话框中,我们将CustomWidget放置在“ Promoted Class Name”中,将Plotter.h 放置在“头文件”中,然后按“添加提升”按钮。

在此处输入图片说明

  1. Then we convert our .ui file to .py然后我们将 .ui 文件转换为 .py

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM