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

时间:2017-09-01 09:18:39

标签: python qt user-interface pyqt widget

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

1 个答案:

答案 0 :(得分:1)

以下是在代码中以及在Qt Designer中通过促销添加AttitudeIndicator小部件所需的步骤。

首先,您需要对“mainwindow.ui”文件进行一些更改。因此,在Qt设计器中,单击Object Inspector中的central-widget,然后在Property Editor中,将 objectName 更改为central_widget。这很重要,因为当前名称隐藏了我们稍后需要使用的QMainWindow.centralWidget()方法。其次,在仍然选中中央窗口小部件的情况下,单击工具栏上的水平放置(图标有三个蓝色垂直条) - 并保存更改。

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