Embed a pyqtgraph plot into a QT .ui?

前端 未结 1 1389
忘了有多久
忘了有多久 2021-01-03 15:36

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

相关标签:
1条回答
  • 2021-01-03 16:29

    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:

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

    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.

    1. Then we convert our .ui file to .py

    0 讨论(0)
提交回复
热议问题