在我的 Qt GUI [python] 中包含外部小部件 [英] including external widget in my Qt GUI [python]

查看:33
本文介绍了在我的 Qt GUI [python] 中包含外部小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Qt 以及如何使用 Python 创建 GUI.我设法创建了自己的 Qt 文件并用按钮和其他简单的东西填充它,但现在我发现 这个惊人的姿态指示器

I am learning Qt and how to create GUIs with python. I managed to create my own Qt files and fill it with buttons and other simple things, but now I found this amazing attitude indicator

这个 ai.py 文件包含一个姿态小部件,我想将它导入到我自己的 GUI 中.所以我用一个名为viz_widget"的空小部件设计了我的 .ui 文件,然后我写了这个 python 文件

This ai.py file contains an attitude widget that I would like to import in my own GUI. So I designed my .ui file with an empty widget named "viz_widget", then I wrote this python file

# -*- 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 中显示姿态小部件.是否可以导入小部件?我的错误是什么?

The GUI is launched, there aren't any errors, but I can not display the attitude widget into my GUI. Is it possible to import the widget? What is my error?

提前致谢

这是文件 maiwindow.ui

this is the file 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 小部件所需的步骤.

Here's the steps needed to add an AttitudeIndicator widget both in code, and via promotion in Qt Designer.

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

Firstly, you will need to make some changes to the "mainwindow.ui" file. So, in Qt designer, click on the central-widget in the Object Inspector, and then in the Property Editor, change the objectName to central_widget. This is important, because the current name is shadowing the QMainWindow.centralWidget() method that we need to use later. Secondly, with the central-widget still selected, click Lay Out Horizontally on the toolbar (the icon has three blue vertical bars) - and save the changes.

现在右键单击 viz_widget(在对象检查器中或表单上),选择 Promote to ...,然后输入 MyAttitudeIndicatorPromoted class name 中的 code>,头文件中的 ai_widget.然后点击 AddPromote - 并保存更改.执行此操作后,您应该会在 Object Inspector 中看到 ClassQWidget 更改为 MyAttitudeIndicator.(但请注意,表单上的实际小部件不会显示为 AttitudeIndicator.为此,您需要编写一个 自定义设计器插件,这超出了这个问题的范围).

Now right-click the viz_widget (either in the Object Inspector or on the form), choose Promote to ..., and enter MyAttitudeIndicator in Promoted class name, and ai_widget in Header file. Then click Add and Promote - and save the changes. After doing that, you should see the Class change from QWidget to MyAttitudeIndicator in the Object Inspector. (But note that the actual widget on the form will not be displayed as an AttitudeIndicator. To do that, you'd need to write a custom designer plugin, which goes way beyond the scope of this question).

要利用这些更改,需要添加一些代码.首先,您需要为提升的小部件类创建一个名为 ai_widget.py 的模块.该模块应包含以下代码:

To make use of these changes, some code needs to be added. Firstly, you need to create a module called ai_widget.py for the promoted widget class. The module should contain this code:

from ai import AttitudeIndicator

class MyAttitudeIndicator(AttitudeIndicator):
    def __init__(self, parent, hz=30):
        super(MyAttitudeIndicator, self).__init__(hz=hz)
        self.setParent(parent)

需要这个类的主要原因是原来的AttitudeIndicator有一个有问题的API.构造函数确实需要采用父小部件(在上面的代码中已修复).但是,您也可以使用此类来添加您自己的自定义功能.

The main reason why this class is needed is because the original AttitudeIndicator has a buggy API. The constructor really needs to take a parent widget (which is fixed in the above code). However, you could also use this class to add your own custom features.

最后一步是向主脚本添加一些更改,如下所示.请注意,Qt Designer 中添加的所有小部件都将成为传递给setupUi() 的对象的属性(即主窗口,在本例中为self).

The final step is to add some changes to the main script, which is shown below. Note that all the widgets added in Qt Designer will become attributes of the object passed to setupUi() (i.e. the main-window, self, in this case).

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 GUI [python] 中包含外部小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆