FigureCanvasQTAgg can't be added as a QWidget

47 Views Asked by At

I'm trying to make a simple Qt application using PySide6. In short, it should open the main window, take some data from QLineEdit and plot a matplotlib graph using this data. First of all I tried to make just a Qt window for the qtagg canvas:

from PySide6.QtWidgets import *
from PySide6.QtGui import *
from matplotlib import pyplot as plt
from matplotlib.backends.backend_qtagg import FigureCanvas

fig, ax =  plt.subplots(figsize=(4, 4))
if QApplication.instance():
    app = QApplication.instance()
else:
    app = QApplication(sys.argv)

class PlottingWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.fig = fig
        self.ax = ax
        self.canvas = FigureCanvas(self.fig)
        self.main_layout = QVBoxLayout()
        self.main_layout.addWidget(self.canvas)
        self.setLayout(self.main_layout)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.plotting_window = PlottingWindow()
        widget = QWidget(parent=self)
        self.main_layout = QVBoxLayout()
        widget.setLayout(self.main_layout)
        self.setCentralWidget(widget)
        
        self.start_button = QPushButton('Start')
        self.main_layout.addWidget(self.start_button)
        self.start_button.clicked.connect(self.show_plotting_window)

    def show_plotting_window(self):
        #some value checking code that is not related to the problem
        self.plotting_window.show()
        return True

main_window = MainWindow()
main_window.show()
app.exec()

However, it refuses to work:

TypeError: 'PySide6.QtWidgets.QBoxLayout.addWidget' called with wrong argument types:
  PySide6.QtWidgets.QBoxLayout.addWidget(FigureCanvasQTAgg)
Supported signatures:
  PySide6.QtWidgets.QBoxLayout.addWidget(PySide6.QtWidgets.QWidget, int = 0, PySide6.QtCore.Qt.AlignmentFlag = Default(Qt.Alignment))

I've googled and found FigureCanvas not interpreted as QtWidget after using PyInstaller topic, but installing and re-installing matplotlib and other recommendations changed nothing. I've also tried to replace backend_qtagg to backend_qt5agg (nothing changed), and to replace pyside6 to pyqt6 (the same error) and to pyside2:

This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: direct2d, minimal, offscreen, webgl, windows

I just can't understand what can be wrong.

IDE: Spyder 5. Python: Python 3.9. Qt: v.6.6.1

0

There are 0 best solutions below