qmainwindow.cpp
来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 1,111 行 · 第 1/3 页
CPP
1,111 行
\e Note: if \a first is currently in a tabbed docked area, \a second will be added as a new tab, not as a neighbor of \a first. This is because a single tab can contain only one dock widget. \e Note: The 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. \sa tabifyDockWidget(), addDockWidget(), removeDockWidget()*/void QMainWindow::splitDockWidget(QDockWidget *after, QDockWidget *dockwidget, Qt::Orientation orientation){ if (!dockwidget->isAreaAllowed(dockWidgetArea(after))) qWarning("QMainWindow::splitDockWidget(): specified 'area' is not an allowed for this widget"); d_func()->layout->splitDockWidget(after, dockwidget, orientation); if (isVisible()) d_func()->layout->relayout();}/*! \fn void QMainWindow::tabifyDockWidget(QDockWidget *first, QDockWidget *second) Moves \a second dock widget on top of \a first dock widget, creating a tabbed docked area in the main window.*/void QMainWindow::tabifyDockWidget(QDockWidget *first, QDockWidget *second){ if (!second->isAreaAllowed(dockWidgetArea(first))) qWarning("QMainWindow::splitDockWidget(): specified 'area' is not an allowed for this widget"); d_func()->layout->tabifyDockWidget(first, second); if (isVisible()) d_func()->layout->relayout();}/*! Removes the \a dockwidget from the main window layout and hides it. Note that the \a dockwidget is \e not deleted.*/void QMainWindow::removeDockWidget(QDockWidget *dockwidget){ if (dockwidget) { d_func()->layout->removeWidget(dockwidget); dockwidget->hide(); }}/*! Returns the Qt::DockWidgetArea for \a dockwidget. If \a dockwidget has not been added to the main window, this function returns \c Qt::NoDockWidgetArea. \sa addDockWidget() splitDockWidget() Qt::DockWidgetArea*/Qt::DockWidgetArea QMainWindow::dockWidgetArea(QDockWidget *dockwidget) const{ return d_func()->layout->dockWidgetArea(dockwidget); }#endif // QT_NO_DOCKWIDGET/*! Saves the current state of this mainwindow's toolbars and dockwidgets. The \a version number is stored as part of the data. The \link QObject::objectName objectName\endlink 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 \a version number to restoreState(). \sa restoreState()*/QByteArray QMainWindow::saveState(int version) const{ QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); stream << QMainWindowLayout::VersionMarker; stream << version; d_func()->layout->saveState(stream); return data;}/*! Restores the \a state of this mainwindow's toolbars and dockwidgets. The \a version number is compared with that stored in \a state. If they do not match, the mainwindow's state is left unchanged, and this function returns \c false; otherwise, the state is restored, and this function returns \c true. \sa saveState()*/bool QMainWindow::restoreState(const QByteArray &state, int version){ if (state.isEmpty()) return false; QByteArray sd = state; QDataStream stream(&sd, QIODevice::ReadOnly); int marker, v; stream >> marker; stream >> v; if (stream.status() != QDataStream::Ok || marker != QMainWindowLayout::VersionMarker || v != version) return false; bool restored = d_func()->layout->restoreState(stream); if (isVisible()) QApplication::postEvent(this, new QResizeEvent(size(), size())); return restored;}#if !defined(QT_NO_DOCKWIDGET) && !defined(QT_NO_CURSOR)QCursor QMainWindowPrivate::separatorCursor(const QList<int> &path) const{ QDockAreaLayoutInfo *info = layout->dockWidgetLayout.info(path); Q_ASSERT(info != 0); if (path.size() == 1) { return info->o == Qt::Horizontal ? Qt::SplitVCursor : Qt::SplitHCursor; } return info->o == Qt::Horizontal ? Qt::SplitHCursor : Qt::SplitVCursor;}void QMainWindowPrivate::adjustCursor(const QPoint &pos){ Q_Q(QMainWindow); hoverPos = pos; if (pos == QPoint(0, 0)) { if (!hoverSeparator.isEmpty()) q->update(layout->dockWidgetLayout.separatorRect(hoverSeparator)); hoverSeparator.clear(); q->unsetCursor(); } else { QList<int> pathToSeparator = layout->dockWidgetLayout.findSeparator(pos); if (pathToSeparator != hoverSeparator) { if (!hoverSeparator.isEmpty()) q->update(layout->dockWidgetLayout.separatorRect(hoverSeparator)); hoverSeparator = pathToSeparator; if (hoverSeparator.isEmpty()) { q->unsetCursor(); } else { q->update(layout->dockWidgetLayout.separatorRect(hoverSeparator)); QCursor cursor = separatorCursor(hoverSeparator); q->setCursor(cursor); } } }}#endif/*! \reimp */bool QMainWindow::event(QEvent *event){ Q_D(QMainWindow); switch (event->type()) {#ifndef QT_NO_DOCKWIDGET case QEvent::Paint: { QPainter p(this); QRegion r = static_cast<QPaintEvent*>(event)->region(); d->layout->dockWidgetLayout.paintSeparators(&p, this, r, d->hoverPos); break; }#ifndef QT_NO_CURSOR case QEvent::HoverMove: { d->adjustCursor(static_cast<QHoverEvent*>(event)->pos()); break; } case QEvent::HoverLeave: d->adjustCursor(QPoint(0, 0)); break;#endif // QT_NO_CURSOR case QEvent::MouseButtonPress: { QMouseEvent *e = static_cast<QMouseEvent*>(event); if (e->button() == Qt::LeftButton && d->layout->startSeparatorMove(e->pos())) { // The click was on a separator, eat this event e->accept(); return true; } break; } case QEvent::MouseMove: { QMouseEvent *e = static_cast<QMouseEvent*>(event);#ifndef QT_NO_CURSOR d->adjustCursor(e->pos());#endif if (e->buttons() & Qt::LeftButton) { if (d->layout->separatorMove(e->pos())) { // We're moving a separator, eat this event e->accept(); return true; } } break; } case QEvent::MouseButtonRelease: { QMouseEvent *e = static_cast<QMouseEvent*>(event); if (d->layout->endSeparatorMove(e->pos())) { // We've released a separator, eat this event e->accept(); return true; } break; }#endif#ifndef QT_NO_TOOLBAR case QEvent::ToolBarChange: { QList<QToolBar *> toolbars = qFindChildren<QToolBar *>(this); QSize minimumSize = d->layout->minimumSize(); for (int i = 0; i < toolbars.size(); ++i) { QToolBar *toolbar = toolbars.at(i); toolbar->setVisible(!toolbar->isVisible()); } QApplication::sendPostedEvents(this, QEvent::LayoutRequest); QSize newMinimumSize = d->layout->minimumSize(); QSize delta = newMinimumSize - minimumSize; resize(size() + delta); return true; }#endif#ifndef QT_NO_STATUSTIP case QEvent::StatusTip:#ifndef QT_NO_STATUSBAR if (QStatusBar *sb = d->layout->statusBar()) sb->showMessage(static_cast<QStatusTipEvent*>(event)->tip()); else#endif static_cast<QStatusTipEvent*>(event)->ignore(); return true;#endif // QT_NO_STATUSTIP case QEvent::StyleChange: if (!d->explicitIconSize) setIconSize(QSize()); break; default: break; } return QWidget::event(event);}/*! \internal*/bool QMainWindow::isSeparator(const QPoint &pos) const{ Q_D(const QMainWindow);#ifndef QT_NO_DOCKWIDGET return !d->layout->dockWidgetLayout.findSeparator(pos).isEmpty();#else return false;#endif}/*! \reimp*/void QMainWindow::contextMenuEvent(QContextMenuEvent *event){ // only show the context menu for direct QDockWidget and QToolBar // children QWidget *child = childAt(event->pos()); while (child && child != this) {#ifndef QT_NO_DOCKWIDGET if (QDockWidget *dw = qobject_cast<QDockWidget *>(child)) { if (dw->parentWidget() != this) return; if (dw->widget() && dw->widget()->geometry().contains(child->mapFrom(this, event->pos()))) { // ignore the event if the mouse is over the QDockWidget contents return; } break; }#endif // QT_NO_DOCKWIDGET#ifndef QT_NO_TOOLBAR if (QToolBar *tb = qobject_cast<QToolBar *>(child)) { if (tb->parentWidget() != this) return; break; }#endif child = child->parentWidget(); } if (child == this) return;#ifndef QT_NO_MENU QMenu *popup = createPopupMenu(); if (!popup) return; popup->exec(event->globalPos()); delete popup; event->accept();#endif}#ifndef QT_NO_MENU/*! Returns a popup menu containing checkable entries for the toolbars and dock widgets present in the main window. 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. \sa addDockWidget(), addToolBar(), menuBar()*/QMenu *QMainWindow::createPopupMenu(){ Q_D(QMainWindow); QMenu *menu = 0;#ifndef QT_NO_DOCKWIDGET QList<QDockWidget *> dockwidgets = qFindChildren<QDockWidget *>(this); if (dockwidgets.size()) { menu = new QMenu(this); for (int i = 0; i < dockwidgets.size(); ++i) { QDockWidget *dockWidget = dockwidgets.at(i); if (dockWidget->parentWidget() == this && d->layout->contains(dockWidget)) { menu->addAction(dockwidgets.at(i)->toggleViewAction()); } } menu->addSeparator(); }#endif // QT_NO_DOCKWIDGET#ifndef QT_NO_TOOLBAR QList<QToolBar *> toolbars = qFindChildren<QToolBar *>(this); if (toolbars.size()) { if (!menu) menu = new QMenu(this); for (int i = 0; i < toolbars.size(); ++i) { QToolBar *toolBar = toolbars.at(i); if (toolBar->parentWidget() == this && d->layout->contains(toolBar)) { menu->addAction(toolbars.at(i)->toggleViewAction()); } } }#endif Q_UNUSED(d); return menu;}#endif // QT_NO_MENU#endif // QT_NO_MAINWINDOW
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?