PyQtGraph 不调整大小

PyQtGraph doesn't resize

我正在尝试制作最简单的 Qt 应用程序以使用 pyqtgraph 库可视化一些数据。我使用 Qt Designer 创建了一个 window 应用程序,将图形视图小部件放在那里,将其提升为 pygtgraph。在我的应用程序(用 python 编写)中,我创建了测试数据集并绘制了它。这有效(图表显示正确)但图表不会随 window 调整大小。那么,在 Qt Designer 中,我将主窗体的布局设置为 "Lay Out in a grid",并且在预览中它工作正常("Graphics View" 小部件随主 window 调整大小)。但是当我 运行 我的应用程序时,绘图看起来非常小,例如 5x20 像素并且无法重新调整大小。

我的应用程序:

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_())

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

我也曾尝试从我的 python 应用程序创建 pyqtgraph 图,然后将其嵌入到 Qt Designer 生成的空布局中,但结果是一样的 - 图不可调整大小。似乎它没有从主窗体继承一些属性。

所以问题是 - 为什么我的图表看起来很小(没有像在 Qt Designer 预览中那样扩展到完整 window)并且不能重新调整大小?如何解决这个问题?

您有 2 个错误:

  • 根据您提供的代码,您没有使用布局。
  • 如果您使用了 "Widget" 模板,那么您必须使用 QWidget 作为基础 class,而不是尝试使用 QMainWindow。

考虑到以上我创建了一个.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>

然后你把它转换成.py:

pyuic5 your_form.ui -o StartForm.py -x

获得以下内容:

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_())