Question

I am trying to embed a PySide PyQtGraph widget into a GraphicsView window created with QT Creator. However, a segmentation error occurs when I import the UI file and use the "promote to" functionality in QT Creator.

This error has been observed previously and a work-around to override the QUiLoader's createWidget() has been proposed:

http://www.mail-archive.com/pyside@qt-project.org/msg00306.html
and
Calling custom class in .ui file fails

However, when I implement the workaround I still end up with a segmentation error.

Does anyone know why this workaround does not seem to work in my code? Or is there another way to embed a PyQtGraph while importing a UI file dynamically?

Many thanks,

Taco

Some example code:

#!/usr/bin/python
import os
import sys

from PySide.QtUiTools import QUiLoader


import pyqtgraph as pg

SCRIPT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))

class UiLoader(QUiLoader):
    def __init__(self, baseinstance):
        QUiLoader.__init__(self, baseinstance)
        self.baseinstance = baseinstance

    def createWidget(self, className, parent = None, name = ""):
        if className in QUiLoader.availableWidgets(self):
            widget = QUiLoader.createWidget(self, className, parent, name)
        else:
            if hasattr(self.baseinstance, "customWidgets"):
                if className in self.baseinstance.customWidgets.keys():
                    widget = self.baseinstance.customWidgets[className](parent)
                else:
                    raise KeyError("Unknown widget '%s'" % className)
            else:
                raise AttributeError("Trying to load custom widget '%s', but base instance '%s' does not specify custom widgets." % (className, repr(self.baseinstance)))

        if self.baseinstance is not None:
            setattr(self.baseinstance, name, widget)

        return widget


def loadUi(uifile, baseinstance=None):
    loader = UiLoader(baseinstance)
    loader.registerCustomWidget(pg.PlotWidget)
    widget = loader.load(uifile)
    QMetaObject.connectSlotsByName(widget)
    return widget

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.ui = loadUi(os.path.join(SCRIPT_DIRECTORY, 'example1.ui'), self)
        self.ui.plotBtn.clicked.connect(self.PlotTest)

    def PlotTest(self):
        self.ui.plottest.plot(np.random.normal(size=50), clear=True)

def main():
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

And the UI file: example1.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="PlotWidget" name="centralwidget">
   <widget class="QGraphicsView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>30</y>
      <width>761</width>
      <height>471</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="plotBtn">
    <property name="geometry">
     <rect>
      <x>670</x>
      <y>510</y>
      <width>114</width>
      <height>32</height>
     </rect>
    </property>
    <property name="text">
     <string>Plot</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PlotWidget</class>
   <extends>QWidget</extends>
   <header>pyqtgraph</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>
Was it helpful?

Solution

You can circumvent PySides issues with QtUiLoader by using pg.Qt.loadUiType('uicfile.ui'). Here is an example based on your code:

from PySide.QtGui import *
import pyqtgraph as pg
import numpy as np

formClass, baseClass = pg.Qt.loadUiType('example1.ui')
class MainWindow(baseClass):
    def __init__(self, parent=None):
        baseClass.__init__(self, parent)
        self.ui = formClass()
        self.ui.setupUi(self)
        self.setCentralWidget(self.ui.centralwidget)
        self.ui.plotBtn.clicked.connect(self.PlotTest)

    def PlotTest(self):
        self.ui.centralwidget.plot(np.random.normal(size=50), clear=True)

def main():
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

Notes:

  • Your .ui file seems to be built improperly--you promoted the 'centralwidget' to be a PlotWidget, whereas I think you intended the GraphicsView to be promoted instead.

  • I ran into some unicode issues with loadUiType and had to use the latest pyqtgraph development version. You might need to do the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top