gpt4 book ai didi

python - 在我的 Qt GUI 中包含外部小部件 [python]

转载 作者:太空宇宙 更新时间:2023-11-03 14:49:18 26 4
gpt4 key购买 nike

我正在学习 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>

最佳答案

以下是添加 AttitudeIndicator 所需的步骤小部件既可以在代码中使用,也可以通过 Qt Designer 中的升级来实现。

首先,您需要对“mainwindow.ui”文件进行一些更改。因此,在 Qt 设计器中,单击对象检查器中的中央小部件,然后在属性编辑器中,将 objectName 更改为 central_widget 。这很重要,因为当前名称遮盖了 QMainWindow.centralWidget()我们后面需要用到的方法。其次,在仍选择中央小部件的情况下,单击工具栏上的水平布局(该图标具有三个蓝色垂直条) - 并保存更改。

现在右键单击 viz_widget (在对象检查器中或在表单上),选择升级到...,然后输入 MyAttitudeIndicator升级类名称中,以及 ai_widget头文件中。然后点击添加升级 - 并保存更改。完成此操作后,您应该会看到 ClassQWidget 更改至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_())

关于python - 在我的 Qt GUI 中包含外部小部件 [python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45996930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com