[Python] Is there a way to bind multiple related `pyqtgraph.opengl.GLLinePlotItem` objects to...

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    Let's say I have two pyqtgraph.opengl.GLLinePlotItem objects in a pyqtgraph.opengl.GLViewWidget view, where the 2nd line's coordinates depend on the 1st line's coordinates. In the code below, the position of the 2nd line has an offset of +1.

    Is there a way to bind the two objects where it optimizes the rendering process such that you don't have to call setData() for the 2nd line? So in the code below, you wouldn't need to have self.line2.setData(pos=self.pos2), it would automatically render the adjusted position.

    I was thinking there might be a way by overriding the GLViewWidget.paintGL() method and then paint it directly with PySide6.QtGui.QPainter(), and not having a GLLinePlotItem for line 2 at all, but I wasn't sure how to do that...

    import sys
    import numpy as np
    from PySide6 import QtWidgets, QtCore
    import pyqtgraph.opengl as gl

    class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
    super().__init__()
    self.setWindowTitle("OpenGL Line Example")
    self.resize(800, 600)
    self.timer = QtCore.QTimer(self)
    self.timer.setInterval(500)
    self.timer.timeout.connect(self.updateData)
    self.timer.start()
    self.gl_view = gl.GLViewWidget()
    self.setCentralWidget(self.gl_view)
    self.x = np.linspace(-1, 1, 100)
    self.y = np.linspace(-1, 1, 100)
    self.z = np.zeros_like(self.x)
    self.pos1 = np.column_stack((self.x, self.y, self.z))
    self.pos2 = np.column_stack((self.x+1, self.y+1, self.z+1))
    self.line1 = gl.GLLinePlotItem(pos=self.pos1, color=(1, 1, 0, 1), width=2)
    self.line2 = gl.GLLinePlotItem(pos=self.pos2, color=(1, 0, 0, 1), width=2)
    self.gl_view.addItem(self.line1)
    self.gl_view.addItem(self.line2)

    def updateData(self):
    self.z += np.random.normal(0, 0.5)

    self.pos1[:,2] = self.z
    self.pos2[:,2] = self.z + 1

    self.line1.setData(pos=self.pos1)
    self.line2.setData(pos=self.pos2)

    if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

    Continue reading...

Compartilhe esta Página