我有以下代码片段:
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用户界面中嵌入此窗口,作为我的主要图形应用程序的一部分,如Widget?
谢谢!
答案 0 :(得分:1)
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_())