📄 qdockwidget.cpp
字号:
recalculate absolute position as if the tool window was to be dropped to toplevel */ target = state->origin; target.moveTopLeft(event->globalPos() - state->offset); } else { /* cannot float the window, so put it back into its original position */ target = state->origin; } } if (!state->rubberband) { const int screen_number = QApplication::desktop()->screenNumber(q->window()); state->rubberband = new QRubberBand(QRubberBand::Rectangle, QApplication::desktop()->screen(screen_number)); state->rubberband->setGeometry(target); state->rubberband->show(); } else { if (state->current != target) state->rubberband->setGeometry(target); } state->current = target;#endif // !defined(QT_NO_MAINWINDOW)}void QDockWidgetPrivate::mouseReleaseEvent(QMouseEvent *event){#if !defined(QT_NO_MAINWINDOW) Q_Q(QDockWidget); if (event->button() != Qt::LeftButton) return; if (!state) return; QMainWindowLayout *layout = qobject_cast<QMainWindowLayout *>(q->parentWidget()->layout()); if (!layout) return; layout->discardLayoutInfo(); delete state->rubberband; QWidget *focus = qApp->focusWidget(); // calculate absolute position if the tool window was to be // dropped to toplevel QRect target; bool dropped = false; if (!(event->modifiers() & Qt::ControlModifier)) { // see if there is a main window under us, and ask it to drop the tool window QWidget *widget = QApplication::widgetAt(event->globalPos()); if (state->canDrop && widget) { QMainWindow *mainwindow = findMainWindow(widget); if (mainwindow) { QMainWindowLayout *layout = qobject_cast<QMainWindowLayout *>(q->parentWidget()->layout()); Q_ASSERT(layout != 0); QRect request = state->origin; // ### remove extra frame request.moveTopLeft(event->globalPos() - state->offset); layout->dropDockWidget(q, request, event->globalPos()); dropped = true; } } } if (!dropped && hasFeature(q, QDockWidget::DockWidgetFloatable)) { target = state->origin; target.moveTopLeft(event->globalPos() - state->offset); if (!q->isFloating()) { q->hide(); q->setFloating(true); q->setGeometry(target); q->show(); } else { // move to new location q->setGeometry(target); } } // restore focus if (focus) focus->setFocus(); delete state; state = 0;#endif // !defined(QT_NO_MAINWINDOW)}/*! \class QDockWidget \brief The QDockWidget class provides a widget that can be docked inside a QMainWindow or floated as a top-level window on the desktop. \ingroup application QDockWidget provides the concept of dock widgets, also know as tool palettes or utility windows. Dock windows are secondary windows placed in the \e {dock widget area} around the \l{QMainWindow::centralWidget()}{central widget} in a QMainWindow. \image mainwindow-docks.png Dock windows can be moved inside their current area, moved into new areas and floated (e.g. undocked) by the end-user. The QDockWidget API allows the programmer to restrict the dock widgets ability to move, float and close, as well as the areas in which they can be placed. \section1 Appearance A QDockWidget consists of a title bar and the content area. The titlebar displays the dock widgets \link QWidget::windowTitle() window title\endlink, a \e float button and a \e close button. Depending on the state of the QDockWidget, the \e float and \e close buttons may be either disabled or not shown at all. The visual appearance of the title bar and buttons is dependent on the \l{QStyle}{style} in use. \sa QMainWindow*//*! \enum QDockWidget::DockWidgetFeature \value DockWidgetClosable The dock widget can be closed. \value DockWidgetMovable The dock widget can be moved between docks by the user. \value DockWidgetFloatable The dock widget can be detached from the main window, and floated as an independent window. \value AllDockWidgetFeatures The dock widget can be closed, moved, and floated. \value NoDockWidgetFeatures The dock widget cannot be closed, moved, or floated. \omitvalue DockWidgetFeatureMask \omitvalue Reserved*//*! Constructs a QDockWidget with parent \a parent and window flags \a flags. The dock widget will be placed in the left dock widget area.*/QDockWidget::QDockWidget(QWidget *parent, Qt::WFlags flags) : QWidget(*new QDockWidgetPrivate, parent, flags){ Q_D(QDockWidget); d->init();}/*! Constructs a QDockWidget with parent \a parent and window flags \a flags. The dock widget will be placed in the left dock widget area. The window title is set to \a title. This title is used when the QDockWidget is docked and undocked. It is also used in the context menu provided by QMainWindow. \sa setWindowTitle()*/QDockWidget::QDockWidget(const QString &title, QWidget *parent, Qt::WFlags flags) : QWidget(*new QDockWidgetPrivate, parent, flags){ Q_D(QDockWidget); d->init(); setWindowTitle(title);}/*! Destroys the dock widget.*/QDockWidget::~QDockWidget(){ }/*! Returns the widget for the dock widget. This function returns zero if the widget has not been set. \sa setWidget()*/QWidget *QDockWidget::widget() const{ Q_D(const QDockWidget); return d->widget;}/*! Sets the widget for the dock widget to \a widget. \sa widget()*/void QDockWidget::setWidget(QWidget *widget){ Q_D(QDockWidget); if (d->widget) d->box->removeWidget(d->widget); d->widget = widget; if (d->widget) { d->box->addChildWidget(widget); d->box->insertItem(1, new QDockWidgetItem(d->widget)); }}/*! \property QDockWidget::features \brief whether the dock widget is movable, closable, and floatable \sa DockWidgetFeature*/void QDockWidget::setFeatures(QDockWidget::DockWidgetFeatures features){ Q_D(QDockWidget); features &= DockWidgetFeatureMask; if (d->features == features) return; d->features = features; d->updateButtons(); d->toggleViewAction->setEnabled((d->features & DockWidgetClosable) == DockWidgetClosable); emit featuresChanged(d->features);}QDockWidget::DockWidgetFeatures QDockWidget::features() const{ Q_D(const QDockWidget); return d->features;}/*! \property QDockWidget::floating \brief whether the dock widget is floating A floating dock widget is presented to the user as an independent window "on top" of its parent QMainWindow, instead of being docked in the QMainWindow. \sa isWindow()*/void QDockWidget::setFloating(bool floating){ Q_D(QDockWidget); if (floating == isFloating()) return; const bool visible = isVisible(); setWindowFlags(Qt::FramelessWindowHint | (floating ? Qt::Tool : Qt::Widget)); d->updateButtons();#ifndef QT_NO_MAINWINDOW if (floating) { if (QMainWindowLayout *layout = qobject_cast<QMainWindowLayout *>(parentWidget()->layout())) layout->invalidate(); }#endif d->resizer->setActive(floating); if (visible) show(); emit topLevelChanged(isWindow());}/*! \property QDockWidget::allowedAreas \brief areas where the dock widget may be placed The default is Qt::AllDockWidgetAreas. \sa Qt::DockWidgetArea*/void QDockWidget::setAllowedAreas(Qt::DockWidgetAreas areas){ Q_D(QDockWidget); areas &= Qt::DockWidgetArea_Mask; if (areas == d->allowedAreas) return; d->allowedAreas = areas; emit allowedAreasChanged(d->allowedAreas);}Qt::DockWidgetAreas QDockWidget::allowedAreas() const{ Q_D(const QDockWidget); return d->allowedAreas;}/*! \fn bool QDockWidget::isAreaAllowed(Qt::DockWidgetArea area) const Returns true if this dock widget can be placed in the given \a area; otherwise returns false.*//*! \reimp */void QDockWidget::changeEvent(QEvent *event){ Q_D(QDockWidget); switch (event->type()) { case QEvent::WindowTitleChange: update(d->titleArea);#ifndef QT_NO_ACTION d->toggleViewAction->setText(windowTitle());#endif break; default: break; } QWidget::changeEvent(event);}/*! \reimp */void QDockWidget::closeEvent(QCloseEvent *event){ Q_D(QDockWidget); if (!(d->features & DockWidgetClosable)) event->ignore();}/*! \reimp */void QDockWidget::paintEvent(QPaintEvent *event){ Q_UNUSED(event) QPainter p(this); // ### Add PixelMetric to change spacers, so style may show border // when not floating. if (isFloating()) { QStyleOptionFrame framOpt; framOpt.init(this); style()->drawPrimitive(QStyle::PE_FrameDockWidget, &framOpt, &p, this); } // Title must be painted after the frame, since the areas overlap, and // the title may wish to extend out to all sides (eg. XP style) Q_D(QDockWidget); QStyleOptionDockWidget titleOpt = d->getStyleOption(); style()->drawControl(QStyle::CE_DockWidgetTitle, &titleOpt, &p, this);}/*! \reimp */bool QDockWidget::event(QEvent *event){ Q_D(QDockWidget); switch (event->type()) {#ifndef QT_NO_ACTION case QEvent::Hide: if (!isHidden()) break; // fallthrough intended case QEvent::Show: d->toggleViewAction->setChecked(event->type() == QEvent::Show); break;#endif case QEvent::StyleChange: d->updateButtons(); break; case QEvent::ContextMenu: if (d->state) { event->accept(); return true; } break; case QEvent::Resize: d->relayout(); break; // return true after calling the handler since we don't want // them to be passed onto the default handlers case QEvent::MouseButtonPress: d->mousePressEvent(static_cast<QMouseEvent *>(event)); return true; case QEvent::MouseButtonDblClick: d->mouseDoubleClickEvent(static_cast<QMouseEvent *>(event)); return true; case QEvent::MouseMove: d->mouseMoveEvent(static_cast<QMouseEvent *>(event)); return true; case QEvent::MouseButtonRelease: d->mouseReleaseEvent(static_cast<QMouseEvent *>(event)); return true; case QEvent::ChildRemoved: if (d->widget == static_cast<QChildEvent *>(event)->child()) d->widget = 0; break; default: break; } return QWidget::event(event);}#ifndef QT_NO_ACTION/*! Returns a checkable action that can be used to show or close this dock widget. The action's text is set to the dock widget's window title. \sa QAction::text QWidget::windowTitle */QAction * QDockWidget::toggleViewAction() const{ Q_D(const QDockWidget); return d->toggleViewAction;}#endif // QT_NO_ACTION/*! \fn void QDockWidget::featuresChanged(QDockWidget::DockWidgetFeatures features) This signal is emitted when the \l features property changes. The \a features parameter gives the new value of the property.*//*! \fn void QDockWidget::topLevelChanged(bool topLevel) This signal is emitted when the \l floating property changes. The \a topLevel parameter is true if the dock widget is now floating; otherwise it is false. \sa isWindow()*//*! \fn void QDockWidget::allowedAreasChanged(Qt::DockWidgetAreas allowedAreas) This signal is emitted when the \l allowedAreas property changes. The \a allowedAreas parameter gives the new value of the property.*/#include "qdockwidget.moc"#include "moc_qdockwidget.cpp"#endif // QT_NO_DOCKWIDGET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -