[ Qt ] QMainWindow

Table of contents

1. Basic overview

2. Menu bar

3. Toolbar

4. Status bar

5. Riveted parts

6. Central part

7. Resource files

(1) Create a menu bar and menu items

(2) Create a toolbar

(3) Create anchor components

(4) Create a central file

(5) Create a status bar


1. Basic overview

QMainWindow is a class that provides users with the main window program, including a menu bar (menu bar), multiple toolbars (tool bars), multiple dock widgets (dock widgets), a status bar (status bar) and a center Components (central widget) are the basis of many applications, such as text editors, image editors, etc.

2. Menu bar

(1) There is only one        menu bar at most , and it is at the top of the window
  (2) Create the menu bar: Obtain the menu bar pointer of the main window through the menubar() function of the QMainWindow class
    #include < QMenuBar >
    QMenuBar *bar = MenuBar();
    at this time the menu The bar is not only empty, but also independent of the window
  (3) The menu bar is put into the window:
    setMenuBar(bar)
    The menu bar has been put into the window at this time, but the menu bar has no content, so it is not displayed
  (4) Create the menu:
    QMenu * fileMenu =bar->addMenu("File");
  (5) Create menu items:
    fileMenu ->setAction("New")
    fileMenu ->setAction("Open")
  (6) Separator between menu items
     fileMenu->addSeparator ();

3. Toolbar

 (1) Toolbar, (can be multiple), but only in the left/right/up/down position, the default is above
  (2) Toolbar creation:
    #include < QToolBar >
    QToolBarr *toolbar = new ToolBar(this) ;
  (3) Toolbar into the window:
    addToolBar (toolbar);
    addToolBar (default docking area, toolbar);
  (4) Toolbar docking range:
    toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);
  (5 ) Toolbar floating:
    toolBar->setFloatable(false);
  (6) Tool item creation
     toolBar->addAction("New");
     toolBar->addAction("Open"); (7) Separator  toolBar
  between tool items
    ->addSeparator();
  (8) The control is used as a tool item
     QPushButton *btn1=new QPushButton("button", this);//Create a control
     toolBar->addWidget(btn1);//Add the control to the toolbar

4. Status bar

       (1) The status bar, (only one at most), is created at the bottom of the window
  (2) The status bar is created:
    #include < QStatusBar >
    QStatusBar * staBar= statusBar();
  (3) The status bar is placed in the window:
    setStatusBar(staBar);
  (4) Add status items:
    QLabel * label=new QLabel("prompt information", this);
     staBar->addWidget(label);//add
     QLabel on the left side *label1=new QLabel("prompt information on the right", this );
     staBar->addPermanentWidget(label1);//Add on the right side

5. Riveted parts

(1) Anchoring parts, ( there can be more than one ), placed in the upper, lower, left and right positions                          

(2) Create docking widget:
  #include
  QDockWidget *dockWidget=new QDockWidget("Docking widget/floating window", this);

(3) Put docking widget into window:
 addDockWidget( Qt::BottomDockWidgetArea,dockWidget);/ /Parameter 1: default position
(4) anchor component placement range:
dockWidget>setAllowedAreas(Qt::TopDockWidgetArea|Qt::BottomDockWidgetArea);

6. Central part

(1) Central widget, ( only one )
(2) Central widget creation:
  #include
  QTextEdit *edit =new QTextEdit(this);//Add to the object tree
(3) Central widget into the window:
  setCentralWidget(edit) ;

7. Resource files

In this section, how to create an interface by dragging components through the interface file: the ui file is shown in the figure:

(1) Create a menu bar and menu items

(2) Create a toolbar

(3) Create anchor components

(4) Create a central file

(5) Create a status bar

Guess you like

Origin blog.csdn.net/m0_53415522/article/details/127707314