使用 PySide2 在 Qt-Designer 中嵌入 PyQtGraph
Posted
技术标签:
【中文标题】使用 PySide2 在 Qt-Designer 中嵌入 PyQtGraph【英文标题】:Embeding PyQtGraph in Qt-Designer using PySide2 【发布时间】:2020-07-17 01:20:54 【问题描述】:我无法使用 PySide2 和 .ui 文件让 PyQtGraph 工作,我在 Qt-Designer 中将 QWidget 提升为 PlotWidget。当我使用 PyQt5 时它工作正常,但使用 PySide2,我收到以下消息:
Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using >QCoreApplication::setAttribute before constructing QGuiApplication.
"QFormBuilder was unable to create a custom widget of the class 'PlotWidget'; defaulting to base class 'QWidget'."
Traceback (most recent call last):
File "test_widget3.py", line 13, in <module>
window.graphWidget.plot([1,5,10], [1,2,7])
AttributeError: 'PySide2.QtWidgets.QWidget' object has no attribute 'plot'
代码:
import sys
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtUiTools import QUiLoader
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
app = QtWidgets.QApplication(sys.argv)
loader = QUiLoader()
window = loader.load("pyqtgraph_window.ui")
window.graphWidget.plot([1,5,10], [1,2,7])
window.show()
app.exec_()
使用 PyQt5(有效):
import sys
from PyQt5 import QtWidgets, uic
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
app = QtWidgets.QApplication(sys.argv)
window = uic.loadUi("pyqtgraph_window.ui")
window.graphWidget.plot([1,5,10], [1,2,7])
window.show()
app.exec_()
.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="PlotWidget" name="graphWidget" native="true"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>PlotWidget</class>
<extends>QWidget</extends>
<header>pyqtgraph</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
我使用来自 Github 和 PySide2 v5.12 的 PyQtGraph 的最新开发版本 如果您能提供帮助,谢谢!
【问题讨论】:
【参考方案1】:loadUi 使用名称动态访问类,该概念在 C++ 中不存在,因此 QUiLoader 具有此限制,因此解决方案是指示转换显式覆盖 createWidget 方法:
import os
import sys
from PySide2 import QtCore, QtGui, QtWidgets, QtUiTools
import pyqtgraph as pg
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
class UiLoader(QtUiTools.QUiLoader):
def createWidget(self, className, parent=None, name=""):
if className == "PlotWidget":
return pg.PlotWidget(parent=parent)
return super().createWidget(className, parent, name)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
loader = UiLoader()
window = loader.load(os.path.join(CURRENT_DIR, "pyqtgraph_window.ui"))
window.graphWidget.plot([1, 5, 10], [1, 2, 7])
window.show()
sys.exit(app.exec_())
【讨论】:
谢谢!它有效,我无法自己解决这个问题!以上是关于使用 PySide2 在 Qt-Designer 中嵌入 PyQtGraph的主要内容,如果未能解决你的问题,请参考以下文章
qt-designer使用教程1--HelloWorld.txt