PyQt study notes - main form (QMainWindow)

Describe the main form information and basic operations of PyQt.

The main form class is a common main form, including menu bar ( QMenuBar ), tool bar ( QToolBar s), hover widget ( QDockWidget s), central widget (Central Widget ), status bar ( QStatusBar ) and other basic types.

The description in PyQt6 is detailed in: QMainWindow — PyQt Documentation v6.4.0 (riverbankcomputing.com)

1. Basic operation of the main form

    • Create the main form

QMainWindow is generally used as a top-level form, usually through inheritance, and then create your own layout based on it.

class MainWindow(QMainWindow):
    """主窗体类"""
    def __init__(self):
        """构造函数"""
        super(MainWindow,self).__init__()        # 调用父类初始化函数,需要调用父类初始化才能正常使用父类定一的变量
        # 其他代码块
    • Set the layout of the middle control of the main form

The QMainWindow class has its own layout, which cannot be laid out using the setLayout() method, nor can it be embedded in other windows. It can only be used as a top-level window. The layout can only be created in the CentralWidget, and the setLayout() method of the central component is called.

self.central_widget = QWidget()
self.central_widget.setLayout(layout)
self.setCentralWidget(self.central_widget)
    • Center the window

The centering of the window needs to be realized by code. You can use the QRect object of the form to do the centering, or you can use the coordinate system of the form to calculate the coordinates, and then move the window to do the centering.

    def center(self):
        """通过获取窗口矩形进行居中显示"""
        win_rect = self.frameGeometry()  # 获取窗口矩形
        screen_center = self.screen().availableGeometry().center()  # 屏幕中心
        win_rect.moveCenter(screen_center)      # 移动窗口矩形到屏幕中心
        self.move(self.win_rect.center)         # 移动窗口与窗口矩形重合


    def center(self):
        """通过坐标系来进行居中显示,通过定位窗口左上角坐标进行定位"""
        # 获取屏幕坐标系
        screen = QDesktopWidget().screenGeometry()
        # 获取窗口坐标系
        size = self.geometry()
        position_left = (screen.width() - size.width())/2
        position_top = (screen.height() - size.height())/2
        self.move(position_left,position_top)

2. Get the built-in controls of the main form

    • Status Bar

QMainWindow has its own status bar, you can get its own status bar through the statusBar() method, or instantiate the QStatusBar class, and then use the QMainWindow.setStatusBar() method to add the status bar to the main window.

Multiple controls can be added to display information through the addPermanentWidget() method of the status bar

Example 1, get the status bar directly:

        # 直接获取状态栏
        self.statusBar = self.statusBar()
        self.statusBar.showMessage("显示状态栏信息",5000)

result:

Example 2, instantiated and then added to the main form:

        # 实例化状态栏后再添加到主窗体中
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("显示状态栏信息",5000)

result:

    • Menu Bar

The menu bar can be obtained by calling the menuBar method of QMainWindow, and then add the QMenu main menu through the addMenu() method, and then further add menus or submenus under the main menu.

        # 获取菜单栏
        self.mbar = self.menuBar()
    • toolbar

QMainWindow directly calls the addToolBar() method to add a toolbar.

Guess you like

Origin blog.csdn.net/u010839204/article/details/128609177