简体   繁体   中英

How to embed a window in a Main Window in PyQt5

I have this snippet of code shown below:

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()

This opens a window to manage directories (files). But out of my MainApp UI (so, opens a new window), where i want to embed this external window, shown below:

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()

How can i embed this window in my MainApp UI, to be a part as a section of my main Graphical Application, like a Widget?

Thanks!

One possible solution is to use a layout to position the widget inside the window. The case of QMainWindow is special since you do not have to establish a layout, it has a predefined one:

在此处输入图片说明

What you must do is create a central widget and to that establish the layout:

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_())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM