如何在QMainWind上显示自定义的qspliter类

2024-04-25 22:46:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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”的拆分屏幕。取而代之的是我之前得到的输出,它只是一个带有菜单栏和状态栏的空白屏幕。你知道吗


Tags: thetoimportselfnewmainexitaction
1条回答
网友
1楼 · 发布于 2024-04-25 22:46:30

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

enter image description here

因此,不应使用布局来设置qspliter,而应使用QMainWindow的^{}方法:

# ...

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

self.setCentralWidget(self.subsplitter)

self.resize(900, 800)

# ...

相关问题 更多 >