PyQt5 MDI子窗口作为一个类

时间:2018-06-27 19:40:27

标签: python pyqt pyqt5 qt-designer qmdiarea

我有一个带有菜单和mdiArea的QMainWindow。当我单击菜单项时,我需要显示一个qDialog,例如带有一个数据列表。

我想用数据列表作为自己的类来构建QDialog。但是,我不知道如何将其作为子窗口附加到mdiArea。能否举一个小例子说明如何做到这一点?

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi

class CliDataBrowser(QDialog):
    def __init__(self,parent=None):
    super(CliDataBrowser,self).__init__(parent)
    loadUi("CliReportsUI/clidata_browser.ui",self)


class MainApplication(QMainWindow):
    def __init__(self,*args):
        super(MainApplication,self).__init__(*args)
        loadUi("CliReportsUI/clireportmain.ui",self)

    @pyqtSlot()
    def on_mnu_close_triggered(self):
        sys.exit();

    @pyqtSlot()
    def on_mnu_master_triggered(self):
        dataBrowser = CliDataBrowser(self) # <--- Need this to be a mdi subwindow
        dataBrowser.show()


app = QApplication(sys.argv)
mainWin = MainApplication()
mainWin.show()
sys.exit(app.exec_())

clireportmain.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>CliReportMain</class>
 <widget class="QMainWindow" name="CliReportMain">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1062</width>
    <height>626</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="windowTitle">
   <string>PPS Custom Client Reports</string>
  </property>
  <property name="windowIcon">
   <iconset resource="clireports.qrc">
    <normaloff>:/Images/clireports.png</normaloff>:/Images/clireports.png</iconset>
  </property>
  <widget class="QWidget" name="centralWidget">
   <property name="enabled">
    <bool>true</bool>
   </property>
   <property name="sizePolicy">
    <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <property name="autoFillBackground">
    <bool>false</bool>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
    <property name="leftMargin">
     <number>0</number>
    </property>
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="rightMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <item>
     <widget class="QMdiArea" name="mdiArea">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Plain</enum>
      </property>
      <property name="sizeAdjustPolicy">
       <enum>QAbstractScrollArea::AdjustIgnored</enum>
      </property>
      <property name="background">
       <brush brushstyle="SolidPattern">
        <color alpha="255">
         <red>20</red>
         <green>38</green>
         <blue>57</blue>
        </color>
       </brush>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1062</width>
     <height>22</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true"/>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="mnu_Close"/>
   </widget>
   <widget class="QMenu" name="menuReports">
    <property name="title">
     <string>Reports</string>
    </property>
   </widget>
   <widget class="QMenu" name="menuDatasets">
    <property name="title">
     <string>Datasets</string>
    </property>
    <addaction name="mnu_Master"/>
    <addaction name="mnu_ICD10Master"/>
    <addaction name="separator"/>
    <addaction name="mnu_ProviderMaster"/>
    <addaction name="mnu_InsuranceMaster"/>
   </widget>
   <addaction name="menuFile"/>
   <addaction name="menuReports"/>
   <addaction name="menuDatasets"/>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="mnu_Close">
   <property name="text">
    <string>Close</string>
   </property>
  </action>
  <action name="mnu_Master">
   <property name="text">
    <string>Master</string>
   </property>
  </action>
  <action name="mnu_ICD10Master">
   <property name="text">
    <string>ICD-10 Master</string>
   </property>
  </action>
  <action name="mnu_ProviderMaster">
   <property name="text">
    <string>Provider Master</string>
   </property>
  </action>
  <action name="mnu_InsuranceMaster">
   <property name="text">
    <string>Insurance Master</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="clireports.qrc"/>
 </resources>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:1)

您必须使用addSubWindow()的{​​{1}}方法:

QMdiArea