PySide6 - Promoting QWidget to MplWidget using QtDesigner not working

Hello everyone,

I am trying to implement a simple program where I can track the evolution of the chemical composition of my calculations. With PyQT5, a similar workflow worked out back in the day. However, I’m using PySide6 for my new “project”, and it’s not working out. The problem is that the MplWidget class is not being called while initializing the view, so the QWidget stays empty, and no axis will be displayed. The „widget_evolution“ is still a QWidget, not the self-defined one.

Firstly, I’m defining the QWidget via the QtDesigner, and I am promoting it to the self-defined MplWidget class (I already tried to use just mplwidget but also mplwidget.py or mplwidget.h as the header filename).

The UI file contains necessary information about the MplWidget:

         <widget class="MplWidget" name="widget_evolution" native="true">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="styleSheet">
           <string notr="true">background-color: rgb(174, 174, 174);</string>
          </property>
         </widget>

and

 <customwidgets>
  <customwidget>
   <class>MplWidget</class>
   <extends>QWidget</extends>
   <header>mplwidget</header>
   <container>1</container>
  </customwidget>
 </customwidgets>

All files (mplwidget.py, post_processing.py and post-processing.ui) are located in the same folder.
The MplWidget class is defined inside the mplwidget.py and contains following code:

from PySide6.QtWidgets import QWidget, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT


class NavigationToolbar(NavigationToolbar2QT):
    # only display the buttons we need
    toolitems = [t for t in NavigationToolbar2QT.toolitems if
                 t[0] in ('Home', 'Back', 'Forward', 'Zoom', 'Pan','Save')]


class MplWidget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.canvas = FigureCanvasQTAgg(Figure())
        self.canvas.axes = self.canvas.figure.add_subplot(111)
        self.toolbar = NavigationToolbar(self.canvas, self)
        vertical_layout = QVBoxLayout()
        vertical_layout.addWidget(self.toolbar)
        vertical_layout.addWidget(self.canvas)
        self.setLayout(vertical_layout)

Finally the UI file has been loaded by the PostProcessingUI module:

class PostProcessingUI(QMainWindow):
    def __init__(self):
        super().__init__()

        loader = QUiLoader()
        self.ui = loader.load(
            os.path.join(basedir, "post-processing.ui"), None
        )

Every help would be appreciated. Many thanks in advance!

Hi @kavd welcome to the forum!

When you say you’re using mplwidget.py or mplwidget.h as the header filename are you including the .py or .h extension? The name provided her is used for a Python import statement, so you need to omit the extension & ensure the file is in a location where this import will then work.

If you compile the .ui file saved from Qt Designer with pyuic it will generate a .py file for you. In this file you will be able to see the import it is using to load the promoted widget (the import is usually at the bottom of the file, confusingly).

Edit: I see from the subsequent UI file that you’re not adding the suffix. In that case, I would still suggest trying to compile the .ui file and taking a look at what is generated. It’s usually easier to debug while looking at the code.

If you can post the compiled output I’ll take a look.