qmainwindow.cpp
来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 1,111 行 · 第 1/3 页
CPP
1,111 行
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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"class QMainWindowPrivate : public QWidgetPrivate{ Q_DECLARE_PUBLIC(QMainWindow)public: inline QMainWindowPrivate() : layout(0), toolButtonStyle(Qt::ToolButtonIconOnly) { } QMainWindowLayout *layout; QSize iconSize; bool explicitIconSize; Qt::ToolButtonStyle toolButtonStyle; 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 QMainWindow provides a main application window, with a menu bar, tool bars, dock widgets and a status bar around a large central widget, such as a text edit, drawing canvas or QWorkspace (for MDI applications). Note that QMainWindow comes with its own customized layout and that setting a layout on a QMainWindow, or creating a layout with a QMainWindow as a parent is considered an error. You should set your own layout on the \l{centralWidget()}{central widget} instead. Topics: \tableofcontents \section1 Saving and restoring state The saveState() and restoreState() functions provide a means to save and restore the layout of the QToolBars and QDockWidgets in the QMainWindow. These functions work by storing the \link QObject::objectName objectName\endlink of each QToolBar and QDockWidget together with information about placement, size, etc. \section1 Behavior of Dock Widgets \target dock-widget-separators \section2 Dock Widget Separators QMainWindow uses separators to separate QDockWidgets from each other and the \l{centralWidget()}{central widget}. These separators let the user control the size of QDockWidgets by dragging the boundary between them. A QDockWidget can be as large or as small as the user wishes, between the minimumSizeHint() (or minimumSize()) and maximumSize() of the QDockWidget. When a QDockWidget reaches its minimum size, space will be taken from other QDockWidgets in the direction of the user's drag, if possible. Once all QDockWidgets have reached their minimum sizes, further dragging does nothing. When a QDockWidget reaches its maximium size, space will be given to other QDockWidgets in the opposite direction of the user's drag, if possible. Once all QDockWidgets have reached their minimum size, futher dragging does nothing. \target dragging-dock-widgets \section2 Dragging Dock Widgets QDockWidget displays a title bar to let the user drag the dock widget to a new location. A QDockWidget can be moved to any location provided enough space is available. (QMainWindow won't resize itself to a larger size in an attempt to provide more space.) A QRubberBand is shown while dragging the QDockWidget. This QRubberBand provides an indication to the user about where the QDockWidget will be placed when the mouse button is released. \section3 Managing Dock Widgets and Toolbars By default, QMainWindow provides a context menu that can be used to toggle the visibility of the toolbars and dock widgets attached to a main window. This menu is usually accessed by right-clicking on a dock window or toolbar, but it can also be obtained programmatically by calling createPopupMenu(). \image mainwindow-contextmenu.png A typical main window context menu. This popup menu can be replaced or customized to suit the specific needs of an application: In a QMainWindow subclass, reimplement createPopupMenu() to either create a custom popup menu by constructing a new QMenu on demand, or to modify the QMenu object obtained by calling the default implementation. \sa QMenuBar, QToolBar, QStatusBar, QDockWidget, {Application Example}, {Dock Widgets Example}, {MDI Example}, {SDI 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.*/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) delete d->layout->menuBar(); d->layout->setMenuBar(menuBar);}/*! \since 4.2 Returns the menu bar for the main window. This function returns null if a menubar hasn't been constructed yet.*/QWidget *QMainWindow::menuWidget() const{ QWidget *menuBar = d_func()->layout->menuBar(); return menuBar;}/*! \since 4.2 Sets the menu bar for the main window to \a menuBar. QMainWindow takes ownership of the \a menuBar pointer and deletes it at the appropriate time.*/void QMainWindow::setMenuWidget(QWidget *menuBar){ Q_D(QMainWindow); if (d->layout->menuBar() && d->layout->menuBar() != menuBar) delete d->layout->menuBar(); d->layout->setMenuBar(menuBar);}#endif // QT_NO_MENUBAR#ifndef QT_NO_STATUSBAR/*! Returns the status bar for the main window. This function creates and returns an empty status bar if the status bar does not exist. \sa setStatusBar()*/QStatusBar *QMainWindow::statusBar() const{ QStatusBar *statusbar = d_func()->layout->statusBar(); if (!statusbar) { QMainWindow *self = const_cast<QMainWindow *>(this); statusbar = new QStatusBar(self); statusbar->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed); self->setStatusBar(statusbar); } return statusbar;}/*! Sets the status bar for the main window to \a statusbar. Setting the status bar to 0 will remove it from the main window.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?