⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qmainwindow.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qmainwindow.h"#include "qmainwindowlayout_p.h"#ifndef QT_NO_MAINWINDOW#include "qdockwidget.h"#include "qtoolbar.h"#include <qapplication.h>#include <qmenubar.h>#include <qstatusbar.h>#include <qevent.h>#include <qstyle.h>#include <qdebug.h>#include <qpainter.h>#include <private/qwidget_p.h>#include "qtoolbar_p.h"#include "qwidgetanimator_p.h"#ifdef Q_WS_MAC#include <private/qt_mac_p.h>extern WindowRef qt_mac_window_for(const QWidget *); // qwidget_mac.cpp#endifclass QMainWindowPrivate : public QWidgetPrivate{    Q_DECLARE_PUBLIC(QMainWindow)public:    inline QMainWindowPrivate()        : layout(0), toolButtonStyle(Qt::ToolButtonIconOnly)#ifdef Q_WS_MAC            , useHIToolBar(false)#endif    { }    QMainWindowLayout *layout;    QSize iconSize;    bool explicitIconSize;    Qt::ToolButtonStyle toolButtonStyle;#ifdef Q_WS_MAC    bool useHIToolBar;#endif    void init();    QList<int> hoverSeparator;    QPoint hoverPos;#if !defined(QT_NO_DOCKWIDGET) && !defined(QT_NO_CURSOR)    QCursor separatorCursor(const QList<int> &path) const;    void adjustCursor(const QPoint &pos);#endif};void QMainWindowPrivate::init(){    Q_Q(QMainWindow);    layout = new QMainWindowLayout(q);    const int metric = q->style()->pixelMetric(QStyle::PM_ToolBarIconSize);    iconSize = QSize(metric, metric);    explicitIconSize = false;    q->setAttribute(Qt::WA_Hover);}/*    The Main Window:    +----------------------------------------------------------+    | Menu Bar                                                 |    +----------------------------------------------------------+    | Tool Bar Area                                            |    |   +--------------------------------------------------+   |    |   | Dock Window Area                                 |   |    |   |   +------------------------------------------+   |   |    |   |   |                                          |   |   |    |   |   | Central Widget                           |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   |                                          |   |   |    |   |   +------------------------------------------+   |   |    |   |                                                  |   |    |   +--------------------------------------------------+   |    |                                                          |    +----------------------------------------------------------+    | Status Bar                                               |    +----------------------------------------------------------+*//*!    \class QMainWindow    \brief The QMainWindow class provides a main application	   window.    \ingroup application    \mainclass    \tableofcontents    \section1 Qt Main Window Framework    A main window provides a framework for building an    application's user interface. Qt has QMainWindow and its \l{Main    Window and Related Classes}{related classes} for main window    management. QMainWindow has its own layout to which you can add    \l{QToolBar}s, \l{QDockWidget}s, a    QMenuBar, and a QStatusBar. The layout has a center area that can    be occupied by any kind of widget. You can see an image of the    layout below.    \image mainwindowlayout.png    \section1 Creating Main Window Components    A central widget will typically be a standard Qt widget such    as a QTextEdit or a QGraphicsView. Custom widgets can also be    used for advanced applications. You set the central widget with \c    setCentralWidget().    Main windows have either a single (SDI) or multiple (MDI)    document interface. You create MDI applications in Qt by using a    QMdiArea 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.    \section2 Creating Menus    Qt implements menus in QMenu and QMainWindow keeps them in a    QMenuBar. \l{QAction}{QAction}s are added to the menus, which    display them as menu items.    You can add new menus to the main window's menu bar by calling    \c menuBar(), which returns the QMenuBar for the window, and then    add a menu with QMenuBar::addMenu().    QMainWindow comes with a default menu bar, but you can also    set one yourself with \c setMenuBar(). If you wish to implement a    custom menu bar (i.e., not use the QMenuBar widget), you can set it    with \c setMenuWidget().    An example of how to create menus follows:    \quotefromfile mainwindows/application/mainwindow.cpp    \skipto /::createMenus/    \printuntil /saveAct/    The \c createPopupMenu() 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 reimplement \c    createPopupMenu() for a custom menu.    \section2 Creating Toolbars    Toolbars are implemented in the QToolBar class.  You add a    toolbar to a main window with \c addToolBar().    You control the initial position of toolbars by assigning them    to a specific Qt::ToolBarArea. You can split an area by inserting    a toolbar break - think of this as a line break in text editing -    with \c addToolBarBreak() or \c insertToolBarBreak(). You can also    restrict placement by the user with QToolBar::setAllowedAreas()    and QToolBar::setMovable().    The size of toolbar icons can be retrieved with \c iconSize().    The sizes are platform dependent; you can set a fixed size with \c    setIconSize(). You can alter the appearance of all tool buttons in    the toolbars with \c setToolButtonStyle().    An example of toolbar creation follows:    \quotefromfile mainwindows/application/mainwindow.cpp    \skipto /::createToolBars/    \printuntil /fileToolBar->addAction/    \section2 Creating Dock Widgets    Dock widgets are implemented in the QDockWidget class. A dock    widget is a window that can be docked into the main window.  You    add dock widgets to a main window with \c 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 \c setDockWidgetCorner(). By default    each area can only contain one row (vertical or horizontal) of    dock widgets, but if you enable nesting with \c    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 that should be    displayed.    We give an example of how to create and add dock widgets to a    main window:    \quotefromfile snippets/mainwindowsnippet.cpp    \skipto /QDockWidget \*dockWidget/    \printuntil /addDockWidget/    \section2 The Status Bar    You can set a status bar with \c setStatusBar(), but one is    created the first time \c statusBar() (which returns the main    window's status bar) is called. See QStatusBar for information on    how to use it.    \section1 Storing State    QMainWindow can store the state of its layout with \c    saveState(); it can later be retrieved with \c restoreState(). It    is the position and size (relative to the size of the main window)    of the toolbars and dock widgets that are stored.    \sa QMenuBar, QToolBar, QStatusBar, QDockWidget, {Application    Example}, {Dock Widgets Example}, {MDI Example}, {SDI Example},    {Menus Example}*//*!    \fn 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 \a iconSize.    You can connect this signal to other components to help maintain    a consistent appearance for your application.    \sa setIconSize()*//*!    \fn 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 \a toolButtonStyle.    You can connect this signal to other components to help maintain    a consistent appearance for your application.    \sa setToolButtonStyle()*//*!    Constructs a QMainWindow with the given \a parent and the specified    widget \a flags. */QMainWindow::QMainWindow(QWidget *parent, Qt::WindowFlags flags)    : QWidget(*(new QMainWindowPrivate()), parent, flags | Qt::Window){    d_func()->init();}#ifdef QT3_SUPPORT/*!    \obsolete    Constructs a QMainWindow with the given \a parent, \a name, and    with the specified widget \a flags. */QMainWindow::QMainWindow(QWidget *parent, const char *name, Qt::WindowFlags flags)    : QWidget(*(new QMainWindowPrivate()), parent, flags | Qt::WType_TopLevel){    setObjectName(QString::fromAscii(name));    d_func()->init();}#endif/*!    Destroys the main window. */QMainWindow::~QMainWindow(){ }/*! \property QMainWindow::iconSize    \brief 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.*//*!    \property QMainWindow::dockOptions    \brief the docking behavior of QMainWindow    \since 4.3    The default value is AnimatedDocks | AllowTabbedDocks.*//*!    \enum QMainWindow::DockOption    \since 4.3    This enum contains flags that specify the docking behavior of QMainWindow.    \value AnimatedDocks    Identical to the \l animated property.    \value AllowNestedDocks Identical to the \l dockNestingEnabled property.    \value AllowTabbedDocks 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.    \value ForceTabbedDocks 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.    \value VerticalTabs     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.    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.*/void QMainWindow::setDockOptions(DockOptions opt){    Q_D(QMainWindow);    d->layout->setDockOptions(opt);}QMainWindow::DockOptions QMainWindow::dockOptions() const{    Q_D(const QMainWindow);    return d->layout->dockOptions;}QSize QMainWindow::iconSize() const{ return d_func()->iconSize; }void QMainWindow::setIconSize(const QSize &iconSize){    Q_D(QMainWindow);    QSize sz = iconSize;    if (!sz.isValid()) {        const int metric = style()->pixelMetric(QStyle::PM_ToolBarIconSize);        sz = QSize(metric, metric);    }    if (d->iconSize != sz) {        d->iconSize = sz;        emit iconSizeChanged(d->iconSize);    }    d->explicitIconSize = iconSize.isValid();}/*! \property QMainWindow::toolButtonStyle    \brief style of toolbar buttons in this mainwindow.    The default is Qt::ToolButtonIconOnly.*/Qt::ToolButtonStyle QMainWindow::toolButtonStyle() const{ return d_func()->toolButtonStyle; }void QMainWindow::setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle){    Q_D(QMainWindow);    if (d->toolButtonStyle == toolButtonStyle)        return;    d->toolButtonStyle = toolButtonStyle;    emit toolButtonStyleChanged(d->toolButtonStyle);}#ifndef QT_NO_MENUBAR/*!    Returns the menu bar for the main window. This function creates    and returns an empty menu bar if the menu bar does not exist.    \sa setMenuBar()*/QMenuBar *QMainWindow::menuBar() const{    QMenuBar *menuBar = qobject_cast<QMenuBar *>(d_func()->layout->menuBar());    if (!menuBar) {	QMainWindow *self = const_cast<QMainWindow *>(this);	menuBar = new QMenuBar(self);	self->setMenuBar(menuBar);    }    return menuBar;}/*!    Sets the menu bar for the main window to \a menuBar.    Note: QMainWindow takes ownership of the \a menuBar pointer and    deletes it at the appropriate time.    \sa menuBar()*/void QMainWindow::setMenuBar(QMenuBar *menuBar){    Q_D(QMainWindow);    if (d->layout->menuBar() && d->layout->menuBar() != menuBar) {        // Reparent corner widgets before we delete the old menu bar.        QMenuBar *oldMenuBar = qobject_cast<QMenuBar *>(d->layout->menuBar());        if (menuBar) {            // TopLeftCorner widget.            QWidget *cornerWidget = oldMenuBar->cornerWidget(Qt::TopLeftCorner);            if (cornerWidget)                menuBar->setCornerWidget(cornerWidget, Qt::TopLeftCorner);            // TopRightCorner widget.            cornerWidget = oldMenuBar->cornerWidget(Qt::TopRightCorner);            if (cornerWidget)                menuBar->setCornerWidget(cornerWidget, Qt::TopRightCorner);        }        delete oldMenuBar;    }    d->layout->setMenuBar(menuBar);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -