在我的 Qt GUI 中包含外部小部件 [python]
including external widget in my Qt GUI [python]
我正在学习 Qt 以及如何使用 python 创建 GUI。
我设法创建了自己的 Qt 文件并用按钮和其他简单的东西填充它,但现在我发现 this amazing attitude indicator
此 ai.py 文件包含一个姿态小部件,我想将其导入到我自己的 GUI 中。所以我用一个名为 "viz_widget" 的空小部件设计了我的 .ui 文件,然后我写了这个 python 文件
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui, uic
from ai import AttitudeIndicator
qtCreatorFile1 = "mainwindow.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile1)
class OperatorGUI(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(OperatorGUI, self).__init__(parent)
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.viz_widget = AttitudeIndicator()
self.viz_widget.setPitch(10)
self.viz_widget.setRoll(20)
self.viz_widget.setHover(500/10.)
self.viz_widget.setBaro(500/10.)
self.viz_widget.update()
# Key press functions
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Q: #Q: close the window
print "pressed Q: exit by keyboard"
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = OperatorGUI()
window.show()
sys.exit(app.exec_())
GUI 已启动,没有任何错误,但我无法在我的 GUI 中显示姿态小部件。是否可以导入小部件?我的错误是什么?
提前致谢
编辑: 这是文件 maiwindow.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">
<widget class="QWidget" name="viz_widget" native="true">
<property name="geometry">
<rect>
<x>50</x>
<y>40</y>
<width>671</width>
<height>441</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
以下是在代码中以及通过 Qt Designer 中的提升添加 AttitudeIndicator
小部件所需的步骤。
首先,您需要对 "mainwindow.ui" 文件进行一些更改。因此,在 Qt 设计器中,单击对象检查器中的中央小部件,然后在 属性 编辑器中,将 objectName 更改为 central_widget
。这很重要,因为当前名称正在遮盖我们稍后需要使用的 QMainWindow.centralWidget()
方法。其次,在仍选中中央小部件的情况下,单击工具栏上的 水平布局(图标具有三个蓝色垂直条)- 并保存更改。
现在右键单击 viz_widget
(在对象检查器中或在表单中),选择 Promote to ...,然后输入 MyAttitudeIndicator
在 提升的 class 名称 和 ai_widget
在 头文件 中。然后单击 添加 和 提升 - 并保存更改。这样做之后,您应该会在对象检查器中看到 Class 从 QWidget
更改为 MyAttitudeIndicator
。 (但请注意,表单 上的实际小部件不会 显示为 AttitudeIndicator
。为此,您需要编写一个 custom designer plugin,远远超出了这个问题的范围)。
要利用这些更改,需要添加一些代码。首先,您需要为提升的小部件 class 创建一个名为 ai_widget.py
的模块。该模块应包含此代码:
from ai import AttitudeIndicator
class MyAttitudeIndicator(AttitudeIndicator):
def __init__(self, parent, hz=30):
super(MyAttitudeIndicator, self).__init__(hz=hz)
self.setParent(parent)
需要这个class的主要原因是因为原来的AttitudeIndicator
有一个bugAPI。构造函数确实需要带一个父窗口小部件(在上面的代码中已修复)。但是,您也可以使用此 class 添加您自己的自定义功能。
最后一步是对主脚本添加一些更改,如下所示。请注意,在 Qt Designer 中添加的所有小部件都将成为传递给 setupUi()
的对象的属性(即在本例中为 main-window、self
)。
import sys
from PyQt4 import QtCore, QtGui, uic
from ai import AttitudeIndicator
qtCreatorFile1 = "mainwindow.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile1)
class OperatorGUI(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(OperatorGUI, self).__init__(parent)
self.setupUi(self)
# promoted widget from qt designer
self.viz_widget.setPitch(10)
self.viz_widget.setRoll(20)
self.viz_widget.setHover(500/10.)
self.viz_widget.setBaro(500/10.)
# optional second widget
self.viz_widget2 = AttitudeIndicator()
self.viz_widget2.setPitch(10)
self.viz_widget2.setRoll(-40)
layout = self.centralWidget().layout()
layout.addWidget(self.viz_widget2)
# Key press functions
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Q: #Q: close the window
print "pressed Q: exit by keyboard"
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = OperatorGUI()
window.show()
sys.exit(app.exec_())
我正在学习 Qt 以及如何使用 python 创建 GUI。 我设法创建了自己的 Qt 文件并用按钮和其他简单的东西填充它,但现在我发现 this amazing attitude indicator
此 ai.py 文件包含一个姿态小部件,我想将其导入到我自己的 GUI 中。所以我用一个名为 "viz_widget" 的空小部件设计了我的 .ui 文件,然后我写了这个 python 文件
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui, uic
from ai import AttitudeIndicator
qtCreatorFile1 = "mainwindow.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile1)
class OperatorGUI(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(OperatorGUI, self).__init__(parent)
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.viz_widget = AttitudeIndicator()
self.viz_widget.setPitch(10)
self.viz_widget.setRoll(20)
self.viz_widget.setHover(500/10.)
self.viz_widget.setBaro(500/10.)
self.viz_widget.update()
# Key press functions
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Q: #Q: close the window
print "pressed Q: exit by keyboard"
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = OperatorGUI()
window.show()
sys.exit(app.exec_())
GUI 已启动,没有任何错误,但我无法在我的 GUI 中显示姿态小部件。是否可以导入小部件?我的错误是什么?
提前致谢
编辑: 这是文件 maiwindow.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">
<widget class="QWidget" name="viz_widget" native="true">
<property name="geometry">
<rect>
<x>50</x>
<y>40</y>
<width>671</width>
<height>441</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
以下是在代码中以及通过 Qt Designer 中的提升添加 AttitudeIndicator
小部件所需的步骤。
首先,您需要对 "mainwindow.ui" 文件进行一些更改。因此,在 Qt 设计器中,单击对象检查器中的中央小部件,然后在 属性 编辑器中,将 objectName 更改为 central_widget
。这很重要,因为当前名称正在遮盖我们稍后需要使用的 QMainWindow.centralWidget()
方法。其次,在仍选中中央小部件的情况下,单击工具栏上的 水平布局(图标具有三个蓝色垂直条)- 并保存更改。
现在右键单击 viz_widget
(在对象检查器中或在表单中),选择 Promote to ...,然后输入 MyAttitudeIndicator
在 提升的 class 名称 和 ai_widget
在 头文件 中。然后单击 添加 和 提升 - 并保存更改。这样做之后,您应该会在对象检查器中看到 Class 从 QWidget
更改为 MyAttitudeIndicator
。 (但请注意,表单 上的实际小部件不会 显示为 AttitudeIndicator
。为此,您需要编写一个 custom designer plugin,远远超出了这个问题的范围)。
要利用这些更改,需要添加一些代码。首先,您需要为提升的小部件 class 创建一个名为 ai_widget.py
的模块。该模块应包含此代码:
from ai import AttitudeIndicator
class MyAttitudeIndicator(AttitudeIndicator):
def __init__(self, parent, hz=30):
super(MyAttitudeIndicator, self).__init__(hz=hz)
self.setParent(parent)
需要这个class的主要原因是因为原来的AttitudeIndicator
有一个bugAPI。构造函数确实需要带一个父窗口小部件(在上面的代码中已修复)。但是,您也可以使用此 class 添加您自己的自定义功能。
最后一步是对主脚本添加一些更改,如下所示。请注意,在 Qt Designer 中添加的所有小部件都将成为传递给 setupUi()
的对象的属性(即在本例中为 main-window、self
)。
import sys
from PyQt4 import QtCore, QtGui, uic
from ai import AttitudeIndicator
qtCreatorFile1 = "mainwindow.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile1)
class OperatorGUI(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(OperatorGUI, self).__init__(parent)
self.setupUi(self)
# promoted widget from qt designer
self.viz_widget.setPitch(10)
self.viz_widget.setRoll(20)
self.viz_widget.setHover(500/10.)
self.viz_widget.setBaro(500/10.)
# optional second widget
self.viz_widget2 = AttitudeIndicator()
self.viz_widget2.setPitch(10)
self.viz_widget2.setRoll(-40)
layout = self.centralWidget().layout()
layout.addWidget(self.viz_widget2)
# Key press functions
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Q: #Q: close the window
print "pressed Q: exit by keyboard"
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = OperatorGUI()
window.show()
sys.exit(app.exec_())