2

I'm trying to embed a pyqtgraph plot in my PySide6 app but the plot is not filling the whole widget:

enter image description here

I want the whole PlotWidget to be filled. The PlotWidget got created with QT-Designer.

The App:

import sys
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QMainWindow
import pyqtgraph


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.window = QUiLoader().load("test.ui", self)
        self.plot_widget = pyqtgraph.PlotWidget(parent=self.window.graphicsView)
        self.plot_widget.plot([1, 2, 3], [1, 2, 3])
        self.window.show()


if __name__ == "__main__":
    App = QApplication(sys.argv)
    MW = MainWindow()
    sys.exit(App.exec())

The test.ui file:

<?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="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="PlotWidget" name="graphicsView"/>
    </item>
   </layout>
  </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>QGraphicsView</extends>
   <header>pyqtgraph</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

So how can I make the plot fill the whole widget?

jamesB
  • 291
  • 2
  • 12

2 Answers2

2

There is no need for the UI file.

You will get the desired result by simply creating the plotWidget and setting it as the centralWidget for the mainWindow.

import sys
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QMainWindow
import pyqtgraph


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.plot_widget = pyqtgraph.PlotWidget()
        self.plot_widget.plot([1, 2, 3], [1, 2, 3])
        self.setCentralWidget(self.plot_widget)


if __name__ == "__main__":
    App = QApplication(sys.argv)
    MW = MainWindow()
    MW.show()
    sys.exit(App.exec())

I am not very familiar with using UI files with pyqtgraph, but it appears to me that the UI file is automatically generating a QGraphicsView right were you want to put your PlotWidget. So one way of making it work would be to deleting the QGraphicsView and replacing it in the layout with your PlotWidget.

import sys
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QMainWindow
import pyqtgraph


class WindowUi:
    def __init__(self):
        self.window = QUiLoader().load("test.ui")
        layout = self.window.verticalLayout
        item = layout.takeAt(0)
        item.widget().deleteLater()
        self.plot_widget = pyqtgraph.PlotWidget(parent=self.window.graphicsView)
        layout.addWidget(self.plot_widget)
        self.plot_widget.plot([1, 2, 3], [1, 2, 3])
        self.window.show()


if __name__ == "__main__":
    App = QApplication(sys.argv)
    MW = WindowUi()
    sys.exit(App.exec())
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • I included the UI file because I use it in my app. The code I provided is just a minimal example. Is there a way to make it work using the UI file? – jamesB Jan 21 '23 at 03:51
  • 1
    @Alexander But that new code would actually create *another* window, thus making `MainWindow` quite useless. – musicamante Jan 21 '23 at 18:02
  • @musicamante I don't know what you mean? which example are you referring to? – Alexander Jan 21 '23 at 21:14
  • @Alexander In your second example you're creating a `MainWindow` instance, but you're not really using it, since what is actually being shown is the one created by QUiLoader. That QMainWindow is useless right now. – musicamante Jan 21 '23 at 21:19
  • Oh... good point. I didn't even consider that when I copied the code. @musicamante I will fix – Alexander Jan 21 '23 at 21:23
  • 1
    @jamesB You may want to take a look at the alterations I made to the second example. There is no need to subclass QMainWindow since the Uiloader already creates one for you. – Alexander Jan 21 '23 at 21:32
1

Use PyQtGraph's helper method to load your .ui file instead (per this PythonGUIs tutorial); this will prevent the qt.pysideplugin warnings at startup as PyQtGraph widgets are handled properly and renders the plot in the whole widget as expected.

import sys
from PySide6.QtWidgets import QApplication
import pyqtgraph


class MainWindow(*pyqtgraph.Qt.loadUiType("test.ui")):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.graphicsView.plot([1, 2, 3], [1, 2, 3])
        self.show()


if __name__ == "__main__":
    App = QApplication(sys.argv)
    MW = MainWindow()
    sys.exit(App.exec())

PyQtGraph plot rendering as expected

TACD
  • 33
  • 6