The QMainWindow 类提供主应用程序窗口。 更多...
| 头: | #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 |
The QMainWindow 类提供主应用程序窗口。
主窗口提供用于构建应用程序用户界面的框架。Qt 有 QMainWindow 及其 相关类 用于主窗口管理。 QMainWindow 有自己的布局,可以添加 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 保持它们在 QMenuBar . QAction 被添加到菜单,将它们显示为菜单项。
可以把新菜单添加到主窗口的菜单栏通过调用
menuBar()
,其返回
QMenuBar
为窗口,然后添加菜单采用
QMenuBar::addMenu
().
QMainWindow
带有默认菜单栏,但也可以自己设置一个采用
setMenuBar()
。若希望实现自定义菜单栏 (即:不使用
QMenuBar
Widget),可以设置它采用
setMenuWidget()
.
以下是如何创建菜单的范例:
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
The
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()
.
有 4 个停放 Widget 区域,给出通过
Qt::DockWidgetArea
枚举:左侧、右侧、顶部及底部。可以指定停放 Widget 区域应该占据哪个区域重叠角落采用
setCorner()
。默认情况下,每个区域只能包含一行 (垂直或水平) 停放 Widget,但若启用嵌套采用
setDockNestingEnabled()
,可以将停放 Widget 添加到任一方向。
2 停放 Widget 也可以彼此堆叠在一起。 QTabBar 然后用于选择应显示哪个 Widget。
我们给出在主窗口如何创建和添加停放 Widget 的范例:
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
可以存储其布局的状态采用
saveState()
;稍后,可以检索它采用
restoreState()
。它是存储的工具栏和停放 Widget 的位置和尺寸 (相对于主窗口的尺寸)。
另请参阅 QMenuBar , QToolBar , QStatusBar , QDockWidget , 应用程序范例 , 停放 Widget 范例 , MDI 范例 , SDI 范例 ,和 菜单范例 .
此枚举包含的标志指定停放行为对于 QMainWindow .
| 常量 | 值 | 描述 |
|---|---|---|
QMainWindow::AnimatedDocks
|
0x01
|
等同于 animated 特性。 |
QMainWindow::AllowNestedDocks
|
0x02
|
等同于 dockNestingEnabled 特性。 |
QMainWindow::AllowTabbedDocks
|
0x04
|
用户可以将某一停放 Widget 掉落到另一停放 widget 的 "顶部"。这 2 Widget 堆叠并出现用于选择哪个停放 Widget 可见的选项卡栏。 |
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.
该枚举在 Qt 4.3 引入或被修改。
DockOptions 类型是 typedef 对于 QFlags <DockOption>。它存储 DockOption 值的 OR (或) 组合。
此特性保持是否动画操纵停放 Widget 和工具栏
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.
设置此特性等同于设置 AnimatedDocks 选项使用 setDockOptions ().
该特性在 Qt 4.2 引入。
访问函数:
| bool | isAnimated () const |
| void | setAnimated (bool enabled ) |
此特性保持是否可以嵌套停放
若此特性为
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.
设置此特性等同于设置 AllowNestedDocks 选项使用 setDockOptions ().
该特性在 Qt 4.2 引入。
访问函数:
| bool | isDockNestingEnabled () const |
| void | setDockNestingEnabled (bool enabled ) |
此特性保持停放行为对于 QMainWindow
默认值为 AnimatedDocks | AllowTabbedDocks .
该特性在 Qt 4.3 引入。
访问函数:
| QMainWindow::DockOptions | dockOptions () const |
| void | setDockOptions (QMainWindow::DockOptions options ) |
此特性保持是否将用于选项卡式停放 Widget 的选项卡栏,设为文档模式。
默认为 false。
该特性在 Qt 4.5 引入。
访问函数:
| bool | documentMode () const |
| void | setDocumentMode (bool enabled ) |
另请参阅 QTabBar::documentMode .
此主窗口工具栏图标的大小。
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 ) |
此特性保持用于选项卡式停放 Widget 的选项卡形状。
默认为 QTabWidget::Rounded .
该特性在 Qt 4.5 引入。
访问函数:
| QTabWidget::TabShape | tabShape () const |
| void | setTabShape (QTabWidget::TabShape tabShape ) |
另请参阅 setTabPosition ().
在此主窗口中工具栏按钮的样式。
要让工具按钮样式遵循系统设置,将此特性设为 Qt::ToolButtonFollowStyle 。在 Unix,将使用来自桌面环境的用户设置。在其它平台, Qt::ToolButtonFollowStyle 意味着仅图标。
默认为 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:
该特性在 Qt 5.2 引入。
访问函数:
| bool | unifiedTitleAndToolBarOnMac () const |
| void | setUnifiedTitleAndToolBarOnMac (bool set ) |
构造 QMainWindow 采用给定 parent 和指定小部件 flags .
QMainWindow 设置 Qt::Window 标志自身,且因此将始终创建作为顶层 Widget。
[虚拟]
QMainWindow::
~QMainWindow
()
销毁主窗口。
添加给定 dockwidget 到指定 area .
添加 dockwidget 进给定 area 按方向指定通过 orientation .
添加 toolbar 到指定 area 在此主窗口。 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 对象,将其窗口标题设为 title ,并把它插入顶部工具栏区域。
另请参阅 setWindowTitle ().
把工具栏中断添加到给定 area 在呈现所有其它对象后。
返回主窗口的中心 Widget。此函数返回零,若中心 Widget 未设置。
另请参阅 setCentralWidget ().
[override virtual protected]
void
QMainWindow::
contextMenuEvent
(
QContextMenuEvent
*
event
)
重实现自 QWidget::contextMenuEvent ().
返回的停放 Widget 区域占据指定 corner .
另请参阅 setCorner ().
[虚拟]
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
尚未被添加到主窗口,此函数返回
Qt::NoDockWidgetArea
.
另请参阅 addDockWidget (), splitDockWidget (),和 Qt::DockWidgetArea .
[override virtual protected]
bool
QMainWindow::
event
(
QEvent
*
event
)
重实现自 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 ().
插入工具栏中断,前于工具栏指定通过 before .
返回用于主窗口的菜单栏。此函数创建并返回空菜单栏,若菜单栏不存在。
若想要 Mac 应用程序中的所有窗口共享某个菜单栏,就不要使用此函数来创建菜单栏,因为这里创建的菜单栏将拥有此 QMainWindow 作为其父级。取而代之,必须创建没有父级的菜单栏,然后就可以在所有 Mac 窗口之间共享。此方式创建无父级的菜单栏:
QMenuBar *menuBar = new QMenuBar(0);
另请参阅 setMenuBar ().
Returns the menu bar for the main window. This function returns null if a menu bar hasn't been constructed yet.
该函数在 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.
该函数在 Qt 5.6 引入。
还原状态为
dockwidget
若创建它后于调用
restoreState
()。返回
true
若状态被还原;否则返回
false
.
另请参阅 restoreState () 和 saveState ().
还原
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
.
要还原保存几何体使用 QSettings ,可以使用的代码像这样:
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.
The 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 ().
要在窗口关闭时保存几何体,可以实现像这样的关闭事件:
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 成为主窗口的中心小部件。
注意: QMainWindow 拥有所有权对于 widget 指针和在适当时删除它。
另请参阅 centralWidget ().
设置给定停放小部件 area 以占据指定 corner .
另请参阅 corner ().
把主窗口菜单栏设为 menuBar .
注意: QMainWindow 拥有所有权对于 menuBar 指针和在适当时删除它。
另请参阅 menuBar ().
把主窗口菜单栏设为 menuBar .
QMainWindow 拥有所有权对于 menuBar 指针和在适当时删除它。
该函数在 Qt 4.2 引入。
另请参阅 menuWidget ().
把主窗口状态栏设为 statusbar .
Setting the status bar to 0 will remove it from the main window. Note that QMainWindow 拥有所有权对于 statusbar 指针和在适当时删除它。
另请参阅 statusBar ().
设置选项卡位置为给定停放小部件 areas 到指定 tabPosition 。默认情况下,所有停放区域都在底部展示其标签。
注意: The VerticalTabs 停放选项覆盖由此方法设置的选项卡位置。
该函数在 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.
The 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.
注意 : 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 ().
返回用于主窗口的状态栏。此函数创建并返回空状态栏,若状态栏不存在。
另请参阅 setStatusBar ().
返回选项卡位置为 area .
注意: The VerticalTabs 停放选项覆盖由此函数返回的选项卡位置。
该函数在 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 .
该函数在 Qt 5.8 引入。
另请参阅 tabifyDockWidget () 和 tabifiedDockWidgets ().
Returns the dock widgets that are tabified together with dockwidget .
该函数在 Qt 4.5 引入。
另请参阅 tabifyDockWidget ().
移动 second dock widget on top of first dock widget, creating a tabbed docked area in the main window.
另请参阅 tabifiedDockWidgets ().
移除此主窗口的中心 Widget。
移除 Widget 的所有权会传递给调用者。
该函数在 Qt 5.2 引入。
返回
Qt::ToolBarArea
for
toolbar
。若
toolbar
尚未被添加到主窗口,此函数返回
Qt::NoToolBarArea
.
另请参阅 addToolBar (), addToolBarBreak (),和 Qt::ToolBarArea .
返回是否有工具栏中断先于 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 ().