python - PyQtGraph 不调整大小

标签 python pyqt pyqt5 qt-designer pyqtgraph

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

最佳答案

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

关于python - PyQtGraph 不调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58186376/

相关文章:

python - 在 Pandas 中执行模糊字符串匹配的更快方法

Python 序列 id 到 mysql 与文本

python - 让 PyQt5 + venv + qt5ct 正常运行时遇到问题

qt - QWebEngineView,在 View 中发布KeyEvents

python - 如何通过文本在PyQt中为QTableView创建过滤器

python - 为什么 "is"关键字在这里不起作用?

python - 在 Python 中根据 Textblob 的极性获取正面和负面的单词(情感分析)

qt - 在上下文管理方面,QFile.open() 的行为与 Python 的 open() 类似吗?

python - Selenium 的应用程序没有响应 - 不显示进度条,并且在任务完成之前不在控制台中发出文本

python - QSlider handle 尺寸 PyQt5