QT main window composition


introduce

The main window QMainWindowis a class that provides the main window program for the user, including

  • a menu bar
  • Multiple toolbars
  • Multiple dock widgets
  • A status bar (status bar)
  • A central widget

The main window is the top-level window that interacts with the user for a long time. Most of the functions of the program are directly provided by the main window. The main window is usually the first window displayed after the application starts. QMainWindow is the base class of the main window in Qt.

Menu Bar

Provides menu-related operations in Qt, providing a list of commands. Qt Actionrepresents these commands using , and achieves consistency for commands such as menus, toolbar buttons, keyboard shortcuts, etc. Located below the title bar of the main window.

Create a menu in the Qt main window:

    QMenuBar* menubar = menuBar();
    QMenu* menu = new QMenu(tr("新建"));
    QAction* action = new QAction(tr("New")); //

    menu->addAction(action);
    menubar->addMenu(menu);

Shortcut key settings:

    action->setShortcut(tr("Ctrl+N"));

Prompt settings:

    action->setStatusTip(tr("新建一个文件"));

Status Bar

The status bar usually displays some status information of the application and is located at the bottom of the main window, on which Qt widgets can be added.

QStatusBar *statusBar = statusBar();
QLabel *label = new QLabel("Status Bar Label");
QLineEdit *lineEdit = new QLineEdit("Status Bar LineEdit");
//statusbar->showMessage("欢迎",30000); // 指定的字符串作为提示信息显示在状态栏左侧,并且第二个是指定显示的时间
statusBar->addWidget(label);
statusBar->addPermanentWidget(lineEdit);
method describe
addWidget() Adds the given widget object (left side) in the status bar
addPermanentWidget() Permanently adds the given widget object (right side) in the status bar
showMessage() Display a temporary message in the status bar for a specified time interval
clearMessage() Delete the temporary information being displayed
removeWidget() Removes the specified gizmo from the status bar

toolbar

A toolbar is a panel made up of a series of button-like actions, usually consisting of frequently used commands ( QAction). It is located below the menu bar and above the status bar, and can be docked in four directions: up, down, left, and right of the main window.

    QToolBar *toolBar = addToolBar(tr("File"));
    toolBar->addAction(action);
    // 设置停靠区域 默认为 AllToolBarAreas
    // TopToolBarArea BottomToolBarArea LeftToolBarArea RightToolBarArea AllToolBarAreas
    toolBar->setAllowedAreas(Qt::TopToolBarArea|Qt::LeftToolBarArea);
    //toolBar->setMovable(false); // 设置可移动性

Dock widget

A dock widget is used as a container to contain other widgets to achieve certain functions. It is located inside the toolbar area. It can be used as a window to float freely above the main window, or it can be docked on the top, bottom, left, and right of the main window like a toolbar.

central part

In the center of the main window.

code display

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    
    
    QMenuBar* menubar = menuBar();
    QMenu* menu = new QMenu(tr("新建"));
    QAction* action = new QAction(tr("New")); //

    menu->addAction(action);
    menubar->addMenu(menu);
    action->setShortcut(tr("Ctrl+N"));
    action->setStatusTip(tr("新建一个文件"));

    QStatusBar *statusbar = statusBar();
    QLabel *label = new QLabel("Status Bar Label");
    QLineEdit *lineEdit = new QLineEdit("Status Bar LineEdit");

    //statusbar->showMessage("欢迎",3000); // 指定的字符串作为提示信息显示在状态栏左侧,并且第二个是指定显示的时间
    statusbar->addWidget(label);
    statusbar->addPermanentWidget(lineEdit);

    QToolBar *toolBar = addToolBar(tr("File"));
    toolBar->addAction(action);
    // 设置停靠区域 默认为 AllToolBarAreas
    // TopToolBarArea BottomToolBarArea LeftToolBarArea RightToolBarArea AllToolBarAreas
    toolBar->setAllowedAreas(Qt::TopToolBarArea|Qt::LeftToolBarArea);
    //toolBar->setMovable(false); // 设置可移动性

    // 停靠窗口1
    // 步骤1 创建一个`QDockWidget`对象的停靠窗体
    QDockWidget *dock1 = new QDockWidget(tr("停靠窗口1"),this);
    // 步骤2 设置此停靠窗体的属性,通常调用 `setFeatures()`以及 `setAllowedAreas()`
    dock1->setFeatures(QDockWidget::DockWidgetMovable); // 可移动
    dock1->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea); // 可 左侧停靠 和 右侧停靠
    //dock1->setFloating(true); // 不设置 不能悬浮
    // 步骤3 新建一个要插入停靠窗体的控件,比如 `QListWidget`、`QTextEdit`
    QLineEdit *textEdit1 = new QLineEdit(this);
    textEdit1->setText(tr("停靠窗口1,可移动到左侧停靠和右侧停靠"));
    // 步骤4 将控件插入停靠窗体,调用`QDockWidget`的`setWidget()`方法
    dock1->setWidget(textEdit1);
    // 步骤5 使用 `addDockWidget()`方法在`MainWindow`中加入此停靠窗体
    addDockWidget(Qt::LeftDockWidgetArea,dock1);

    QDockWidget *dockWidget = new QDockWidget(tr("DockWidget"));
    QListWidget *listWidget = new QListWidget;
    listWidget->addItem(tr("Item1"));
    listWidget->addItem(tr("Item2"));
    listWidget->addItem(tr("Item3"));
    dockWidget->setWidget(listWidget);
    dockWidget->setFloating(false); // 浮动主窗口内,可手动脱离
    //LeftDockWidgetArea RightDockWidgetArea TopDockWidgetArea BottomDockWidgetArea AllDockWidgetAreas
    dockWidget->setAllowedAreas(Qt::BottomDockWidgetArea|Qt::TopDockWidgetArea);
    addDockWidget(Qt::TopDockWidgetArea,dockWidget);

    QTextEdit *textEdit = new QTextEdit(this);
    textEdit->setText(tr("主窗口"));
    textEdit->setAlignment(Qt::AlignCenter);
    this->setCentralWidget(textEdit); // 设置 textEdit 为主窗口的中央窗体
}

MainWindow::~MainWindow()
{
    
    
}

Guess you like

Origin blog.csdn.net/m0_45463480/article/details/130434168
Recommended