Search code examples
pythonpyqtpyqt5qt-designerpyqtgraph

PyQtGraph doesn't resize


I'm trying to make simplest Qt app to visualize some data using pyqtgraph lib. I've created one-window app using Qt Designer, placed there Graphics View widget, promoted it to pygtgraph. In my app (written in python) I create test data set and plot it. This works (graph is displayed correctly) but the graph doesn't re-size with the window. So then, in Qt Designer I set layout of main form to "Lay Out in a grid" and in preview it works fine ("Graphics View" widget resizing with main window). But when I run my app then the plot appears very small, like 5x20 pixels and is not re-sizable.

My app:

class AppWindow(QtWidgets.QMainWindow, StartForm.Ui_StartForm):
    def __init__(self):
        super(AppWindow, self).__init__()
        self.setupUi(self)

        line1 = ([1, 3, 2, 4, 6, 5, 3])
        pl = self.graphicsView.plot(line1)  # graphicsView is Graphics View widget from Qt Designer

app = QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())

generated code by Qt Designer:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_StartForm(object):
    def setupUi(self, StartForm):
        StartForm.setObjectName("StartForm")
        StartForm.resize(1609, 1062)
        self.graphicsView = PlotWidget(StartForm)
        self.graphicsView.setGeometry(QtCore.QRect(11, 11, 1261, 931))
        self.graphicsView.setObjectName("graphicsView")

        self.retranslateUi(StartForm)
        QtCore.QMetaObject.connectSlotsByName(StartForm)

    def retranslateUi(self, StartForm):
        _translate = QtCore.QCoreApplication.translate
        StartForm.setWindowTitle(_translate("StartForm", "Form"))


from pyqtgraph import PlotWidget

I've also tried to create pyqtgraph plot from my python app and then embed it into empty layout generated by Qt Designer but the result is the same - plot is not resizable. Seems like it doesn't inherit some properties from main form.

So the question is - why my graph appears very small (doesn't expand to full window as it does in Qt Designer pre-view) and is not re-sizable? How to fix this?


Solution

  • You have 2 errors:

    • Depending on the code you provide you are not using a layout.
    • If you have used the "Widget" template then you must use QWidget as a base class, instead you try to use QMainWindow.

    Considering the above I created a .ui

    *.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>StartForm</class>
     <widget class="QWidget" name="StartForm">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>400</width>
        <height>300</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Form</string>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <widget class="PlotWidget" name="graphicsView"/>
       </item>
      </layout>
     </widget>
     <customwidgets>
      <customwidget>
       <class>PlotWidget</class>
       <extends>QGraphicsView</extends>
       <header>pyqtgraph</header>
      </customwidget>
     </customwidgets>
     <resources/>
     <connections/>
    </ui>
    

    Then you convert it to .py:

    pyuic5 your_form.ui -o StartForm.py -x
    

    obtaining the following:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_StartForm(object):
        def setupUi(self, StartForm):
            StartForm.setObjectName("StartForm")
            StartForm.resize(400, 300)
            self.verticalLayout = QtWidgets.QVBoxLayout(StartForm)
            self.verticalLayout.setObjectName("verticalLayout")
            self.graphicsView = PlotWidget(StartForm)
            self.graphicsView.setObjectName("graphicsView")
            self.verticalLayout.addWidget(self.graphicsView)
    
            self.retranslateUi(StartForm)
            QtCore.QMetaObject.connectSlotsByName(StartForm)
    
        def retranslateUi(self, StartForm):
            _translate = QtCore.QCoreApplication.translate
            StartForm.setWindowTitle(_translate("StartForm", "Form"))
    
    from pyqtgraph import PlotWidget
    
    from PyQt5 import QtWidgets
    import StartForm
    
    class AppWindow(QtWidgets.QWidget, StartForm.Ui_StartForm):
        def __init__(self):
            super(AppWindow, self).__init__()
            self.setupUi(self)
    
            line1 = ([1, 3, 2, 4, 6, 5, 3])
            pl = self.graphicsView.plot(line1)
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = AppWindow()
        w.show()
        sys.exit(app.exec_())