【问题标题】:PyQtGraph doesn't resizePyQtGraph 不会调整大小
【发布时间】:2020-01-30 20:08:45
【问题描述】:

我正在尝试使用 pyqtgraph lib 制作最简单的 Qt 应用程序来可视化一些数据。我使用 Qt Designer 创建了单窗口应用程序,将其放置在 Graphics View 小部件中,并将其提升为 pygtgraph。在我的应用程序(用 python 编写)中,我创建测试数据集并绘制它。这有效(图形显示正确),但图形不会随窗口重新调整大小。因此,在 Qt Designer 中,我将主窗体的布局设置为“在网格中布局”,并且在预览中它工作正常(“图形视图”小部件随主窗口调整大小)。但是当我运行我的应用程序时,绘图会显得非常小,例如 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 预览中那样扩展到全窗口)并且无法调整大小?如何解决这个问题?

【问题讨论】:

    标签: python pyqt pyqt5 qt-designer pyqtgraph


    【解决方案1】:

    您有 2 个错误:

    • 根据您提供的代码,您没有使用布局。
    • 如果您使用了“Widget”模板,那么您必须使用 QWidget 作为基类,而不是尝试使用 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_())
    

    【讨论】:

    • 非常感谢,eyllanesc!现在可以了。实际上,我已经纠正了第二个错误(基类)并且它立即起作用。我怀疑继承有问题,但没有看到。关于第一个错误(布局)-我今天早些时候已经尝试过各种组合,也有布局,但由于基类,它不起作用。无论如何,你是对的,谢谢你的快速帮助!
    猜你喜欢
    • 1970-01-01
    • 2011-05-17
    • 2011-11-14
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 2011-02-13
    • 2012-03-22
    • 2019-06-17
    相关资源
    最近更新 更多