Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QMainWindow Class

TheQMainWindow class provides a main application window.More...

Header:#include <QMainWindow>
Inherits:QWidget

Public Types

enumDockOption { AnimatedDocks, AllowNestedDocks, AllowTabbedDocks, ForceTabbedDocks, VerticalTabs }
flagsDockOptions

Properties

Public Functions

QMainWindow(QWidget * parent = 0, Qt::WindowFlags flags = 0)
~QMainWindow()
voidaddDockWidget(Qt::DockWidgetArea area, QDockWidget * dockwidget)
voidaddDockWidget(Qt::DockWidgetArea area, QDockWidget * dockwidget, Qt::Orientation orientation)
voidaddToolBar(Qt::ToolBarArea area, QToolBar * toolbar)
voidaddToolBar(QToolBar * toolbar)
QToolBar *addToolBar(const QString & title)
voidaddToolBarBreak(Qt::ToolBarArea area = Qt::TopToolBarArea)
QWidget *centralWidget() const
Qt::DockWidgetAreacorner(Qt::Corner corner) const
virtual QMenu *createPopupMenu()
DockOptionsdockOptions() const
Qt::DockWidgetAreadockWidgetArea(QDockWidget * dockwidget) const
booldocumentMode() const
QSizeiconSize() const
voidinsertToolBar(QToolBar * before, QToolBar * toolbar)
voidinsertToolBarBreak(QToolBar * before)
boolisAnimated() const
boolisDockNestingEnabled() const
QMenuBar *menuBar() const
QWidget *menuWidget() const
voidremoveDockWidget(QDockWidget * dockwidget)
voidremoveToolBar(QToolBar * toolbar)
voidremoveToolBarBreak(QToolBar * before)
boolrestoreDockWidget(QDockWidget * dockwidget)
boolrestoreState(const QByteArray & state, int version = 0)
QByteArraysaveState(int version = 0) const
voidsetCentralWidget(QWidget * widget)
voidsetCorner(Qt::Corner corner, Qt::DockWidgetArea area)
voidsetDockOptions(DockOptions options)
voidsetDocumentMode(bool enabled)
voidsetIconSize(const QSize & iconSize)
voidsetMenuBar(QMenuBar * menuBar)
voidsetMenuWidget(QWidget * menuBar)
voidsetStatusBar(QStatusBar * statusbar)
voidsetTabPosition(Qt::DockWidgetAreas areas, QTabWidget::TabPosition tabPosition)
voidsetTabShape(QTabWidget::TabShape tabShape)
voidsetToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)
voidsetUnifiedTitleAndToolBarOnMac(bool set)
voidsplitDockWidget(QDockWidget * first, QDockWidget * second, Qt::Orientation orientation)
QStatusBar *statusBar() const
QTabWidget::TabPositiontabPosition(Qt::DockWidgetArea area) const
QTabWidget::TabShapetabShape() const
QList<QDockWidget *>tabifiedDockWidgets(QDockWidget * dockwidget) const
voidtabifyDockWidget(QDockWidget * first, QDockWidget * second)
Qt::ToolBarAreatoolBarArea(QToolBar * toolbar) const
booltoolBarBreak(QToolBar * toolbar) const
Qt::ToolButtonStyletoolButtonStyle() const
boolunifiedTitleAndToolBarOnMac() const

Public Slots

voidsetAnimated(bool enabled)
voidsetDockNestingEnabled(bool enabled)
  • 19 public slots inherited fromQWidget
  • 1 public slot inherited fromQObject

Signals

voidiconSizeChanged(const QSize & iconSize)
voidtoolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)

Reimplemented Protected Functions

virtual voidcontextMenuEvent(QContextMenuEvent * event)
virtual boolevent(QEvent * event)
  • 37 protected functions inherited fromQWidget
  • 8 protected functions inherited fromQObject
  • 1 protected function inherited fromQPaintDevice

Additional Inherited Members

  • 4 static public members inherited fromQWidget
  • 7 static public members inherited fromQObject
  • 37 protected functions inherited fromQWidget
  • 8 protected functions inherited fromQObject
  • 1 protected function inherited fromQPaintDevice
  • 1 protected slot inherited fromQWidget

Detailed Description

TheQMainWindow class provides a main application window.

Qt Main Window Framework

A main window provides a framework for building an application's user interface. Qt hasQMainWindow and itsrelated classes for main window management.QMainWindow has its own layout to which you can addQToolBars,QDockWidgets, aQMenuBar, and aQStatusBar. The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below.

Note:Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.

Creating Main Window Components

A central widget will typically be a standard Qt widget such as aQTextEdit or aQGraphicsView. Custom widgets can also be used for advanced applications. You set the central widget withsetCentralWidget().

Main windows have either a single (SDI) or multiple (MDI) document interface. You create MDI applications in Qt by using aQMdiArea as the central widget.

We will now examine each of the other widgets that can be added to a main window. We give examples on how to create and add them.

Creating Menus

Qt implements menus inQMenu andQMainWindow keeps them in aQMenuBar.QActions are added to the menus, which display them as menu items.

You can add new menus to the main window's menu bar by callingmenuBar(), which returns theQMenuBar for the window, and then add a menu withQMenuBar::addMenu().

QMainWindow comes with a default menu bar, but you can also set one yourself withsetMenuBar(). If you wish to implement a custom menu bar (i.e., not use theQMenuBar widget), you can set it withsetMenuWidget().

An example of how to create menus follows:

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

ThecreatePopupMenu() function creates popup menus when the main window receives context menu events. The default implementation generates a menu with the checkable actions from the dock widgets and toolbars. You can reimplementcreatePopupMenu() for a custom menu.

Creating Toolbars

Toolbars are implemented in theQToolBar class. You add a toolbar to a main window withaddToolBar().

You control the initial position of toolbars by assigning them to a specificQt::ToolBarArea. You can split an area by inserting a toolbar break - think of this as a line break in text editing - withaddToolBarBreak() orinsertToolBarBreak(). You can also restrict placement by the user withQToolBar::setAllowedAreas() andQToolBar::setMovable().

The size of toolbar icons can be retrieved withiconSize(). The sizes are platform dependent; you can set a fixed size withsetIconSize(). You can alter the appearance of all tool buttons in the toolbars withsetToolButtonStyle().

An example of toolbar creation follows:

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

Creating Dock Widgets

Dock widgets are implemented in theQDockWidget class. A dock widget is a window that can be docked into the main window. You add dock widgets to a main window withaddDockWidget().

There are four dock widget areas as given by theQt::DockWidgetArea enum: left, right, top, and bottom. You can specify which dock widget area that should occupy the corners where the areas overlap withsetCorner(). By default each area can only contain one row (vertical or horizontal) of dock widgets, but if you enable nesting withsetDockNestingEnabled(), dock widgets can be added in either direction.

Two dock widgets may also be stacked on top of each other. AQTabBar is then used to select which of the widgets that should be displayed.

We give an example of how to create and add dock widgets to a main window:

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

The Status Bar

You can set a status bar withsetStatusBar(), but one is created the first timestatusBar() (which returns the main window's status bar) is called. SeeQStatusBar for information on how to use it.

Storing State

QMainWindow can store the state of its layout withsaveState(); it can later be retrieved withrestoreState(). It is the position and size (relative to the size of the main window) of the toolbars and dock widgets that are stored.

See alsoQMenuBar,QToolBar,QStatusBar,QDockWidget,Application Example,Dock Widgets Example,MDI Example,SDI Example, andMenus Example.

Member Type Documentation

enum QMainWindow::DockOption
flags QMainWindow::DockOptions

This enum contains flags that specify the docking behavior ofQMainWindow.

ConstantValueDescription
QMainWindow::AnimatedDocks0x01Identical to theanimated property.
QMainWindow::AllowNestedDocks0x02Identical to thedockNestingEnabled property.
QMainWindow::AllowTabbedDocks0x04The 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::ForceTabbedDocks0x08Each 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::VerticalTabs0x10The 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 alsosetTabPosition().

These options only control how dock widgets may be dropped in aQMainWindow. 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 forQFlags<DockOption>. It stores an OR combination of DockOption values.

Property Documentation

animated :bool

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 causesQMainWindow 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 theAnimatedDocks option usingsetDockOptions().

This property was introduced in Qt 4.2.

Access functions:

boolisAnimated() const
voidsetAnimated(bool enabled)

dockNestingEnabled :bool

This property holds whether docks can be nested.

If this property is 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 theAllowNestedDocks option usingsetDockOptions().

This property was introduced in Qt 4.2.

Access functions:

boolisDockNestingEnabled() const
voidsetDockNestingEnabled(bool enabled)

dockOptions :DockOptions

This property holds the docking behavior of QMainWindow.

The default value isAnimatedDocks |AllowTabbedDocks.

This property was introduced in Qt 4.3.

Access functions:

DockOptionsdockOptions() const
voidsetDockOptions(DockOptions options)

documentMode :bool

This property holds whether the tab bar for tabbed dockwidgets is set to document mode.

The default is false.

This property was introduced in Qt 4.5.

Access functions:

booldocumentMode() const
voidsetDocumentMode(bool enabled)

See alsoQTabBar::documentMode.

iconSize :QSize

This property holds 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.

Access functions:

QSizeiconSize() const
voidsetIconSize(const QSize & iconSize)

tabShape :QTabWidget::TabShape

This property holds the tab shape used for tabbed dock widgets.

The default isQTabWidget::Rounded.

This property was introduced in Qt 4.5.

Access functions:

QTabWidget::TabShapetabShape() const
voidsetTabShape(QTabWidget::TabShape tabShape)

See alsosetTabPosition().

toolButtonStyle :Qt::ToolButtonStyle

This property holds style of toolbar buttons in this mainwindow.

The default isQt::ToolButtonIconOnly.

Access functions:

Qt::ToolButtonStyletoolButtonStyle() const
voidsetToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)

unifiedTitleAndToolBarOnMac :bool

This property holds whether the window uses the unified title and toolbar look on Mac OS X.

This property is false by default and only has any effect on Mac OS X 10.4 or higher.

If set to true, then the top toolbar area is replaced with a Carbon HIToolbar or a Cocoa NSToolbar (depending on whether Qt was built with Carbon or Cocoa). All toolbars in the top toolbar area and any toolbars added afterwards are moved to that. This means a couple of things.

  • QToolBars in this toolbar area are not movable and you cannot drag other toolbars to it
  • Toolbar breaks are not respected or preserved
  • Any custom widgets in the toolbar will not be shown if the toolbar becomes too small (only actions will be shown)
  • Before Qt 4.5, if you calledshowFullScreen() on the main window, theQToolbar would disappear since it is considered to be part of the title bar. Qt 4.5 and up will now work around this by pulling the toolbars out and back into the regular toolbar and vice versa when you swap out.

Setting this back to false will remove these restrictions.

TheQt::WA_MacBrushedMetal attribute takes precedence over this property.

This property was introduced in Qt 4.3.

Access functions:

boolunifiedTitleAndToolBarOnMac() const
voidsetUnifiedTitleAndToolBarOnMac(bool set)

Member Function Documentation

QMainWindow::QMainWindow(QWidget * parent = 0,Qt::WindowFlags flags = 0)

Constructs aQMainWindow with the givenparent and the specified widgetflags.

QMainWindow sets theQt::Window flag itself, and will hence always be created as a top-level widget.

QMainWindow::~QMainWindow()

Destroys the main window.

void QMainWindow::addDockWidget(Qt::DockWidgetArea area,QDockWidget * dockwidget)

Adds the givendockwidget to the specifiedarea.

void QMainWindow::addDockWidget(Qt::DockWidgetArea area,QDockWidget * dockwidget,Qt::Orientation orientation)

Addsdockwidget into the givenarea in the direction specified by theorientation.

void QMainWindow::addToolBar(Qt::ToolBarArea area,QToolBar * toolbar)

Adds thetoolbar into the specifiedarea in this main window. Thetoolbar is placed at the end of the current tool bar block (i.e. line). If the main window already managestoolbar then it will only move the toolbar toarea.

See alsoinsertToolBar(),addToolBarBreak(), andinsertToolBarBreak().

void QMainWindow::addToolBar(QToolBar * toolbar)

This is an overloaded function.

Equivalent of callingaddToolBar(Qt::TopToolBarArea,toolbar)

QToolBar * QMainWindow::addToolBar(constQString & title)

This is an overloaded function.

Creates aQToolBar object, setting its window title totitle, and inserts it into the top toolbar area.

See alsosetWindowTitle().

void QMainWindow::addToolBarBreak(Qt::ToolBarArea area = Qt::TopToolBarArea)

Adds a toolbar break to the givenarea after all the other objects that are present.

QWidget * QMainWindow::centralWidget() const

Returns the central widget for the main window. This function returns zero if the central widget has not been set.

See alsosetCentralWidget().

[virtual protected]void QMainWindow::contextMenuEvent(QContextMenuEvent * event)

Reimplemented fromQWidget::contextMenuEvent().

Qt::DockWidgetArea QMainWindow::corner(Qt::Corner corner) const

Returns the dock widget area that occupies the specifiedcorner.

See alsosetCorner().

[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.

See alsoaddDockWidget(),addToolBar(), andmenuBar().

Qt::DockWidgetArea QMainWindow::dockWidgetArea(QDockWidget * dockwidget) const

Returns theQt::DockWidgetArea fordockwidget. Ifdockwidget has not been added to the main window, this function returnsQt::NoDockWidgetArea.

See alsoaddDockWidget(),splitDockWidget(), andQt::DockWidgetArea.

[virtual protected]bool QMainWindow::event(QEvent * event)

Reimplemented fromQObject::event().

[signal]void QMainWindow::iconSizeChanged(constQSize & iconSize)

This signal is emitted when the size of the icons used in the window is changed. The new icon size is passed iniconSize.

You can connect this signal to other components to help maintain a consistent appearance for your application.

See alsosetIconSize().

void QMainWindow::insertToolBar(QToolBar * before,QToolBar * toolbar)

Inserts thetoolbar into the area occupied by thebefore toolbar so that it appears before it. For example, in normal left-to-right layout operation, this means thattoolbar will appear to the left of the toolbar specified bybefore in a horizontal toolbar area.

See alsoinsertToolBarBreak(),addToolBar(), andaddToolBarBreak().

void QMainWindow::insertToolBarBreak(QToolBar * before)

Inserts a toolbar break before the toolbar specified bybefore.

QMenuBar * QMainWindow::menuBar() const

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 thisQMainWindow 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=newQMenuBar(0);

See alsosetMenuBar().

QWidget * QMainWindow::menuWidget() const

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.

See alsosetMenuWidget().

void QMainWindow::removeDockWidget(QDockWidget * dockwidget)

Removes thedockwidget from the main window layout and hides it. Note that thedockwidget isnot deleted.

void QMainWindow::removeToolBar(QToolBar * toolbar)

Removes thetoolbar from the main window layout and hides it. Note that thetoolbar isnot deleted.

void QMainWindow::removeToolBarBreak(QToolBar * before)

Removes a toolbar break previously inserted before the toolbar specified bybefore.

bool QMainWindow::restoreDockWidget(QDockWidget * dockwidget)

Restores the state ofdockwidget if it is created after the call torestoreState(). Returns true if the state was restored; otherwise returns false.

See alsorestoreState() andsaveState().

bool QMainWindow::restoreState(constQByteArray & state,int version = 0)

Restores thestate of this mainwindow's toolbars and dockwidgets. Theversion number is compared with that stored instate. If they do not match, the mainwindow's state is left unchanged, and this function returnsfalse; otherwise, the state is restored, and this function returnstrue.

To restore geometry saved usingQSettings, 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());}

See alsosaveState(),QWidget::saveGeometry(),QWidget::restoreGeometry(), andrestoreDockWidget().

QByteArray QMainWindow::saveState(int version = 0) const

Saves the current state of this mainwindow's toolbars and dockwidgets. Theversion number is stored as part of the data.

TheobjectName property is used to identify eachQToolBar andQDockWidget. You should make sure that this property is unique for eachQToolBar andQDockWidget you add to theQMainWindow

To restore the saved state, pass the return value andversion number torestoreState().

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);}

See alsorestoreState(),QWidget::saveGeometry(), andQWidget::restoreGeometry().

void QMainWindow::setCentralWidget(QWidget * widget)

Sets the givenwidget to be the main window's central widget.

Note:QMainWindow takes ownership of thewidget pointer and deletes it at the appropriate time.

See alsocentralWidget().

void QMainWindow::setCorner(Qt::Corner corner,Qt::DockWidgetArea area)

Sets the given dock widgetarea to occupy the specifiedcorner.

See alsocorner().

void QMainWindow::setMenuBar(QMenuBar * menuBar)

Sets the menu bar for the main window tomenuBar.

Note:QMainWindow takes ownership of themenuBar pointer and deletes it at the appropriate time.

See alsomenuBar().

void QMainWindow::setMenuWidget(QWidget * menuBar)

Sets the menu bar for the main window tomenuBar.

QMainWindow takes ownership of themenuBar pointer and deletes it at the appropriate time.

This function was introduced in Qt 4.2.

See alsomenuWidget().

void QMainWindow::setStatusBar(QStatusBar * statusbar)

Sets the status bar for the main window tostatusbar.

Setting the status bar to 0 will remove it from the main window. Note thatQMainWindow takes ownership of thestatusbar pointer and deletes it at the appropriate time.

See alsostatusBar().

void QMainWindow::setTabPosition(Qt::DockWidgetAreas areas,QTabWidget::TabPosition tabPosition)

Sets the tab position for the given dock widgetareas to the specifiedtabPosition. By default, all dock areas show their tabs at the bottom.

Note:TheVerticalTabs dock option overrides the tab positions set by this method.

This function was introduced in Qt 4.5.

See alsotabPosition() andsetTabShape().

void QMainWindow::splitDockWidget(QDockWidget * first,QDockWidget * second,Qt::Orientation orientation)

Splits the space covered by thefirst dock widget into two parts, moves thefirst dock widget into the first part, and moves thesecond dock widget into the second part.

Theorientation specifies how the space is divided: AQt::Horizontal split places the second dock widget to the right of the first; aQt::Vertical split places the second dock widget below the first.

Note: iffirst is currently in a tabbed docked area,second will be added as a new tab, not as a neighbor offirst. This is because a single tab can contain only one dock widget.

Note: TheQt::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.

See alsotabifyDockWidget(),addDockWidget(), andremoveDockWidget().

QStatusBar * QMainWindow::statusBar() const

Returns the status bar for the main window. This function creates and returns an empty status bar if the status bar does not exist.

See alsosetStatusBar().

QTabWidget::TabPosition QMainWindow::tabPosition(Qt::DockWidgetArea area) const

Returns the tab position forarea.

Note:TheVerticalTabs dock option overrides the tab positions returned by this function.

This function was introduced in Qt 4.5.

See alsosetTabPosition() andtabShape().

QList<QDockWidget *> QMainWindow::tabifiedDockWidgets(QDockWidget * dockwidget) const

Returns the dock widgets that are tabified together withdockwidget.

This function was introduced in Qt 4.5.

See alsotabifyDockWidget().

void QMainWindow::tabifyDockWidget(QDockWidget * first,QDockWidget * second)

Movessecond dock widget on top offirst dock widget, creating a tabbed docked area in the main window.

See alsotabifiedDockWidgets().

Qt::ToolBarArea QMainWindow::toolBarArea(QToolBar * toolbar) const

Returns theQt::ToolBarArea fortoolbar. Iftoolbar has not been added to the main window, this function returnsQt::NoToolBarArea.

See alsoaddToolBar(),addToolBarBreak(), andQt::ToolBarArea.

bool QMainWindow::toolBarBreak(QToolBar * toolbar) const

Returns whether there is a toolbar break before thetoolbar.

See alsoaddToolBarBreak() andinsertToolBarBreak().

[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 intoolButtonStyle.

You can connect this signal to other components to help maintain a consistent appearance for your application.

See alsosetToolButtonStyle().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp