我正在尝试制作最简单的Qt应用程序以使用pyqtgraph lib可视化某些数据。我已经使用Qt Designer创建了一个单窗口应用程序,并在其中放置了Graphics View小部件,并将其提升为pygtgraph。在我的应用程序(用python编写)中,我创建了测试数据集并将其绘制。此方法有效(图形正确显示),但图形不会随窗口调整大小。因此,然后在Qt Designer中,我将主窗体的布局设置为“在网格中放置”,并在预览中正常工作(“ Graphics 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预览中那样展开到全窗口)并且无法调整大小?如何解决这个问题?
答案 0 :(得分:2)
您有2个错误:
考虑到上述情况,我创建了一个.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_())