Python 如何在QMainWindow上显示自定义的QSplitter类

Python 如何在QMainWindow上显示自定义的QSplitter类,python,python-3.x,pyqt,pyqt5,qsplitter,Python,Python 3.x,Pyqt,Pyqt5,Qsplitter,我目前正在尝试使用带有QSplitter和QMainWindow的PyQt5在我的窗口上显示分屏视图。我需要QMainWindow能够为我正在处理的应用程序创建菜单选项,并且需要使用qspliter为应用程序使用三个单独的窗口。目前,根据我的代码,窗口正在显示,但根本没有显示分割屏幕。显示屏上只有一个空白屏幕,每个角落都应该显示数字:1、2、3和4 我曾尝试在主窗口中实现qspliters,但后来发现这种方法不起作用,因为它无法在从QMainWindow设置主窗口布局的基础上设置qspliter

我目前正在尝试使用带有QSplitter和QMainWindow的PyQt5在我的窗口上显示分屏视图。我需要QMainWindow能够为我正在处理的应用程序创建菜单选项,并且需要使用qspliter为应用程序使用三个单独的窗口。目前,根据我的代码,窗口正在显示,但根本没有显示分割屏幕。显示屏上只有一个空白屏幕,每个角落都应该显示数字:1、2、3和4

我曾尝试在主窗口中实现qspliters,但后来发现这种方法不起作用,因为它无法在从QMainWindow设置主窗口布局的基础上设置qspliters的布局。我使用的下一种方法是基于类的模型,在该模型中,我为每个拆分器定义了我想要的内容,并在主窗口中实现了子窗口,并使用了两个拆分器(水平和垂直)


我希望有四个分割屏幕的结果,屏幕的每个角落都有“1”、“2”、“3”和“4”的值。相反,输出是我之前得到的,它只是一个空白屏幕,菜单栏和状态栏可以正常工作。

QMainWindow与QWidget不同,它已经有一个:

因此,您不应使用布局来设置QSplitter,而应使用QMainWindow的方法:

#。。。
self.subsplitter=qspliter(Qt.垂直)
self.subslitter.addWidget(self.subslitter 1)
self.subsliter.addWidget(self.subsliter 2)
self.setCentralWidget(self.subscreet)
自我调整大小(900800)
# ...

非常感谢!这个实现对我来说是成功的,你的解释非常好!
import sys

#imports
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction, QVBoxLayout, QStackedWidget
from PyQt5.QtWidgets import QMessageBox, QFrame, QSplitter, QTextEdit, QHBoxLayout, QLineEdit, QLabel
from PyQt5.QtGui import QKeySequence
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from PyQt5.QtGui import QIcon #to import icon

'''
class widget1(QWidget):
    """docstring for widget1"""
    def __init__(self):
        QWidget.__init__(self)
        hbox = QHBoxLayout()
        #create split screen

        #this is the top left frame of the window
        topleft = QFrame()
        topleft.setFrameShape(QFrame.StyledPanel)

        #first splitter
        splitter1 = QSplitter(Qt.Horizontal)
        lineedit = QLineEdit()
        splitter1.addWidget(topleft)
        splitter1.addWidget(lineedit)
        splitter1.setSizes([200,200])

        #second splitter and it is located at the bottom of the screen
        bottom = QFrame()
        bottom.setFrameShape(QFrame.StyledPanel)
        splitter2 = QSplitter(Qt.Horizontal)

        #add the splitter to the layout
        hbox.addWidget(splitter2)
'''

class SubWindow(QWidget):
    def __init__(self, label):
        super(SubWindow, self).__init__()

        self.label = QLabel(label)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setStyleSheet("QLabel {font-size:40px;}")

        self.main_layout = QVBoxLayout()
        self.main_layout.addWidget(self.label)
        self.setLayout(self.main_layout)


#GUI class to create window
class GUI(QMainWindow):
    """docstring for GUI"""
    def __init__(self,):
        # Understand and explain ...
        super().__init__()

        #initialize the UI using the initUI method
        self.initUI()

    def initUI(self):
        #set the title of the window
        self.setWindowTitle('Visualization')
        #Set the status bar in the beginning
        self.statusBar().showMessage("In Progress")

        #create new menubar object at top of window
        menubar = self.menuBar()

        #add file button to menubar
        file_menu = menubar.addMenu("File")

        #add edit button to menubar
        edit_menu = menubar.addMenu("Edit")

        #add view button to menubar
        view_menu = menubar.addMenu("View")

        #path for new icon and create the icon
        new_icon = QIcon('newfileicon.png')
        open_icon =QIcon('openfileicon.png')
        exitapp_icon = QIcon('exitappicon.png')

        #create new subbutton for the file menu and add icon and shortcuts
        #as well as connecting the methods for each file menu
        new_action = QAction(new_icon,'New', self)
        new_action.setShortcut('Ctrl+N')
        new_action.triggered.connect(self.newCall)

        open_action = QAction(open_icon,'Open', self)
        open_action.setShortcut('Ctrl+O')
        open_action.triggered.connect(self.openCall)

        exit_action = QAction(exitapp_icon,'Exit', self)
        exit_action.setShortcut('Ctrl+X')
        exit_action.triggered.connect(self.exitCall)

        #add the action to the file menu button
        file_menu.addAction(new_action)
        file_menu.addAction(open_action)
        file_menu.addAction(exit_action)

        #when hovering over the "new", "open", and "exit", update statusbar
        new_action.setStatusTip("Create New File")
        open_action.setStatusTip("Open a file")
        exit_action.setStatusTip("Exit application")

        #when clicking the exit, close the application
        exit_action.triggered.connect(self.close)


        self.SubWindow1 = SubWindow("1")
        self.SubWindow2 = SubWindow("2")
        self.SubWindow3 = SubWindow("3")
        self.SubWindow4 = SubWindow("4")

        self.subsplitter1 = QSplitter(Qt.Horizontal)
        self.subsplitter1.addWidget(self.SubWindow1)
        self.subsplitter1.addWidget(self.SubWindow2)

        self.subsplitter2 = QSplitter(Qt.Horizontal)
        self.subsplitter2.addWidget(self.SubWindow3)
        self.subsplitter2.addWidget(self.SubWindow4)

        self.subsplitter = QSplitter(Qt.Vertical)
        self.subsplitter.addWidget(self.subsplitter1)
        self.subsplitter.addWidget(self.subsplitter2)

        self.main_layout = QVBoxLayout()
        self.main_layout.addWidget(self.subsplitter)
        self.setLayout(self.main_layout)



        #hbox = widget1()

        #self.view = QHBoxLayout(self)

        #resize the window to a 900x800 window
        self.resize(900, 800)

    def newCall(self):
        QMessageBox.about(self, "Confirmation", "Are you sure you want to create a new file?")

    def openCall(self):
        QMessageBox.about(self, "Confirmation", "Are you sure you want to open a file?")

    #if yes to the messagebox, then close, else dont close and pass
    def exitCall(self):
        reply = QMessageBox.question(self, "Confirmation", "Are you sure you want to exit the application?", 
            QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.close
            sys.exit()
        else:
            pass


#main function
if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = GUI()
    gui.show()
    sys.exit(app.exec_())