Issue
I'm trying to embed a pyqtgraph plot in my PySide6 app but the plot is not filling the whole widget:
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?
Solution
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())
Answered By - Alexander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.