📄 q3mainwindow.cpp
字号:
\compat Main windows are most often used to provide menus, toolbars and a status bar around a large central widget, such as a text edit, drawing canvas or QWorkspace (for MDI applications). Q3MainWindow is usually subclassed since this makes it easier to encapsulate the central widget, menus and toolbars as well as the window's state. Subclassing makes it possible to create the slots that are called when the user clicks menu items or toolbar buttons. We'll briefly review adding menu items and toolbar buttons then describe the facilities of Q3MainWindow itself. \code Q3MainWindow *mw = new Q3MainWindow; QTextEdit *edit = new QTextEdit(mw, "editor"); edit->setFocus(); mw->setWindowTitle("Main Window"); mw->setCentralWidget(edit); mw->show(); \endcode Q3MainWindows may be created in their own right as shown above. The central widget is set with setCentralWidget(). Popup menus can be added to the default menu bar, widgets can be added to the status bar, toolbars and dock windows can be added to any of the dock areas. The main window will take care of the dock areas, and the geometry of the central widget, but all other aspects of the central widget are left to you. Q3MainWindow automatically detects the creation of a menu bar or status bar if you specify the Q3MainWindow as parent, or you can use the provided menuBar() and statusBar() functions. The functions menuBar() and statusBar() create a suitable widget if one doesn't exist, and update the window's layout to make space. New dock windows and toolbars can be added to a Q3MainWindow using addDockWindow(). Qt::Dock windows can be moved using moveDockWindow() and removed with removeDockWindow(). Q3MainWindow allows default dock window (toolbar) docking in all its dock areas (\c Top, \c Left, \c Right, \c Bottom). You can use setDockEnabled() to enable and disable docking areas for dock windows. When adding or moving dock windows you can specify their 'edge' (dock area). The currently available edges are: \c Top, \c Left, \c Right, \c Bottom, \c Minimized (effectively a 'hidden' dock area) and \c TornOff (floating). See \l Qt::Dock for an explanation of these areas. Note that the *ToolBar functions are included for backward compatibility; all new code should use the *DockWindow functions. QToolbar is a subclass of Q3DockWindow so all functions that work with dock windows work on toolbars in the same way. \target dwm If the user clicks the close button, then the dock window is hidden. A dock window can be hidden or unhidden by the user by right clicking a dock area and clicking the name of the relevant dock window on the pop up dock window menu. This menu lists the names of every dock window; visible dock windows have a tick beside their names. The dock window menu is created automatically as required by createDockWindowMenu(). Since it may not always be appropriate for a dock window to appear on this menu the setAppropriate() function is used to inform the main window whether or not the dock window menu should include a particular dock window. Double clicking a dock window handle (usually on the left-hand side of the dock window) undocks (floats) the dock window. Double clicking a floating dock window's title bar will dock the floating dock window. (See also \l{Q3MainWindow::DockWindows}.) Some functions change the appearance of a Q3MainWindow globally: \list \i Q3DockWindow::setHorizontalStretchable() and Q3DockWindow::setVerticalStretchable() are used to make specific dock windows or toolbars stretchable. \i setUsesBigPixmaps() is used to set whether tool buttons should draw small or large pixmaps (see QIcon for more information). \i setUsesTextLabel() is used to set whether tool buttons should display a textual label in addition to pixmaps (see QToolButton for more information). \endlist The user can drag dock windows into any enabled docking area. Qt::Dock windows can also be dragged \e within a docking area, for example to rearrange the order of some toolbars. Qt::Dock windows can also be dragged outside any docking area (undocked or 'floated'). Being able to drag dock windows can be enabled (the default) and disabled using setDockWindowsMovable(). The \c Minimized edge is a hidden dock area. If this dock area is enabled the user can hide (minimize) a dock window or show (restore) a minimized dock window by clicking the dock window handle. If the user hovers the mouse cursor over one of the handles, the caption of the dock window is displayed in a tool tip (see Q3DockWindow::windowTitle() or Q3ToolBar::label()), so if you enable the \c Minimized dock area, it is best to specify a meaningful caption or label for each dock window. To minimize a dock window programmatically use moveDockWindow() with an edge of \c Minimized. Qt::Dock windows are moved transparently by default, i.e. during the drag an outline rectangle is drawn on the screen representing the position of the dock window as it moves. If you want the dock window to be shown normally whilst it is moved use setOpaqueMoving(). The location of a dock window, i.e. its dock area and position within the dock area, can be determined by calling getLocation(). Movable dock windows can be lined up to minimize wasted space with lineUpDockWindows(). Pointers to the dock areas are available from topDock(), leftDock(), rightDock() and bottomDock(). A customize menu item is added to the pop up dock window menu if isCustomizable() returns true; it returns false by default. Reimplement isCustomizable() and customize() if you want to offer this extra menu item, for example, to allow the user to change settings relating to the main window and its toolbars and dock windows. The main window's menu bar is fixed (at the top) by default. If you want a movable menu bar, create a QMenuBar as a stretchable widget inside its own movable dock window and restrict this dock window to only live within the \c Top or \c Bottom dock: \code Q3ToolBar *tb = new Q3ToolBar(this); addDockWindow(tb, tr("Menubar"), Top, false); QMenuBar *mb = new QMenuBar(tb); mb->setFrameStyle(QFrame::NoFrame); tb->setStretchableWidget(mb); setDockEnabled(tb, Left, false); setDockEnabled(tb, Right, false); \endcode An application with multiple dock windows can choose to save the current dock window layout in order to restore it later, e.g. in the next session. You can do this by using the streaming operators for Q3MainWindow. To save the layout and positions of all the dock windows do this: \code QFile file(filename); if (file.open(IO_WriteOnly)) { QTextStream stream(&file); stream << *mainWindow; file.close(); } \endcode To restore the dock window positions and sizes (normally when the application is next started), do the following: \code QFile file(filename); if (file.open(IO_ReadOnly)) { QTextStream stream(&file); stream >> *mainWindow; file.close(); } \endcode The QSettings class can be used in conjunction with the streaming operators to store the application's settings. Q3MainWindow's management of dock windows and toolbars is done transparently behind-the-scenes by Q3DockArea. For multi-document interfaces (MDI), use a QWorkspace as the central widget. Adding dock windows, e.g. toolbars, to Q3MainWindow's dock areas is straightforward. If the supplied dock areas are not sufficient for your application we suggest that you create a QWidget subclass and add your own dock areas (see \l Q3DockArea) to the subclass since Q3MainWindow provides functionality specific to the standard dock areas it provides. \sa Q3ToolBar Q3DockWindow QStatusBar QAction QMenuBar Q3PopupMenu QDialog*//*! \enum Q3MainWindow::DockWindows Right-clicking a dock area will pop-up the dock window menu (createDockWindowMenu() is called automatically). When called in code you can specify what items should appear on the menu with this enum. \value OnlyToolBars The menu will list all the toolbars, but not any other dock windows. \value NoToolBars The menu will list dock windows but not toolbars. \value AllDockWindows The menu will list all toolbars and other dock windows. (This is the default.)*//*! \fn void Q3MainWindow::addToolBar(Q3DockWindow *dockWindow, Qt::Dock position, bool newLine); Adds a new toolbar to the \a dockWindow. The toolbar is placed in the given \a position. If \a newLine is true the toolbar is put on a new line.*//*! \fn void Q3MainWindow::addToolBar(Q3DockWindow *dockWindow, const QString &label, Qt::Dock position, bool newLine) \overload The toolbar has the caption \a label and is placed in the given \a position.*//*! \fn void Q3MainWindow::moveToolBar(Q3DockWindow *dockWindow, Qt::Dock position); Moves the given \a dockWindow into the given \a position.*//*! \fn void Q3MainWindow::moveToolBar(Q3DockWindow *dockWindow, Qt::Dock position, bool nl, int index, int extraOffset) \overload The \a dockWindow is made the \a{index}-th item in the toolbar, moved over by \a extraOffset. If \a nl is true, the dock window is put on a new line.*//*! \fn void Q3MainWindow::removeToolBar(Q3DockWindow *dockWindow); Removes the toolbar from the given \a dockWindow.*//*! \fn void Q3MainWindow::lineUpToolBars(bool keepNewLines); Lines up the toolbars. Line breaks are preserved if \a keepNewLines is true.*//*! \fn void Q3MainWindow::toolBarPositionChanged(Q3ToolBar *toolbar); This signal is emitted when a \a toolbar is moved.*//*! \fn bool Q3MainWindow::toolBarsMovable() const Returns true if the window allows its toolbars to be moved; otherwise returns false.*//*! \fn void Q3MainWindow::setToolBarsMovable(bool b) If \a b is true the tool bars can be moved.*//*! Constructs an empty main window. The \a parent, \a name and widget flags \a f, are passed on to the QWidget constructor. By default, the widget flags are set to Qt::WType_TopLevel rather than 0 as they are with QWidget. If you don't want your Q3MainWindow to be a top level widget then you will need to set \a f to 0.*/Q3MainWindow::Q3MainWindow(QWidget * parent, const char * name, Qt::WindowFlags f) : QWidget(*new Q3MainWindowPrivate, parent, f){ Q_D(Q3MainWindow); setObjectName(QLatin1String(name));#ifdef Q_WS_MAC d->opaque = true;#else d->opaque = false;#endif installEventFilter(this); d->topDock = new Q3DockArea(Qt::Horizontal, Q3DockArea::Normal, this, "qt_top_dock"); d->topDock->installEventFilter(this); d->bottomDock = new Q3DockArea(Qt::Horizontal, Q3DockArea::Reverse, this, "qt_bottom_dock"); d->bottomDock->installEventFilter(this); d->leftDock = new Q3DockArea(Qt::Vertical, Q3DockArea::Normal, this, "qt_left_dock"); d->leftDock->installEventFilter(this); d->rightDock = new Q3DockArea(Qt::Vertical, Q3DockArea::Reverse, this, "qt_right_dock"); d->rightDock->installEventFilter(this); d->hideDock = new QHideDock(this);}/*! Destroys the object and frees any allocated resources.*/Q3MainWindow::~Q3MainWindow(){ delete layout();}#ifndef QT_NO_MENUBAR/*! Sets this main window to use the menu bar \a newMenuBar. The existing menu bar (if any) is deleted along with its contents. \sa menuBar()*/void Q3MainWindow::setMenuBar(QMenuBar * newMenuBar){ Q_D(Q3MainWindow); if (!newMenuBar) return; if (d->mb) delete d->mb; d->mb = newMenuBar; d->mb->installEventFilter(this); triggerLayout();}/*! Returns the menu bar for this window. If there isn't one, then menuBar() creates an empty menu bar. \sa statusBar()*/QMenuBar * Q3MainWindow::menuBar() const{ Q_D(const Q3MainWindow); if (d->mb) return d->mb; QObjectList l = queryList("QMenuBar", 0, false, false); QMenuBar * b; if (l.size()) { b = static_cast<QMenuBar *>(l.at(0)); } else { b = new QMenuBar((Q3MainWindow *)this); b->setObjectName(QLatin1String("automatic menu bar")); b->show(); } d->mb = b; d->mb->installEventFilter(const_cast<Q3MainWindow *>(this)); ((Q3MainWindow *)this)->triggerLayout(); return b;}#endif // QT_NO_MENUBAR/*! Sets this main window to use the status bar \a newStatusBar. The existing status bar (if any) is deleted along with its contents. Note that \a newStatusBar \e must be a child of this main window, and that it is not automatically displayed. If you call this function after show(), you will probably also need to call newStatusBar->show(). \sa setMenuBar() statusBar()*/void Q3MainWindow::setStatusBar(QStatusBar * newStatusBar){ Q_D(Q3MainWindow); if (!newStatusBar || newStatusBar == d->sb) return; if (d->sb) delete d->sb; d->sb = newStatusBar;#if 0 // ### this code can cause unnecessary creation of a tool tip group connect(toolTipGroup(), SIGNAL(showTip(QString)), d->sb, SLOT(showMessage(QString))); connect(toolTipGroup(), SIGNAL(removeTip()), d->sb, SLOT(clearMessage()));#endif d->sb->installEventFilter(this); triggerLayout();}/*! Returns this main window's status bar. If there isn't one, statusBar() creates an empty status bar, and if necessary a tool tip group too. \sa menuBar()*/QStatusBar * Q3MainWindow::statusBar() const{ Q_D(const Q3MainWindow); if (d->sb) return d->sb; QObjectList l = queryList("QStatusBar", 0, false, false); QStatusBar * s; if (l.size()) { s = (QStatusBar *)l.at(0); } else { s = new QStatusBar((Q3MainWindow *)this, "automatic status bar"); s->show(); } ((Q3MainWindow *)this)->setStatusBar(s); ((Q3MainWindow *)this)->triggerLayout(true); return s;}#if 0/*! Sets this main window to use the tool tip group \a newToolTipGroup. The existing tool tip group (if any) is deleted along with its contents. All the tool tips connected to it lose the ability to display the group texts. \sa menuBar()*/void Q3MainWindow::setToolTipGroup(QToolTipGroup * newToolTipGroup){ Q_D(Q3MainWindow); if (!newToolTipGroup || newToolTipGroup == d->ttg) return; if (d->ttg) delete d->ttg;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -