包括我的Qt GUI中的外部部件[python]
我正在学习Qt以及如何使用python创建GUI。 我设法创建我自己的Qt文件,并用按钮和其他简单的东西填充它,但现在我发现this amazing attitude indicator包括我的Qt GUI中的外部部件[python]
这个ai.py文件包含一个态度小部件,我想在我自己的GUI中导入。所以我设计与名为空部件的.ui文件“viz_widget”,然后我写了这条巨蟒文件
# -*- 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>
下面是两个代码添加AttitudeIndicator
部件所需的步骤,并通过推广Qt设计。
首先,您需要对“mainwindow.ui”文件进行一些更改。因此,在Qt设计器中,单击Object Inspector中的中央窗口小部件,然后在Property Editor中将objectName更改为central_widget
。这很重要,因为当前的名字是我们稍后需要使用的QMainWindow.centralWidget()
方法的阴影。其次,在中央小部件仍处于选中状态的情况下,单击工具栏上的水平放置(该图标有三个蓝色垂直条) - 并保存更改。
现在右键单击viz_widget
(无论是在Object Inspector或形式),选择推进到...,并在头文件进入促进类名MyAttitudeIndicator
和ai_widget
。然后点击添加和升级 - 并保存更改。完成之后,您应该在Object Inspector中看到类从QWidget
更改为MyAttitudeIndicator
。 (但是请注意,形式为的实际小部件不会显示为AttitudeIndicator
。为此,您需要编写一个custom designer plugin,这超出了本问题的范围)。
要利用这些更改,需要添加一些代码。首先,您需要为提升的窗口小部件类创建一个名为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)
的最主要的原因,这个类需要的是因为原来AttitudeIndicator
具有越野车的API。构造函数确实需要一个父窗口部件(在上面的代码中已经修复)。但是,您也可以使用此类添加您自己的自定义功能。
最后一步是对主脚本添加一些更改,如下所示。请注意,在Qt Designer中添加的所有小部件将成为传递到setupUi()
的对象的属性(即在这种情况下为主窗口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_())
非常感谢。第二个策略奇妙地工作(也许中央小部件名称使窍门)。推广看起来很有趣,但它仍然不起作用,并给我这个错误'超级(MyAttitudeIndicator,self).__ init __(hz = hz) TypeError:__init __()有一个意外的关键字参数'hz''。我不知道'hz'。但是你发现至少有一个解决方案可以工作,我接受了答案,并再次感谢你的帮助。 – marcoresk
@marcoresk。没问题。我使用了[github,它有'hz'参数]中的当前“ai.py”(https://github.com/capriele/crazyflie-clients-python-move/blob/master/build/lib.linux- i686-2.7/cfclient/UI /部件/ ai.py#L45)。我的代码已经过全面测试,所以我知道这一切正常。 – ekhumoro
我发现,在没有意识的情况下,在ai_widget中删除hz = hz,该升级似乎可行(至少它出现在GUI中)。再次感谢你。 – marcoresk
您可能只需将窗口小部件添加到窗口的布局 - 或者说,它的中心窗口部件的布局。 – ekhumoro
@ekhumoro我在QtCreator中的窗口(ui.file)中添加了一个名为viz_widget的小部件。然后,我将它连接到self.viz_widget = AttitudeIndicator()'行的对象AttitudeIndicator。我需要做更多或不同的事情吗? – marcoresk
首先:摆脱这两个'__init__'调用 - 如果您使用'super',则不需要它们。其次,删除你在QtCreator中添加的小部件,所以你只需要一个空的主窗口(并保存更改)。然后将此行添加到'__init__'的末尾:'layout = QtGui.QVBoxLayout(self.centralWidget())'。最后,将小部件添加到布局:'layout.addWidget(self.viz_widget)'。 – ekhumoro