QMainWindow class provides a main application window. 更多...
Header: | #include <QMainWindow> |
qmake: | QT += widgets |
继承: | QWidget |
enum | DockOption { AnimatedDocks, AllowNestedDocks, AllowTabbedDocks, ForceTabbedDocks, VerticalTabs, GroupedDragging } |
flags | DockOptions |
|
|
QMainWindow (QWidget * parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()) | |
virtual | ~QMainWindow () |
void | addDockWidget (Qt::DockWidgetArea area , QDockWidget * dockwidget ) |
void | addDockWidget (Qt::DockWidgetArea area , QDockWidget * dockwidget , Qt::Orientation orientation ) |
void | addToolBar (Qt::ToolBarArea area , QToolBar * toolbar ) |
void | addToolBar (QToolBar * toolbar ) |
QToolBar * | addToolBar (const QString & title ) |
void | addToolBarBreak (Qt::ToolBarArea area = Qt::TopToolBarArea) |
QWidget * | centralWidget () const |
Qt::DockWidgetArea | corner (Qt::Corner corner ) const |
virtual QMenu * | createPopupMenu () |
QMainWindow::DockOptions | dockOptions () const |
Qt::DockWidgetArea | dockWidgetArea (QDockWidget * dockwidget ) const |
bool | documentMode () const |
QSize | iconSize () const |
void | insertToolBar (QToolBar * before , QToolBar * toolbar ) |
void | insertToolBarBreak (QToolBar * before ) |
bool | isAnimated () const |
bool | isDockNestingEnabled () const |
QMenuBar * | menuBar () const |
QWidget * | menuWidget () const |
void | removeDockWidget (QDockWidget * dockwidget ) |
void | removeToolBar (QToolBar * toolbar ) |
void | removeToolBarBreak (QToolBar * before ) |
void | resizeDocks (const QList<QDockWidget *> & docks , const QList<int> & sizes , Qt::Orientation orientation ) |
bool | restoreDockWidget (QDockWidget * dockwidget ) |
bool | restoreState (const QByteArray & state , int version = 0) |
QByteArray | saveState (int version = 0) const |
void | setCentralWidget (QWidget * widget ) |
void | setCorner (Qt::Corner corner , Qt::DockWidgetArea area ) |
void | setDockOptions (QMainWindow::DockOptions options ) |
void | setDocumentMode (bool enabled ) |
void | setIconSize (const QSize & iconSize ) |
void | setMenuBar (QMenuBar * menuBar ) |
void | setMenuWidget (QWidget * menuBar ) |
void | setStatusBar (QStatusBar * statusbar ) |
void | setTabPosition (Qt::DockWidgetAreas areas , QTabWidget::TabPosition tabPosition ) |
void | setTabShape (QTabWidget::TabShape tabShape ) |
void | setToolButtonStyle (Qt::ToolButtonStyle toolButtonStyle ) |
void | splitDockWidget (QDockWidget * first , QDockWidget * second , Qt::Orientation orientation ) |
QStatusBar * | statusBar () const |
QTabWidget::TabPosition | tabPosition (Qt::DockWidgetArea area ) const |
QTabWidget::TabShape | tabShape () const |
QList<QDockWidget *> | tabifiedDockWidgets (QDockWidget * dockwidget ) const |
void | tabifyDockWidget (QDockWidget * first , QDockWidget * second ) |
QWidget * | takeCentralWidget () |
Qt::ToolBarArea | toolBarArea (QToolBar * toolbar ) const |
bool | toolBarBreak (QToolBar * toolbar ) const |
Qt::ToolButtonStyle | toolButtonStyle () const |
bool | unifiedTitleAndToolBarOnMac () const |
void | setAnimated (bool enabled ) |
void | setDockNestingEnabled (bool enabled ) |
void | setUnifiedTitleAndToolBarOnMac (bool set ) |
void | iconSizeChanged (const QSize & iconSize ) |
void | tabifiedDockWidgetActivated (QDockWidget * dockWidget ) |
void | toolButtonStyleChanged (Qt::ToolButtonStyle toolButtonStyle ) |
const QMetaObject | staticMetaObject |
virtual void | contextMenuEvent (QContextMenuEvent * event ) override |
virtual bool | event (QEvent * event ) override |
QMainWindow class provides a main application window.
A main window provides a framework for building an application's user interface. Qt has QMainWindow and its 相关类 for main window management. QMainWindow has its own layout to which you can add QToolBar s, QDockWidget s, a QMenuBar ,和 QStatusBar 。布局具有可以被任何种类 Widget 占据的中心区域。见以下布局图像。
注意: 不支持创建没有中心 Widget 的主窗口。必须有中心小部件,即使它只是占位符。
中心小部件通常是标准 Qt Widget,譬如
QTextEdit
或
QGraphicsView
。自定义 Widget 还可以用于高级应用程序。设置中心 Widget 采用
setCentralWidget()
.
主窗口有 SDI (单文档界面) 或 MDI (多文档界面)。在 Qt 中创建 MDI 应用程序通过使用 QMdiArea 作为中心 Widget。
现在,我们将研究可以被添加到主窗口的每一其它 Widget。举例说明如何创建和添加它们。
Qt 实现菜单在 QMenu and QMainWindow keeps them in a QMenuBar . QAction 被添加到菜单,将它们显示为菜单项。
可以把新菜单添加到主窗口的菜单栏通过调用
menuBar()
,其返回
QMenuBar
为窗口,然后添加菜单采用
QMenuBar::addMenu
().
QMainWindow
comes with a default menu bar, but you can also set one yourself with
setMenuBar()
。若希望实现自定义菜单栏 (即:不使用
QMenuBar
Widget),可以设置它采用
setMenuWidget()
.
以下是如何创建菜单的范例:
void MainWindow::createMenus() { fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAct); fileMenu->addAction(openAct); fileMenu->addAction(saveAct);
createPopupMenu()
函数创建弹出菜单,当主窗口接收到上下文菜单事件时。默认实现生成带来自停放 Widget 和工具栏的可复选动作的菜单。可以重实现
createPopupMenu()
为自定义菜单。
工具栏的实现在
QToolBar
类。把工具栏添加到主窗口采用
addToolBar()
.
控制工具栏的初始位置通过把它们赋值给特定
Qt::ToolBarArea
。可以通过插入工具栏打断分割区域 - 把它想像成文本编辑中的换行符 - 采用
addToolBarBreak()
or
insertToolBarBreak()
。还可以约束用户放置采用
QToolBar::setAllowedAreas
() 和
QToolBar::setMovable
().
可以检索工具栏图标的尺寸采用
iconSize()
。尺寸从属平台;可以设置固定尺寸采用
setIconSize()
。可以更改工具栏中所有工具按钮的外观采用
setToolButtonStyle()
.
以下是创建工具栏的范例:
void MainWindow::createToolBars() { fileToolBar = addToolBar(tr("File")); fileToolBar->addAction(newAct);
停放 Widget 的实现在
QDockWidget
类。停放 Widget 是可以停放在主窗口中的窗口。可以把停放 Widget 添加到主窗口采用
addDockWidget()
.
There are four dock widget areas as given by the
Qt::DockWidgetArea
enum: left, right, top, and bottom. You can specify which dock widget area that should occupy the corners where the areas overlap with
setCorner()
. By default each area can only contain one row (vertical or horizontal) of dock widgets, but if you enable nesting with
setDockNestingEnabled()
, dock widgets can be added in either direction.
Two dock widgets may also be stacked on top of each other. A QTabBar is then used to select which of the widgets should be displayed.
We give an example of how to create and add dock widgets to a main window:
QDockWidget *dockWidget = new QDockWidget(tr("Dock Widget"), this); dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); dockWidget->setWidget(dockWidgetContents); addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
可以设置状态栏采用
setStatusBar()
,但仅创建一个当首次
statusBar()
(返回主窗口的状态栏) 被调用。见
QStatusBar
了解如何使用它的有关信息。
QMainWindow
can store the state of its layout with
saveState()
;稍后,可以检索它采用
restoreState()
。它是存储的工具栏和停放 Widget 的位置和尺寸 (相对于主窗口的尺寸)。
另请参阅 QMenuBar , QToolBar , QStatusBar , QDockWidget , 应用程序范例 , 停放 Widget 范例 , MDI 范例 , SDI Example ,和 菜单范例 .
This enum contains flags that specify the docking behavior of QMainWindow .
常量 | 值 | 描述 |
---|---|---|
QMainWindow::AnimatedDocks
|
0x01
|
Identical to the animated 特性。 |
QMainWindow::AllowNestedDocks
|
0x02
|
Identical to the dockNestingEnabled 特性。 |
QMainWindow::AllowTabbedDocks
|
0x04
|
The user can drop one dock widget "on top" of another. The two widgets are stacked and a tab bar appears for selecting which one is visible. |
QMainWindow::ForceTabbedDocks
|
0x08
|
Each dock area contains a single stack of tabbed dock widgets. In other words, dock widgets cannot be placed next to each other in a dock area. If this option is set, AllowNestedDocks has no effect. |
QMainWindow::VerticalTabs
|
0x10
|
The two vertical dock areas on the sides of the main window show their tabs vertically. If this option is not set, all dock areas show their tabs at the bottom. Implies AllowTabbedDocks. See also setTabPosition (). |
QMainWindow::GroupedDragging
|
0x20
|
When dragging the titlebar of a dock, all the tabs that are tabbed with it are going to be dragged. Implies AllowTabbedDocks. Does not work well if some QDockWidgets have restrictions in which area they are allowed. (This enum value was added in Qt 5.6.) |
These options only control how dock widgets may be dropped in a QMainWindow . They do not re-arrange the dock widgets to conform with the specified options. For this reason they should be set before any dock widgets are added to the main window. Exceptions to this are the AnimatedDocks and VerticalTabs options, which may be set at any time.
This enum was introduced or modified in Qt 4.3.
The DockOptions type is a typedef for QFlags <DockOption>. It stores an OR combination of DockOption values.
This property holds whether manipulating dock widgets and tool bars is animated
When a dock widget or tool bar is dragged over the main window, the main window adjusts its contents to indicate where the dock widget or tool bar will be docked if it is dropped. Setting this property causes QMainWindow to move its contents in a smooth animation. Clearing this property causes the contents to snap into their new positions.
By default, this property is set. It may be cleared if the main window contains widgets which are slow at resizing or repainting themselves.
Setting this property is identical to setting the AnimatedDocks option using setDockOptions ().
This property was introduced in Qt 4.2.
访问函数:
bool | isAnimated () const |
void | setAnimated (bool enabled ) |
This property holds whether docks can be nested
若此特性为
false
, dock areas can only contain a single row (horizontal or vertical) of dock widgets. If this property is
true
, the area occupied by a dock widget can be split in either direction to contain more dock widgets.
Dock nesting is only necessary in applications that contain a lot of dock widgets. It gives the user greater freedom in organizing their main window. However, dock nesting leads to more complex (and less intuitive) behavior when a dock widget is dragged over the main window, since there are more ways in which a dropped dock widget may be placed in the dock area.
Setting this property is identical to setting the AllowNestedDocks option using setDockOptions ().
This property was introduced in Qt 4.2.
访问函数:
bool | isDockNestingEnabled () const |
void | setDockNestingEnabled (bool enabled ) |
This property holds the docking behavior of QMainWindow
默认值为 AnimatedDocks | AllowTabbedDocks .
This property was introduced in Qt 4.3.
访问函数:
QMainWindow::DockOptions | dockOptions () const |
void | setDockOptions (QMainWindow::DockOptions options ) |
This property holds whether the tab bar for tabbed dockwidgets is set to document mode.
默认为 false。
This property was introduced in Qt 4.5.
访问函数:
bool | documentMode () const |
void | setDocumentMode (bool enabled ) |
另请参阅 QTabBar::documentMode .
size of toolbar icons in this mainwindow.
The default is the default tool bar icon size of the GUI style. Note that the icons used must be at least of this size as the icons are only scaled down.
访问函数:
QSize | iconSize () const |
void | setIconSize (const QSize & iconSize ) |
This property holds the tab shape used for tabbed dock widgets.
默认为 QTabWidget::Rounded .
This property was introduced in Qt 4.5.
访问函数:
QTabWidget::TabShape | tabShape () const |
void | setTabShape (QTabWidget::TabShape tabShape ) |
另请参阅 setTabPosition ().
style of toolbar buttons in this mainwindow.
To have the style of toolbuttons follow the system settings, set this property to Qt::ToolButtonFollowStyle . On Unix, the user settings from the desktop environment will be used. On other platforms, Qt::ToolButtonFollowStyle means icon only.
默认为 Qt::ToolButtonIconOnly .
访问函数:
Qt::ToolButtonStyle | toolButtonStyle () const |
void | setToolButtonStyle (Qt::ToolButtonStyle toolButtonStyle ) |
This property holds whether the window uses the unified title and toolbar look on macOS
Note that the Qt 5 implementation has several limitations compared to Qt 4:
This property was introduced in Qt 5.2.
访问函数:
bool | unifiedTitleAndToolBarOnMac () const |
void | setUnifiedTitleAndToolBarOnMac (bool set ) |
构造 QMainWindow with the given parent and the specified widget flags .
QMainWindow sets the Qt::Window flag itself, and will hence always be created as a top-level widget.
[virtual]
QMainWindow::
~QMainWindow
()
Destroys the main window.
添加给定 dockwidget to the specified area .
添加 dockwidget into the given area in the direction specified by the orientation .
添加 toolbar into the specified area in this main window. The toolbar is placed at the end of the current tool bar block (i.e. line). If the main window already manages toolbar then it will only move the toolbar to area .
另请参阅 insertToolBar (), addToolBarBreak (),和 insertToolBarBreak ().
这是重载函数。
Equivalent of calling addToolBar ( Qt::TopToolBarArea , toolbar )
这是重载函数。
创建 QToolBar object, setting its window title to title , and inserts it into the top toolbar area.
另请参阅 setWindowTitle ().
Adds a toolbar break to the given area after all the other objects that are present.
返回主窗口的中心 Widget。此函数返回零,若中心 Widget 未设置。
另请参阅 setCentralWidget ().
[override virtual protected]
void
QMainWindow::
contextMenuEvent
(
QContextMenuEvent
*
event
)
Reimplemented from QWidget::contextMenuEvent ().
Returns the dock widget area that occupies the specified corner .
另请参阅 setCorner ().
[virtual]
QMenu
*QMainWindow::
createPopupMenu
()
Returns a popup menu containing checkable entries for the toolbars and dock widgets present in the main window. If there are no toolbars and dock widgets present, this function returns a null pointer.
By default, this function is called by the main window when the user activates a context menu, typically by right-clicking on a toolbar or a dock widget.
If you want to create a custom popup menu, reimplement this function and return a newly-created popup menu. Ownership of the popup menu is transferred to the caller.
另请参阅 addDockWidget (), addToolBar (),和 menuBar ().
返回
Qt::DockWidgetArea
for
dockwidget
。若
dockwidget
has not been added to the main window, this function returns
Qt::NoDockWidgetArea
.
另请参阅 addDockWidget (), splitDockWidget (),和 Qt::DockWidgetArea .
[override virtual protected]
bool
QMainWindow::
event
(
QEvent
*
event
)
Reimplemented from QWidget::event ().
[signal]
void
QMainWindow::
iconSizeChanged
(const
QSize
&
iconSize
)
This signal is emitted when the size of the icons used in the window is changed. The new icon size is passed in iconSize .
You can connect this signal to other components to help maintain a consistent appearance for your application.
另请参阅 setIconSize ().
插入 toolbar into the area occupied by the before toolbar so that it appears before it. For example, in normal left-to-right layout operation, this means that toolbar will appear to the left of the toolbar specified by before in a horizontal toolbar area.
另请参阅 insertToolBarBreak (), addToolBar (),和 addToolBarBreak ().
Inserts a toolbar break before the toolbar specified by before .
Returns the menu bar for the main window. This function creates and returns an empty menu bar if the menu bar does not exist.
If you want all windows in a Mac application to share one menu bar, don't use this function to create it, because the menu bar created here will have this QMainWindow as its parent. Instead, you must create a menu bar that does not have a parent, which you can then share among all the Mac windows. Create a parent-less menu bar this way:
另请参阅 setMenuBar ().
Returns the menu bar for the main window. This function returns null if a menu bar hasn't been constructed yet.
This function was introduced in Qt 4.2.
另请参阅 setMenuWidget ().
移除 dockwidget from the main window layout and hides it. Note that the dockwidget is not deleted.
移除 toolbar from the main window layout and hides it. Note that the toolbar is not deleted.
Removes a toolbar break previously inserted before the toolbar specified by before .
Resizes the dock widgets in the list docks to the corresponding size in pixels from the list sizes 。若 orientation is Qt::Horizontal , adjusts the width, otherwise adjusts the height of the dock widgets. The sizes will be adjusted such that the maximum and the minimum sizes are respected and the QMainWindow itself will not be resized. Any additional/missing space is distributed amongst the widgets according to the relative weight of the sizes.
范例:
resizeDocks({blueWidget, yellowWidget}, {20 , 40}, Qt::Horizontal);
If the blue and the yellow widget are nested on the same level they will be resized such that the yellowWidget is twice as big as the blueWidget
If some widgets are grouped in tabs, only one widget per group should be specified. Widgets not in the list might be changed to repect the constraints.
This function was introduced in Qt 5.6.
Restores the state of
dockwidget
if it is created after the call to
restoreState
(). Returns
true
if the state was restored; otherwise returns
false
.
另请参阅 restoreState () 和 saveState ().
Restores the
state
of this mainwindow's toolbars and dockwidgets. Also restores the corner settings too. The
version
number is compared with that stored in
state
. If they do not match, the mainwindow's state is left unchanged, and this function returns
false
; otherwise, the state is restored, and this function returns
true
.
To restore geometry saved using QSettings , you can use code like this:
void MainWindow::readSettings() { QSettings settings("MyCompany", "MyApp"); restoreGeometry(settings.value("myWidget/geometry").toByteArray()); restoreState(settings.value("myWidget/windowState").toByteArray()); }
另请参阅 saveState (), QWidget::saveGeometry (), QWidget::restoreGeometry (),和 restoreDockWidget ().
Saves the current state of this mainwindow's toolbars and dockwidgets. This includes the corner settings which can be set with setCorner ()。 version number is stored as part of the data.
objectName property is used to identify each QToolBar and QDockWidget . You should make sure that this property is unique for each QToolBar and QDockWidget you add to the QMainWindow
To restore the saved state, pass the return value and version number to restoreState ().
To save the geometry when the window closes, you can implement a close event like this:
void MyMainWindow::closeEvent(QCloseEvent *event) { QSettings settings("MyCompany", "MyApp"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState()); QMainWindow::closeEvent(event); }
另请参阅 restoreState (), QWidget::saveGeometry (),和 QWidget::restoreGeometry ().
设置给定 widget to be the main window's central widget.
注意: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.
另请参阅 centralWidget ().
Sets the given dock widget area to occupy the specified corner .
另请参阅 corner ().
Sets the menu bar for the main window to menuBar .
注意: QMainWindow takes ownership of the menuBar pointer and deletes it at the appropriate time.
另请参阅 menuBar ().
Sets the menu bar for the main window to menuBar .
QMainWindow takes ownership of the menuBar pointer and deletes it at the appropriate time.
This function was introduced in Qt 4.2.
另请参阅 menuWidget ().
Sets the status bar for the main window to statusbar .
Setting the status bar to 0 will remove it from the main window. Note that QMainWindow takes ownership of the statusbar pointer and deletes it at the appropriate time.
另请参阅 statusBar ().
Sets the tab position for the given dock widget areas to the specified tabPosition . By default, all dock areas show their tabs at the bottom.
注意: VerticalTabs dock option overrides the tab positions set by this method.
This function was introduced in Qt 4.5.
另请参阅 tabPosition () 和 setTabShape ().
Splits the space covered by the first dock widget into two parts, moves the first dock widget into the first part, and moves the second dock widget into the second part.
orientation specifies how the space is divided: A Qt::Horizontal split places the second dock widget to the right of the first; a Qt::Vertical split places the second dock widget below the first.
注意 : if first is currently in a tabbed docked area, second will be added as a new tab, not as a neighbor of first . This is because a single tab can contain only one dock widget.
注意 : The Qt::LayoutDirection influences the order of the dock widgets in the two parts of the divided area. When right-to-left layout direction is enabled, the placing of the dock widgets will be reversed.
另请参阅 tabifyDockWidget (), addDockWidget (),和 removeDockWidget ().
Returns the status bar for the main window. This function creates and returns an empty status bar if the status bar does not exist.
另请参阅 setStatusBar ().
Returns the tab position for area .
注意: VerticalTabs dock option overrides the tab positions returned by this function.
This function was introduced in Qt 4.5.
另请参阅 setTabPosition () 和 tabShape ().
[signal]
void
QMainWindow::
tabifiedDockWidgetActivated
(
QDockWidget
*
dockWidget
)
This signal is emitted when the tabified dock widget is activated by selecting the tab. The activated dock widget is passed in dockWidget .
This function was introduced in Qt 5.8.
另请参阅 tabifyDockWidget () 和 tabifiedDockWidgets ().
Returns the dock widgets that are tabified together with dockwidget .
This function was introduced in Qt 4.5.
另请参阅 tabifyDockWidget ().
Moves second dock widget on top of first dock widget, creating a tabbed docked area in the main window.
另请参阅 tabifiedDockWidgets ().
Removes the central widget from this main window.
The ownership of the removed widget is passed to the caller.
This function was introduced in Qt 5.2.
返回
Qt::ToolBarArea
for
toolbar
。若
toolbar
has not been added to the main window, this function returns
Qt::NoToolBarArea
.
另请参阅 addToolBar (), addToolBarBreak (),和 Qt::ToolBarArea .
Returns whether there is a toolbar break before the toolbar .
另请参阅 addToolBarBreak () 和 insertToolBarBreak ().
[signal]
void
QMainWindow::
toolButtonStyleChanged
(
Qt::ToolButtonStyle
toolButtonStyle
)
This signal is emitted when the style used for tool buttons in the window is changed. The new style is passed in toolButtonStyle .
You can connect this signal to other components to help maintain a consistent appearance for your application.
另请参阅 setToolButtonStyle ().