导航

Qt Designer设计界面二,应用树结构和MDI区域

Posted on 2022-03-07 17:37  初之萌萌  阅读(808)  评论(0编辑  收藏  举报

功能:

1.添加,删除,修改分组、节点

2.按钮来显示/隐藏界面某部分区域

3.MDI区域显示子窗口

 

效果图:

 

代码:

# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'display.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(880, 542)
        self.horizontalLayout_3 = QHBoxLayout(Form)
        self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
        self.horizontalLayout_2 = QHBoxLayout()
        self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
        self.groupBox = QGroupBox(Form)
        self.groupBox.setObjectName(u"groupBox")
        sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
        self.groupBox.setSizePolicy(sizePolicy)
        self.verticalLayout = QVBoxLayout(self.groupBox)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.treeWidget = QTreeWidget(self.groupBox)
        __qtreewidgetitem = QTreeWidgetItem(self.treeWidget)
        __qtreewidgetitem.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEditable|Qt.ItemIsDragEnabled|Qt.ItemIsDropEnabled|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled);
        self.treeWidget.setObjectName(u"treeWidget")
        sizePolicy1 = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
        sizePolicy1.setHorizontalStretch(0)
        sizePolicy1.setVerticalStretch(0)
        sizePolicy1.setHeightForWidth(self.treeWidget.sizePolicy().hasHeightForWidth())
        self.treeWidget.setSizePolicy(sizePolicy1)

        self.verticalLayout.addWidget(self.treeWidget)

        self.verticalSpacer_2 = QSpacerItem(20, 10, QSizePolicy.Minimum, QSizePolicy.Fixed)

        self.verticalLayout.addItem(self.verticalSpacer_2)

        self.verticalLayout_5 = QVBoxLayout()
        self.verticalLayout_5.setObjectName(u"verticalLayout_5")
        self.verticalLayout_2 = QVBoxLayout()
        self.verticalLayout_2.setObjectName(u"verticalLayout_2")
        self.horizontalLayout_4 = QHBoxLayout()
        self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
        self.label = QLabel(self.groupBox)
        self.label.setObjectName(u"label")

        self.horizontalLayout_4.addWidget(self.label)

        self.ip_lineEdit = QLineEdit(self.groupBox)
        self.ip_lineEdit.setObjectName(u"ip_lineEdit")

        self.horizontalLayout_4.addWidget(self.ip_lineEdit)


        self.verticalLayout_2.addLayout(self.horizontalLayout_4)

        self.horizontalLayout_5 = QHBoxLayout()
        self.horizontalLayout_5.setObjectName(u"horizontalLayout_5")
        self.add_group_btn = QPushButton(self.groupBox)
        self.add_group_btn.setObjectName(u"add_group_btn")

        self.horizontalLayout_5.addWidget(self.add_group_btn)

        self.add_btn = QPushButton(self.groupBox)
        self.add_btn.setObjectName(u"add_btn")

        self.horizontalLayout_5.addWidget(self.add_btn)

        self.del_btn = QPushButton(self.groupBox)
        self.del_btn.setObjectName(u"del_btn")

        self.horizontalLayout_5.addWidget(self.del_btn)


        self.verticalLayout_2.addLayout(self.horizontalLayout_5)


        self.verticalLayout_5.addLayout(self.verticalLayout_2)

        self.verticalSpacer = QSpacerItem(20, 10, QSizePolicy.Minimum, QSizePolicy.Fixed)

        self.verticalLayout_5.addItem(self.verticalSpacer)

        self.groupBox_2 = QGroupBox(self.groupBox)
        self.groupBox_2.setObjectName(u"groupBox_2")
        self.verticalLayout_7 = QVBoxLayout(self.groupBox_2)
        self.verticalLayout_7.setObjectName(u"verticalLayout_7")
        self.horizontalLayout_6 = QHBoxLayout()
        self.horizontalLayout_6.setObjectName(u"horizontalLayout_6")
        self.tile_window_btn = QPushButton(self.groupBox_2)
        self.tile_window_btn.setObjectName(u"tile_window_btn")

        self.horizontalLayout_6.addWidget(self.tile_window_btn)

        self.close_all_win_btn = QPushButton(self.groupBox_2)
        self.close_all_win_btn.setObjectName(u"close_all_win_btn")

        self.horizontalLayout_6.addWidget(self.close_all_win_btn)


        self.verticalLayout_7.addLayout(self.horizontalLayout_6)


        self.verticalLayout_5.addWidget(self.groupBox_2)


        self.verticalLayout.addLayout(self.verticalLayout_5)


        self.horizontalLayout_2.addWidget(self.groupBox)

        self.display_btn = QPushButton(Form)
        self.display_btn.setObjectName(u"display_btn")
        sizePolicy2 = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
        sizePolicy2.setHorizontalStretch(0)
        sizePolicy2.setVerticalStretch(1)
        sizePolicy2.setHeightForWidth(self.display_btn.sizePolicy().hasHeightForWidth())
        self.display_btn.setSizePolicy(sizePolicy2)
        self.display_btn.setMinimumSize(QSize(0, 0))
        self.display_btn.setMaximumSize(QSize(20, 16777215))
        self.display_btn.setAutoRepeatInterval(100)
        self.display_btn.setAutoDefault(False)

        self.horizontalLayout_2.addWidget(self.display_btn)

        self.verticalLayout_6 = QVBoxLayout()
        self.verticalLayout_6.setObjectName(u"verticalLayout_6")
        self.mdiArea = QMdiArea(Form)
        self.mdiArea.setObjectName(u"mdiArea")
        self.mdiArea.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)

        self.verticalLayout_6.addWidget(self.mdiArea)


        self.horizontalLayout_2.addLayout(self.verticalLayout_6)

        self.horizontalLayout_2.setStretch(0, 1)
        self.horizontalLayout_2.setStretch(2, 5)

        self.horizontalLayout_3.addLayout(self.horizontalLayout_2)


        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
        self.groupBox.setTitle("")
        ___qtreewidgetitem = self.treeWidget.headerItem()
        ___qtreewidgetitem.setText(0, QCoreApplication.translate("Form", u"\u9879\u76ee", None));

        __sortingEnabled = self.treeWidget.isSortingEnabled()
        self.treeWidget.setSortingEnabled(False)
        ___qtreewidgetitem1 = self.treeWidget.topLevelItem(0)
        ___qtreewidgetitem1.setText(0, QCoreApplication.translate("Form", u"\u9ed8\u8ba4\u5206\u7ec4", None));
        self.treeWidget.setSortingEnabled(__sortingEnabled)

        self.label.setText(QCoreApplication.translate("Form", u"\u9879\u76ee\u540d\u79f0\uff1a", None))
        self.ip_lineEdit.setText(QCoreApplication.translate("Form", u"\u8282\u70b91", None))
        self.add_group_btn.setText(QCoreApplication.translate("Form", u"\u65b0\u589e\u5206\u7ec4", None))
        self.add_btn.setText(QCoreApplication.translate("Form", u"\u65b0\u589e\u8282\u70b9", None))
        self.del_btn.setText(QCoreApplication.translate("Form", u"\u5220\u9664\u5206\u7ec4/\u8282\u70b9", None))
        self.groupBox_2.setTitle(QCoreApplication.translate("Form", u"\u7a97\u53e3\u64cd\u4f5c\uff1a", None))
        self.tile_window_btn.setText(QCoreApplication.translate("Form", u"\u5e73\u94fa\u7a97\u53e3", None))
        self.close_all_win_btn.setText(QCoreApplication.translate("Form", u"\u5173\u95ed\u5168\u90e8\u7a97\u53e3", None))
        self.display_btn.setText(QCoreApplication.translate("Form", u"<<", None))
    # retranslateUi

 

# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'video.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if not Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(400, 300)
        self.verticalLayout_2 = QVBoxLayout(Dialog)
        self.verticalLayout_2.setSpacing(0)
        self.verticalLayout_2.setObjectName(u"verticalLayout_2")
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout = QVBoxLayout()
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.video_lbl = QLabel(Dialog)
        self.video_lbl.setObjectName(u"video_lbl")

        self.verticalLayout.addWidget(self.video_lbl)


        self.verticalLayout_2.addLayout(self.verticalLayout)


        self.retranslateUi(Dialog)

        QMetaObject.connectSlotsByName(Dialog)
    # setupUi

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"\u89c6\u9891\u5bf9\u8bdd\u6846", None))
        self.video_lbl.setText("")
    # retranslateUi

 

 

import sys
from threading import Thread
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (QApplication, QWidget, QTreeWidgetItem, QMdiSubWindow, QDialog)
from display import Ui_Form
from video import Ui_Dialog

class video_dialog(QDialog, QMdiSubWindow):
    '''
    功能:子窗口
    '''
    def __init__(self):
        super(video_dialog, self).__init__()
        self.video_dialog = Ui_Dialog()
        self.video_dialog.setupUi(self)
        self.video_dialog.video_lbl.setFixedWidth(220)
        self.video_dialog.video_lbl.setFixedHeight(220)
        self.video_dialog.video_lbl.setScaledContents(True)


class UI(QWidget):

    def __init__(self):
        super(UI, self).__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.display_btn.clicked.connect(self.display_area)
        self.ui.treeWidget.clicked.connect(self.on_tree_clicked)
        self.ui.add_btn.clicked.connect(self.addNode)
        self.ui.add_group_btn.clicked.connect(self.add_group)
        self.ui.del_btn.clicked.connect(self.delete_node)
        self.ui.tile_window_btn.clicked.connect(self.tile_window)
        self.ui.close_all_win_btn.clicked.connect(self.close_all_window)

        self.ui.treeWidget.expandAll()
        # 字典用于记录打开窗口,某窗口存在时为激活已存在窗口
        self.subDict = {}

    def tile_window(self):
        self.ui.mdiArea.tileSubWindows()

    def close_all_window(self):
        self.subDict = {}
        self.ui.mdiArea.closeAllSubWindows()

    def display_area(self):
        '''
        功能:通过按钮显示/隐藏某区域
        :return:
        '''
        if self.ui.display_btn.text() == '<<':
            self.ui.groupBox.setHidden(True)
            self.ui.display_btn.setText('>>')
        else:
            self.ui.groupBox.setHidden(False)
            self.ui.display_btn.setText('<<')

    def on_tree_clicked(self, index):
        '''
        功能:单击显示子窗口
        :param index:
        :return:
        '''
        item = self.ui.treeWidget.currentItem()
        if item.parent() == None:
            return
        win_title = item.text(0)
        if win_title in self.subDict.keys():
            try:
                self.subDict[win_title].setFocus()
                return
            except:
                pass
        self.video_ui = video_dialog()
        self.ui.mdiArea.addSubWindow(self.video_ui)
        self.video_ui.setWindowTitle(win_title)
        self.video_ui.show()
        self.subDict[win_title] = self.video_ui
        th = Thread(target = self.display)
        th.start()

    def display(self):
        item = self.ui.treeWidget.currentItem()
        self.video_ui.video_dialog.video_lbl.setText(item.text(0))

    def add_group(self):
        root = QTreeWidgetItem(self.ui.treeWidget)
        root.setText(0, '新分组')
        root.setFlags(root.flags() | Qt.ItemIsEditable)
        self.ui.treeWidget.expandAll()

    def delete_node(self):
        '''
        功能:单击选中项后删除分组或节点
        :return:
        '''
        item = self.ui.treeWidget.currentItem()
        parent = item.parent()
        if parent != None:
            parent.removeChild(item)
            win_title = item.text(0)
            if win_title in self.subDict.keys():
                try:
                    self.subDict[win_title].setFocus()
                    self.ui.mdiArea.closeActiveSubWindow()
                except:
                    pass
                del self.subDict[win_title]

        else:
            # 获取当前根节点的子节点数量
            count = item.childCount()
            if count != 0:
                for i in range(0, count):
                    win_title = item.child(i).text(0)
                    if win_title in self.subDict.keys():
                        try:
                            self.subDict[win_title].setFocus()
                            self.ui.mdiArea.closeActiveSubWindow()
                        except:
                            pass
                        del self.subDict[win_title]
            root_index = self.ui.treeWidget.indexOfTopLevelItem(item)
            self.ui.treeWidget.takeTopLevelItem(root_index)

    def addNode(self):
        '''
        功能:新建节点
        :return:
        '''
        item = self.ui.treeWidget.currentItem()
        ip = self.ui.ip_lineEdit.text().strip()
        if len(ip) == 0:
            return
        try:
            if item.parent() == None:
                node = QTreeWidgetItem(item)
                node.setText(0, ip)
            else:
                node = QTreeWidgetItem(item.parent())
                node.setText(0, ip)
        except:
            pass
        self.ui.treeWidget.expandAll()

if __name__ == '__main__':
    app = QApplication([])
    server = UI()
    server.show()
    sys.exit(app.exec_())