pythonpython-3.xpyqtpyqt5qsplitter

How to display self defined QSplitter Class onto QMainWindow


I am currently trying to display a split screen view onto my window using PyQt5 with QSplitter and QMainWindow. I need QMainWindow to be able to create the menu options for the application that I am working on and would need to use QSplitter to use three separate windows for the application. Currently based on my code, the window is showing up, but there is no split screen shown at all. The only thing on the display is a blank screen when there should be the numbers: 1, 2, 3, and 4 shown in each corner.

I have tried to implement the QSplitters into the main window but then found that that approach does not work as it cannot set the layout of the QSplitters on top of setting the layout of the main window from QMainWindow. The next approach I used was the class based model where I defined what exactly I wanted for each splitter and implemented the subwindows into the main window and used two splitters (horizontal and vertical).

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

I expected there to be the results of having four split screens with the values of "1", "2", "3", and "4" on each corner of the screen. Instead the output is what I have gotten before which is just a blank screen with the menubar and status bar being functional.


Solution

  • QMainWindow unlike QWidget already has a predefined layout:

    enter image description here

    So you should not use a layout to set the QSplitter but use the setCentralWidget() method of QMainWindow:

    # ...
    
    self.subsplitter = QSplitter(Qt.Vertical)
    self.subsplitter.addWidget(self.subsplitter1)
    self.subsplitter.addWidget(self.subsplitter2)
    
    self.setCentralWidget(self.subsplitter)
    
    self.resize(900, 800)
    
    # ...