Qt development summary (8) - a form of MainWindow

MainWindow is a special Widget, it has toolbars a QToolBar , menu bar QMenuBar , and status bar QStatusBar , and may be designed to stop and center the form form, typical architecture MainWindow shown below.

Center Widget

In particular, create a MainWindow must create a center widget, even if it is empty widget. In general, center widget is a standard Qt Widget, such as QTextEdit, QGraphicsView and so on. Of course, you can create yourself a new widget, the function set with the setCentralWidget () a widget to center widget.

There MainWindow SDI (Single Document) and MDI (Multiple Document), with respect to the MDI can create QMdiArea as a center widget. QMainWindow can the current layout by saveState () preserved until to recover with restoreState ().

Menubars

QMainWindow with QMenuBar achieve some QMenu. Add QActions on these Menus, it will display the dropdown menu to the menu bar. QMenuBar can be obtained by menuBar (), which in turn calls QMenuBar :: addMenu () to add a new menu option. QMainWindow can select the default menu bar in new construction, you can also add their own definition of the menu bar in the program.

void MainWindow::createMenus()
{
      fileMenu = menuBar()->addMenu(tr("&File"));
      fileMenu->addAction(newAct);
      fileMenu->addAction(openAct);
      fileMenu->addAction(saveAct);
}

Toolbars

Toolbar achieved by QToolBar class. When you call addToolBar () to add a new toolbar to MainWindow, create, Qt :: ToolBarArea specify the location of the toolbar (top left bottom right). Can also be called addToolBarBreak () or insertToolBarBreak () in the middle of a toolbar to insert delimiter. Can QToolBar :: setAllowedAreas () and QToolBar :: setMovable () limit position and move the tool bar. Each toolbar icon size can be () limited by iconSize, you can call setIconSize modify it, even calling setToolButtonStyle modify display.

void MainWindow::createToolBars()
{
      fileToolBar = addToolBar(tr("File"));
      fileToolBar->addAction(newAct);
}

Dock Widgets

QDockWidget object may be generated Dock widgets. Dock Widget is a MainWindow can be docked in a form, you can call addDockWidget () to add the Dock Widgets in MainWindow. There are four regions can be docked in MainWindow, respectively Qt :: DockWidgetArea enum: left, right, top, and bottom. () Function to specify which Dock Widget with setCorner can occupy corners. The default each zone may contain only one line dock widgets, but if you turn on the grid function setDockNestingEnabled (), dock widgets can be any combination. Two dock widgets may also be formed Tab form is displayed, this is a stop mode.     

QDockWidget *dockWidget = new QDockWidget(tr("Dock Widget"), this);
dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
dockWidget->setWidget(dockWidgetContents);
addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

Status Bar

() Gets main window's status bar by statusBar, the returned object is QStatusBar . With setStatusBar () set the status bar. The status bar provides a horizontal progress bar to show the current status information.

With showMessage () function to display a short slot information, using clearMessage () Clear message,

void MainWindow::createStatusBar()
{
      statusBar()->showMessage(tr("Ready"));
}

Normal and permanent information by creating a small form, such as QLabel, QProgressBar or QToolButton, then add them with addWidget toolbar () or addPermanentWidget () be achieved. With removeWidget () function to clear the mind that information.

statusBar()->addWidget(new MyReadWriteIndication);

code example

There is a relatively good MainWindow example, the venue can I download Channel: QtMainWindow

Published 76 original articles · won praise 63 · views 50000 +

Guess you like

Origin blog.csdn.net/bjtuwayne/article/details/100173937