📄 qtoolbar.cpp
字号:
action->d_func()->autoCreated = true; addAction(action); return action;}/*! Inserts the given \a widget in front of the toolbar item associated with the \a before action. Note: You should use QAction::setVisible() to change the visibility of the widget. Using QWidget::setVisible(), QWidget::show() and QWidget::hide() does not work. \sa addWidget()*/QAction *QToolBar::insertWidget(QAction *before, QWidget *widget){ QWidgetAction *action = new QWidgetAction(this); action->setDefaultWidget(widget); action->d_func()->autoCreated = true; insertAction(before, action); return action;}/*! \internal Returns the geometry of the toolbar item associated with the given \a action, or an invalid QRect if no matching item is found.*/QRect QToolBar::actionGeometry(QAction *action) const{ Q_D(const QToolBar); int index = d->layout->indexOf(action); if (index == -1) return QRect(); return d->layout->itemAt(index)->widget()->geometry();}/*! Returns the action at point \a p. This function returns zero if no action was found. \sa QWidget::childAt()*/QAction *QToolBar::actionAt(const QPoint &p) const{ Q_D(const QToolBar); QWidget *widget = childAt(p); int index = d->layout->indexOf(widget); if (index == -1) return 0; QLayoutItem *item = d->layout->itemAt(index); return static_cast<QToolBarItem*>(item)->action;}/*! \fn QAction *QToolBar::actionAt(int x, int y) const \overload Returns the action at the point \a x, \a y. This function returns zero if no action was found.*//*! \reimp */void QToolBar::actionEvent(QActionEvent *event){ Q_D(QToolBar); QAction *action = event->action(); QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(action); switch (event->type()) { case QEvent::ActionAdded: { Q_ASSERT_X(widgetAction == 0 || d->layout->indexOf(widgetAction) == -1, "QToolBar", "widgets cannot be inserted multiple times"); // reparent the action to this toolbar if it has been created // using the addAction(text) etc. convenience functions, to // preserve Qt 4.1.x behavior. The widget is already // reparented to us due to the createWidget call inside // createItem() if (widgetAction != 0 && widgetAction->d_func()->autoCreated) widgetAction->setParent(this); int index = d->layout->count(); if (event->before()) { index = d->layout->indexOf(event->before()); Q_ASSERT_X(index != -1, "QToolBar::insertAction", "internal error"); } d->layout->insertAction(index, action); break; } case QEvent::ActionChanged: d->layout->invalidate(); break; case QEvent::ActionRemoved: { int index = d->layout->indexOf(action); if (index != -1) { delete d->layout->takeAt(index); } break; } default: Q_ASSERT_X(false, "QToolBar::actionEvent", "internal error"); }}/*! \reimp */void QToolBar::changeEvent(QEvent *event){ Q_D(QToolBar); switch (event->type()) { case QEvent::WindowTitleChange: d->toggleViewAction->setText(windowTitle()); break; case QEvent::StyleChange: d->layout->invalidate(); if (!d->explicitIconSize) setIconSize(QSize()); d->layout->updateMarginAndSpacing(); break; case QEvent::LayoutDirectionChange: d->layout->invalidate(); break; default: break; } QWidget::changeEvent(event);}/*! \reimp */void QToolBar::paintEvent(QPaintEvent *){ Q_D(QToolBar); QPainter p(this); QStyle *style = this->style(); QStyleOptionToolBar opt; initStyleOption(&opt); if (d->layout->expanded || d->layout->animating || isWindow()) { p.fillRect(opt.rect, palette().background()); style->drawPrimitive(QStyle::PE_FrameMenu, &opt, &p, this); } else { style->drawControl(QStyle::CE_ToolBar, &opt, &p, this); } opt.rect = d->layout->handleRect(); if (opt.rect.isValid()) style->drawPrimitive(QStyle::PE_IndicatorToolBarHandle, &opt, &p, this);}/* Checks if an expanded toolbar has to wait for this popup to close before the toolbar collapses. This is true if 1) the popup has the toolbar in its parent chain, 2) the popup is a menu whose menuAction is somewhere in the toolbar.*/static bool waitForPopup(QToolBar *tb, QWidget *popup){ if (popup == 0 || popup->isHidden()) return false; QWidget *w = popup; while (w != 0) { if (w == tb) return true; w = w->parentWidget(); } QMenu *menu = qobject_cast<QMenu*>(popup); if (menu == 0) return false; QAction *action = menu->menuAction(); QList<QWidget*> widgets = action->associatedWidgets(); for (int i = 0; i < widgets.count(); ++i) { if (waitForPopup(tb, widgets.at(i))) return true; } return false;}/*! \reimp */bool QToolBar::event(QEvent *event){ Q_D(QToolBar); switch (event->type()) { case QEvent::Hide: if (!isHidden()) break; // fallthrough intended case QEvent::Show: d->toggleViewAction->setChecked(event->type() == QEvent::Show);#ifdef Q_WS_MAC // Fall through case QEvent::LayoutRequest: // There's currently no way to invalidate the size and let // HIToolbar know about it. This forces a re-check. if (QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget())) { WindowRef windowRef = qt_mac_window_for(this); if (mainWindow->unifiedTitleAndToolBarOnMac() && mainWindow->toolBarArea(this) == Qt::TopToolBarArea && IsWindowToolbarVisible(windowRef)) { DisableScreenUpdates(); ShowHideWindowToolbar(windowRef, false, false); ShowHideWindowToolbar(windowRef, true, false); EnableScreenUpdates(); } }#endif break; case QEvent::ParentChange: d->layout->setUsePopupMenu(qobject_cast<QMainWindow*>(parentWidget()) == 0); break; case QEvent::MouseButtonPress: { QMouseEvent *e = static_cast<QMouseEvent*>(event); if (d->layout->handleRect().contains(e->pos())) { d->mousePressEvent(e); return true; } break; } case QEvent::MouseButtonRelease: if (d->state != 0) { d->mouseReleaseEvent(static_cast<QMouseEvent*>(event)); return true; } break; case QEvent::HoverMove: {#ifndef QT_NO_CURSOR QHoverEvent *e = static_cast<QHoverEvent*>(event); if (d->layout->handleRect().contains(e->pos())) setCursor(Qt::SizeAllCursor); else unsetCursor();#endif break; } case QEvent::MouseMove: { QMouseEvent *e = static_cast<QMouseEvent*>(event); if (d->state != 0) { d->mouseMoveEvent(e); return true; } } break; case QEvent::Leave: if (d->state != 0 && d->state->dragging) {#ifdef Q_OS_WIN // This is a workaround for loosing the mouse on Vista. QPoint pos = QCursor::pos(); QMouseEvent fake(QEvent::MouseMove, mapFromGlobal(pos), pos, Qt::NoButton, QApplication::mouseButtons(), QApplication::keyboardModifiers()); d->mouseMoveEvent(&fake);#endif } else { if (!d->layout->expanded) break; QWidget *w = qApp->activePopupWidget(); if (waitForPopup(this, w)) { d->waitForPopupTimer->start(); break; } d->waitForPopupTimer->stop(); d->layout->setExpanded(false); break; } default: break; } return QWidget::event(event);}void QToolBarPrivate::_q_waitForPopup(){ Q_Q(QToolBar); QWidget *w = qApp->activePopupWidget(); if (!waitForPopup(q, w)) { waitForPopupTimer->stop(); if (!q->underMouse()) layout->setExpanded(false); }}/*! Returns a checkable action that can be used to show or hide this toolbar. The action's text is set to the toolbar's window title. \sa QAction::text QWidget::windowTitle*/QAction *QToolBar::toggleViewAction() const{ Q_D(const QToolBar); return d->toggleViewAction; }/*! \fn void QToolBar::setLabel(const QString &label) Use setWindowTitle() instead.*//*! \fn QString QToolBar::label() const Use windowTitle() instead.*//*! \since 4.2 Returns the widget associated with the specified \a action. \sa addWidget()*/QWidget *QToolBar::widgetForAction(QAction *action) const{ Q_D(const QToolBar); int index = d->layout->indexOf(action); if (index == -1) return 0; return d->layout->itemAt(index)->widget();}/*! \internal*/void QToolBar::initStyleOption(QStyleOptionToolBar *option) const{ Q_D(const QToolBar); if (!option) return; option->initFrom(this); if (orientation() == Qt::Horizontal) option->state |= QStyle::State_Horizontal; option->lineWidth = style()->pixelMetric(QStyle::PM_ToolBarFrameWidth, 0, this); option->features = d->layout->movable() ? QStyleOptionToolBar::Movable : QStyleOptionToolBar::None; // if the tool bar is not in a QMainWindow, this will make the painting right option->toolBarArea = Qt::NoToolBarArea; // Add more styleoptions if the toolbar has been added to a mainwindow. QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget()); if (!mainWindow) return; QMainWindowLayout *layout = qobject_cast<QMainWindowLayout *>(mainWindow->layout()); Q_ASSERT_X(layout != 0, "QToolBar::initStyleOption()", "QMainWindow->layout() != QMainWindowLayout"); layout->getStyleOptionInfo(option, const_cast<QToolBar *>(this));}/*! \reimp*/void QToolBar::childEvent(QChildEvent *event) // ### remove me in 5.0{ QWidget::childEvent(event);}/*! \reimp*/void QToolBar::resizeEvent(QResizeEvent *event) // ### remove me in 5.0{ QWidget::resizeEvent(event);}#include "moc_qtoolbar.cpp"#endif // QT_NO_TOOLBAR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -