Incömplete
Incömplete

Reputation: 943

pyqtgraph with Qt Designer: how to add PlotItems to GraphicsLayoutWidget

According to pyqtgraph's documentation, I can promote a QGraphicsView to a GraphicsLayoutWidget. If I'm not mistaken, I can also promote a QGraphicsView to a PlotItem, where the plot lives. Now my question is, how do I add many PlotItems (promoted from QGraphicsView) to the GraphicsLayoutWidget (also promoted from QGraphicsView) using Qt Designer?

I'm using pyside2, if that matters.

Upvotes: 1

Views: 4849

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

PlotItem is not a widget, much less a QGraphicsView so you can not promote a PlotItem (if you can do it it would be great if you show it). A PlotItem is an item that belongs to the scene that QGraphicsView handles, and as you say it is the data. What you must do is promote the GraphicsLayoutWidget and add the items with code.

Example:

design.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="GraphicsLayoutWidget" name="graphicsView"/>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>GraphicsLayoutWidget</class>
   <extends>QGraphicsView</extends>
   <header>pyqtgraph</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

execute:

pyside2-uic design.ui -o design.py -x

design.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'design.ui',
# licensing of 'design.ui' applies.
#
# Created: Mon Oct  8 23:14:34 2018
#      by: pyside2-uic  running on PySide2 5.11.2
#
# WARNING! All changes made in this file will be lost!

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        self.graphicsView = GraphicsLayoutWidget(Form)
        self.graphicsView.setObjectName("graphicsView")
        self.verticalLayout.addWidget(self.graphicsView)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtWidgets.QApplication.translate("Form", "Form", None, -1))

from pyqtgraph import GraphicsLayoutWidget

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

main.py

from PySide2 import QtWidgets
from design import Ui_Form
import pyqtgraph as pg
import numpy as np


class Widget(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.setupUi(self)

        w1 = self.graphicsView.addPlot()
        w2 = self.graphicsView.addViewBox()
        w2.setAspectLocked(True)
        self.graphicsView.nextRow()
        w3 = self.graphicsView.addPlot()
        w4 = self.graphicsView.addPlot()

        n = 300
        s1 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120))
        pos = np.random.normal(size=(2,n), scale=1e-5)
        spots = [{'pos': pos[:,i], 'data': 1} for i in range(n)] + [{'pos': [0,0], 'data': 1}]
        s1.addPoints(spots)
        w1.addItem(s1)


        s2 = pg.ScatterPlotItem(size=10, pen=pg.mkPen('w'), pxMode=True)
        pos = np.random.normal(size=(2,n), scale=1e-5)
        spots = [{'pos': pos[:,i], 'data': 1, 'brush':pg.intColor(i, n), 'symbol': i%5, 'size': 5+i/10.} for i in range(n)]
        s2.addPoints(spots)
        w2.addItem(s2)

        s3 = pg.ScatterPlotItem(pxMode=False)   ## Set pxMode=False to allow spots to transform with the view
        spots3 = []
        for i in range(10):
            for j in range(10):
                spots3.append({'pos': (1e-6*i, 1e-6*j), 'size': 1e-6, 'pen': {'color': 'w', 'width': 2}, 'brush':pg.intColor(i*10+j, 100)})
        s3.addPoints(spots3)
        w3.addItem(s3)

        s4 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 20))
        pos = np.random.normal(size=(2,10000), scale=1e-9)
        s4.addPoints(x=pos[0], y=pos[1])
        w4.addItem(s4)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

Output:

enter image description here

Upvotes: 2

Related Questions