Как отобразить самостоятельно определенный класс QSplitter на QMainWindow - PullRequest
1 голос
/ 05 июня 2019

В настоящее время я пытаюсь отобразить в моем окне разделенный экран, используя PyQt5 с QSplitter и QMainWindow.Мне нужно, чтобы QMainWindow мог создавать опции меню для приложения, над которым я работаю, и мне нужно было бы использовать QSplitter, чтобы использовать три отдельных окна для приложения.В настоящее время, основываясь на моем коде, окно отображается, но разделенный экран вообще не отображается.Единственное, что отображается на экране, это пустой экран, когда в каждом углу должны отображаться цифры: 1, 2, 3 и 4.

Я попытался внедрить QSplitters в главное окно, но потом нашелчто этот подход не работает, так как он не может установить макет QSplitters поверх установки макета главного окна из QMainWindow.Следующим подходом, который я использовал, была модель на основе классов, в которой я определил, что именно мне нужно для каждого сплиттера, внедрил подокна в главное окно и использовал два сплиттера (горизонтальный и вертикальный).

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

Я ожидал тамбыть результатом наличия четырех разделенных экранов со значениями «1», «2», «3» и «4» в каждом углу экрана.Вместо этого я получаю вывод, который представляет собой пустой экран с функциональной строкой меню и строкой состояния.

1 Ответ

1 голос
/ 05 июня 2019

QMainWindow в отличие от QWidget уже имеет предопределенный макет :

enter image description here

Поэтому вам не следует использовать макет для установки QSplitterно используйте метод setCentralWidget() QMainWindow:

# ...

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

self.setCentralWidget(self.subsplitter)

self.resize(900, 800)

# ...
...