PyQt5 多文档窗口界面

2019-08-14 16:18:22 浏览数 (2)

本篇介绍多文档窗口界面的写法。代码中并未实现关闭窗口前文档未保存的提醒对话框,因为之前已有介绍。

代码语言:javascript复制
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QFont, QIcon, QKeySequence

class MdiSubWindow(QMdiSubWindow):
    def __init__(self):
        super(MdiSubWindow, self).__init__()
    def closeEvent(self, event):
        self.deleteLater()  #关闭后删除
        
class MainWindow(QMainWindow):
    
    def __init__(self,parent=None):
        super(MainWindow, self).__init__(parent)
        #self.setAttribute(Qt.WA_DeleteOnClose)
        
        self.mdi=QMdiArea()  #实例化Qmidarea区域
        self.setCentralWidget(self.mdi)   #设置为中央窗口部件
        self.createFileActions()
        
        menuBar=self.menuBar()  #实例化菜单栏
        file=menuBar.addMenu('File') #添加文件菜单
        #添加子菜单
        file.addAction(self.fileNewAction)
        file.addAction(self.separator)
        file.addAction(self.exitAction)
        self.windowMenu = menuBar.addMenu("&Window")   #添加窗口菜单
        self.windowMenu.aboutToShow.connect(self.updateWindowMenu)#用于动态更新菜
        #设置主窗口的标题
        self.setWindowTitle("多重文档界面示例")
        
    def createAction(self, text,icon=None,checkable=False,slot=None,tip=None,shortcut=None):
        action = QAction(text,self)
        if icon is not None:
            action.setIcon(QIcon(icon))
        if checkable:
            action.setCheckable(True)#可切换
            if slot is not None: action.toggled.connect(slot)
        else:
            if slot is not None: action.triggered.connect(slot)
        if tip is not None:
            action.setToolTip(tip)#工具栏提示
            action.setStatusTip(tip)#状态栏提示
        if shortcut is not None:
            action.setShortcut(shortcut)#快捷键
   
        return action
    def createFileActions(self): #创建文件相关动作
        
        #动作分隔符
        self.fileNewAction = self.createAction("&New",icon="filenew.png",checkable=False,
                                                slot=self.fileNew,tip="新建文件",shortcut=QKeySequence.New)
        self.separator  = QAction(self)
        self.separator.setSeparator(True)
        self.exitAction = self.createAction("E&xit",icon="",checkable=False,
                                                slot=self.close,tip="退出",shortcut=QKeySequence.Close)
    def fileNew(self):
        window =MdiSubWindow()   #实例化多文档界面对象
        window.setWidget(QTextEdit())   #设置sub内部部件
        window.setWindowTitle('subWindow %d' % len(self.mdi.subWindowList()))#设置新建子窗口的标题
        #print(sub.windowTitle())
        self.mdi.addSubWindow(window) #将子窗口添加到Mdi区域
        window.show()  #子窗口显示
        
    def updateWindowMenu(self):#动态显示窗口菜单
        self.windowMenu.clear() # 先清空已有的菜单项
        self.windowMenu.addAction('cascade')
        self.windowMenu.addAction('Tiled')
        self.windowMenu.addAction("Previous Window")
        self.windowMenu.addAction("Next Window")
        self.windowMenu.addSeparator()
        
        for window in self.mdi.subWindowList():
            action = self.windowMenu.addAction(window.windowTitle())
            action.setData(window)# 关联窗口和action
            
        self.windowMenu.addSeparator()
        self.windowMenu.addAction("close Active Window")
        self.windowMenu.addAction("close All Windows")
        
        #点击QAction绑定自定义的槽函数(传递有值【QAction】)
        self.windowMenu.triggered[QAction].connect(self.windowAction)
        
    def windowAction(self,q):
        if q.text()=='cascade':
            self.mdi.cascadeSubWindows()  #层叠显示
        elif q.text()=='Tiled':
            self.mdi.tileSubWindows()  #平铺显示
        elif q.text() == "Previous Window":
            self.mdi.activatePreviousSubWindow()
        elif q.text() == "Next Window":
            self.mdi.activateNextSubWindow()
        elif q.text() == "close Active Window":
            self.mdi.closeActiveSubWindow()   
        elif q.text() == "close All Windows":
            self.mdi.closeAllSubWindows()
        else: #激活窗口
            self.mdi.setActiveSubWindow(q.data())
        '''
        currentSubWindow(...)00
        setDocumentMode(self, bool)
        activateNextSubWindow(...)
        activatePreviousSubWindow(...)
        activeSubWindow(...)
        closeAllSubWindows(...)
        closeActiveSubWindow(...)
        removeSubWindow(self, QWidget)
        setActiveSubWindow(self, QMdiSubWindow)
        subWindowList(...)
        '''
        
if __name__ == '__main__':
    app=QApplication(sys.argv)
    mdi=MainWindow()
    mdi.show()
    sys.exit(app.exec_())

0 人点赞