代码之家  ›  专栏  ›  技术社区  ›  user9165727

如何在PyQt5的主窗口中嵌入窗口

  •  1
  • user9165727  · 技术社区  · 7 年前

    我有以下代码片段:

    self.model = QFileSystemModel()
    self.model.setRootPath('')
    self.tree = QTreeView()
    self.tree.setModel(self.model)
    
    self.tree.setAnimated(False)
    self.tree.setIndentation(20)
    self.tree.setSortingEnabled(True)
    
    self.tree.setWindowTitle("Directory Viewer")
    self.tree.resize(323, 300)
    self.tree.show()
    

    这将打开一个窗口来管理目录(文件)。但在我的MainApp UI之外(因此,会打开一个新窗口),我想在其中嵌入此外部窗口,如下所示:

    class MainApp(QMainWindow):
        """This is the class of the MainApp GUI system"""
        def __init__(self):
            """Constructor method"""
            super().__init__()
            self.initUI()
    
        def initUI(self):
            """This method creates our GUI"""
    
            # Box Layout to organize our GUI
            # labels
            types1 = QLabel('Label', self)
            types1.resize(170, 20)
            types1.move(1470, 580)
    
            self.model = QFileSystemModel()
            self.model.setRootPath('')
            self.tree = QTreeView()
            self.tree.setModel(self.model)
    
            self.tree.setAnimated(False)
            self.tree.setIndentation(20)
            self.tree.setSortingEnabled(True)
    
            self.tree.setWindowTitle("Directory Viewer")
            self.tree.resize(323, 300)
            self.tree.show()
    
            self.setGeometry(50, 50, 1800, 950)
            self.setFixedSize(self.size())
            self.centering()
            self.setWindowTitle('MainApp')
            self.setWindowIcon(QIcon('image/logo.png'))
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = MainApp()
        sys.exit(app.exec_())
        self.show()
    

    如何将此窗口嵌入MainApp UI,作为主要图形应用程序的一部分,如小部件?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   eyllanesc    7 年前

    一种可能的解决方案是使用布局将小部件放置在窗口内。的情况 QMainWindow 是特殊的,因为您不必建立布局,它有一个预定义的布局:

    enter image description here

    您必须做的是创建一个中心小部件并建立布局:

    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    import sys
    
    
    class MainApp(QMainWindow):
        """This is the class of the MainApp GUI system"""
        def __init__(self):
            """Constructor method that inherits methods from QWidgets"""
            super().__init__()
            self.initUI()
    
        def initUI(self):
            """This method creates our GUI"""
            centralwidget = QWidget()
            self.setCentralWidget(centralwidget)
            lay = QVBoxLayout(centralwidget)
            # Box Layout to organize our GUI
            # labels
            types1 = QLabel('Label')
            lay.addWidget(types1)
    
            self.model = QFileSystemModel()
            self.model.setRootPath('')
            self.tree = QTreeView()
            self.tree.setModel(self.model)
    
            self.tree.setAnimated(False)
            self.tree.setIndentation(20)
            self.tree.setSortingEnabled(True)
            lay.addWidget(self.tree)
    
            self.setGeometry(50, 50, 1800, 950)
            self.setFixedSize(self.size())
            self.setWindowTitle('MainApp')
            self.setWindowIcon(QIcon('image/logo.png'))
            self.show()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = MainApp()
        sys.exit(app.exec_())
    

    enter image description here