QMainWindow 类提供主应用程序窗口。 更多...
头: | #include <QMainWindow> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
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 (const 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 ) |
virtual void | contextMenuEvent (QContextMenuEvent * event ) override |
virtual bool | event (QEvent * event ) override |
主窗口提供用于构建应用程序用户界面的框架。Qt 有 QMainWindow 及其 相关类 用于主窗口管理。QMainWindow 有自己的布局,可以添加 QToolBar s, QDockWidget s, a QMenuBar ,和 QStatusBar 。布局具有可以被任何种类 Widget 占据的中心区域。见以下布局图像。
中心小部件通常是标准 Qt Widget,譬如
QTextEdit
或
QGraphicsView
。自定义 Widget 还可以用于高级应用程序。设置中心 Widget 采用
setCentralWidget()
.
主窗口有 SDI (单文档界面) 或 MDI (多文档界面)。在 Qt 中创建 MDI 应用程序通过使用 QMdiArea 作为中心 Widget。
现在,我们将研究可以被添加到主窗口的每一其它 Widget。举例说明如何创建和添加它们。
Qt 实现菜单在 QMenu 和 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 ,和 菜单范例 .
This enum contains flags that specify the docking behavior of QMainWindow .
常量 | 值 | 描述 |
---|---|---|
QMainWindow::AnimatedDocks
|
0x01
|
等同于 animated 特性。 |
QMainWindow::AllowNestedDocks
|
0x02
|
等同于 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.
DockOptions 类型是 typedef 对于 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 ().
访问函数:
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.
Setting this property is identical to setting the AllowNestedDocks option using setDockOptions ().
访问函数:
bool | isDockNestingEnabled () const |
void | setDockNestingEnabled (bool enabled ) |
This property holds the docking behavior of QMainWindow
默认值为 AnimatedDocks | AllowTabbedDocks .
访问函数:
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。
访问函数:
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 .
访问函数:
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:
访问函数:
bool | unifiedTitleAndToolBarOnMac () const |
void | setUnifiedTitleAndToolBarOnMac (bool set ) |
[explicit]
QMainWindow::
QMainWindow
(
QWidget
*
parent
= nullptr,
Qt::WindowFlags
flags
= Qt::WindowFlags())
Constructs a QMainWindow with the given parent 和指定小部件 flags .
QMainWindow sets the Qt::Window flag itself, and will hence always be created as a top-level widget.
[virtual noexcept]
QMainWindow::
~QMainWindow
()
销毁主窗口。
添加给定 dockwidget 到指定 area .
添加 dockwidget 进给定 area in the direction specified by the orientation .
添加 toolbar 到指定 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.
Returns the central widget for the main window. This function returns
nullptr
if the central widget has not been set.
另请参阅 setCentralWidget ().
[override virtual protected]
void
QMainWindow::
contextMenuEvent
(
QContextMenuEvent
*
event
)
重实现: QWidget::contextMenuEvent (QContextMenuEvent *event).
Returns the dock widget area that occupies the specified 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
nullptr
.
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
)
重实现: QWidget::event (QEvent *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:
QMenuBar *menuBar = new QMenuBar(nullptr);
另请参阅 setMenuBar ().
Returns the menu bar for the main window. This function returns null if a menu bar hasn't been constructed yet.
另请参阅 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 respect the constraints.
Restores the state of
dockwidget
if it is created after the call to
restoreState
()。返回
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
.
要还原保存几何体使用 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 ().
Sets the given dock widget area to occupy the specified corner .
另请参阅 corner ().
把主窗口菜单栏设为 menuBar .
注意: QMainWindow 拥有所有权对于 menuBar 指针和在适当时删除它。
另请参阅 menuBar ().
把主窗口菜单栏设为 menuBar .
QMainWindow 拥有所有权对于 menuBar 指针和在适当时删除它。
另请参阅 menuWidget ().
把主窗口状态栏设为 statusbar .
Setting the status bar to
nullptr
will remove it from the main window. Note that
QMainWindow
拥有所有权对于
statusbar
指针和在适当时删除它。
另请参阅 statusBar ().
Sets the tab position for the given dock widget areas 到指定 tabPosition . By default, all dock areas show their tabs at the bottom.
注意: The VerticalTabs dock option overrides the tab positions set by this method.
另请参阅 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 停放选项覆盖由此函数返回的选项卡位置。
另请参阅 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 .
另请参阅 tabifyDockWidget () 和 tabifiedDockWidgets ().
Returns the dock widgets that are tabified together with dockwidget .
另请参阅 tabifyDockWidget ().
移动 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.
返回
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 ().