我正在学习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(在“对象检查器”中或在窗体上),选择“升级为...”,然后在“升级的类名”中输入MyAttitudeIndicator,在“头文件”中输入ai_widget。然后点击添加并升级-并保存更改。之后,您应该在对象检查器中看到类从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_())

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

10-12 16:54