Home > Software design >  pyqtgraph plot not filling whole widget (from qt designer)
pyqtgraph plot not filling whole widget (from qt designer)

Time:01-21

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  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  name="centralwidget">
   <layout  name="verticalLayout">
    <item>
     <widget  name="graphicsView"/>
    </item>
   </layout>
  </widget>
  <widget  name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget  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?

CodePudding user response:

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 MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.window = QUiLoader().load("test.ui", self)
        layout = self.window.verticalLayout  # get the ui layout
        item = layout.takeAt(0)              # remove the graphicsView
        item.widget().deleteLater()          # delete the graphicsView
        self.plot_widget = pyqtgraph.PlotWidget(parent=self.window.graphicsView) 
        layout.addWidget(self.plot_widget)    # add PlotWidget to layout
        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())
  • Related