📄 qtoolbar.cpp
字号:
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); for (int i = 0; i < d->items.size(); ++i) { const QToolBarItem &item = d->items.at(i); if (item.action == action) return item.widget->geometry(); } return QRect();}/*! 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); for (int i = 0; i < d->items.size(); ++i) { const QToolBarItem &item = d->items.at(i); if (item.widget == widget) return item.action; } return 0;}/*! \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(); QToolBarWidgetAction *widgetAction = qobject_cast<QToolBarWidgetAction *>(action); switch (event->type()) { case QEvent::ActionAdded: { Q_ASSERT_X(!widgetAction || d->indexOf(widgetAction) == -1, "QToolBar", "widgets cannot be inserted multiple times"); QToolBarItem item = d->createItem(action); bool visible = item.action->isVisible(); if (widgetAction && widgetAction->parentWidget() != this) { // reparent the action and its widget to this toolbar widgetAction->setParent(this); widgetAction->widget()->setParent(this); } // make sure the layout doesn't show() the widget too soon item.widget->hide(); if (event->before()) { int index = d->indexOf(event->before()); Q_ASSERT_X(index >= 0 && index < d->items.size(), "QToolBar::insertAction", "internal error"); d->items.insert(index, item); qobject_cast<QBoxLayout *>(layout())->insertWidget(index + 1, item.widget); } else { d->items.append(item); qobject_cast<QBoxLayout *>(layout())->insertWidget(d->items.size(), item.widget); } item.widget->setVisible(visible); QApplication::postEvent(this, new QResizeEvent(size(), size())); break; } case QEvent::ActionChanged: { int index = d->indexOf(action); Q_ASSERT_X(index >= 0 && index < d->items.size(), "QToolBar::actionEvent", "internal error"); const QToolBarItem &item = d->items.at(index); if (!item.hidden) { item.widget->setVisible(item.action->isVisible()); } else { // more elephant shaving QApplication::postEvent(this, new QResizeEvent(size(), size())); } break; } case QEvent::ActionRemoved: { int index = d->indexOf(action); Q_ASSERT_X(index >= 0 && index < d->items.size(), "QToolBar::removeAction", "internal error"); QToolBarItem item = d->items.takeAt(index); layout()->removeWidget(item.widget); if (!widgetAction) { // destroy the QToolButton/QToolBarSeparator item.widget->hide(); item.widget->deleteLater(); } else { if (!isHidden() && item.widget->testAttribute(Qt::WA_WState_Created)) item.widget->hide(); } QApplication::postEvent(this, new QResizeEvent(size(), size())); 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: { QStyleOptionToolBar opt = getStyleOption(this); d->layout->setMargin(style()->pixelMetric(QStyle::PM_ToolBarFrameWidth, &opt, this) + style()->pixelMetric(QStyle::PM_ToolBarItemMargin, &opt, this)); d->layout->setSpacing(style()->pixelMetric(QStyle::PM_ToolBarItemSpacing, &opt, this)); break; } default: break; } QWidget::changeEvent(event);}/*! \reimp */void QToolBar::childEvent(QChildEvent *event){ Q_D(QToolBar); QWidget *widget = qobject_cast<QWidget *>(event->child()); if (widget && event->type() == QEvent::ChildRemoved) { for (int i = 0; i < d->items.size(); ++i) { const QToolBarItem &item = d->items.at(i); QToolBarWidgetAction *widgetAction = 0; if (item.widget == widget && (widgetAction = qobject_cast<QToolBarWidgetAction *>(item.action))) { removeAction(widgetAction); // ### should we delete the action, or is it the programmers reponsibility? // delete widgetAction; } } } QWidget::childEvent(event);}/*! \reimp */void QToolBar::paintEvent(QPaintEvent *event){ Q_UNUSED(event); QPainter p(this); QStyleOptionToolBar opt = getStyleOption(this); style()->drawControl(QStyle::CE_ToolBar, &opt, &p, this);}/*! \reimp */void QToolBar::resizeEvent(QResizeEvent *event){ Q_D(QToolBar); if (d->inResizeEvent) return; d->inResizeEvent = true; QBoxLayout *box = qobject_cast<QBoxLayout *>(layout()); Qt::Orientation orientation = (box->direction() == QBoxLayout::LeftToRight || box->direction() == QBoxLayout::RightToLeft) ? Qt::Horizontal : Qt::Vertical; const int margin = box->margin(); int i = 0; int extension_size = 0; int hidden_count = 0; int max_item_extent = 0; i = d->items.size(); // note: the toolbar handle is not counted while (i > 0) { QWidget *w = box->itemAt(i)->widget(); bool hide = false; if (QApplication::layoutDirection() == Qt::RightToLeft && orientation == Qt::Horizontal) { if (w->isHidden()) { if (box->itemAt(i-1) && !box->itemAt(i-1)->widget()->isHidden()) { QWidget *pw = box->itemAt(i-1)->widget(); hide = pw->pos().x() < (extension_size + w->size().width() + margin + box->spacing()); } else { // calculate the pos of the hidden item int pos = 0; for (int k = 1; k < i; ++k) { // idx 0 == handle QWidget * pw = box->itemAt(k)->widget(); if (pw == w) break; pos = pw->isHidden() ? pos - pw->size().width() - box->spacing() : pw->pos().x(); } pos = pos - w->size().width() - box->spacing(); hide = pos < extension_size + margin; } } else { hide = w->pos().x() < extension_size + margin; } } else { hide = pick(orientation, w->pos()) + pick(orientation, w->size()) >= pick(orientation, size()) - extension_size; } if (hide && i > 1) { // never hide the first item in the tb w->hide(); if (d->items[i - 1].action->isVisible()) { d->items[i - 1].hidden = true; ++hidden_count; } // the size of the extension menu button needs to be // considered when buttons in the toolbar are hidden extension_size = pick(orientation, d->extension->sizeHint()); } else { w->setVisible(d->items[i - 1].action->isVisible()); d->items[i - 1].hidden = false; if (orientation == Qt::Horizontal) max_item_extent = qMax(max_item_extent, w->sizeHint().height()); else max_item_extent = qMax(max_item_extent, w->sizeHint().width()); } --i; } int box_spacing = (d->items.size() > 1) ? box->spacing()*2 : box->spacing(); if (orientation == Qt::Horizontal) { setMinimumSize(d->handle->sizeHint().width() + box_spacing + extension_size + margin*2 + (d->items.isEmpty() ? d->iconSize.width() : d->items[0].widget->minimumSizeHint().width()), max_item_extent + margin*2); } else { setMinimumSize(max_item_extent + margin*2, d->handle->sizeHint().height() + box_spacing + extension_size + margin*2 + (d->items.isEmpty() ? d->iconSize.height() : d->items[0].widget->minimumSizeHint().height())); } if (hidden_count > 0) { if (orientation == Qt::Horizontal) { int x = QApplication::layoutDirection() == Qt::RightToLeft ? margin : width() - d->extension->sizeHint().width() - margin; d->extension->setGeometry(x, margin, d->extension->sizeHint().width(), max_item_extent); } else { d->extension->setGeometry(margin, height() - d->extension->sizeHint().height() - margin, max_item_extent, d->extension->sizeHint().height()); }#ifndef QT_NO_MENU QMenu *pop = d->extension->menu(); if (!pop) { pop = new QMenu(this); d->extension->setMenu(pop); } pop->clear(); for(int i = 0; i < d->items.size(); ++i) { const QToolBarItem &item = d->items.at(i); if (!item.hidden) continue; if (!qobject_cast<QToolBarWidgetAction *>(item.action)) { pop->addAction(item.action); } else {#if !defined(QT_NO_SIGNALMAPPER) && !defined(QT_NO_COMBOBOX) if (QComboBox *cb = qobject_cast<QComboBox *>(item.widget)) { QMenu *cb_menu = new QMenu(cb->windowTitle(), pop); QSignalMapper *cb_mapper = new QSignalMapper(cb_menu); pop->addMenu(cb_menu); for (int i=0; i<cb->count(); ++i) { QAction *ac = cb_menu->addAction(cb->itemIcon(i), cb->itemText(i)); connect(ac, SIGNAL(triggered(bool)), cb_mapper, SLOT(map())); cb_mapper->setMapping(ac, i); } connect(cb_mapper, SIGNAL(mapped(int)), cb, SIGNAL(activated(int))); } else#endif // QT_NO_SIGNALMAPPER if (QToolButton *tb = qobject_cast<QToolButton *>(item.widget)) { QAction *ac = pop->addAction(tb->icon(), tb->text()); connect(ac, SIGNAL(triggered()), tb, SIGNAL(clicked())); } } } if (pop->actions().size() > 0) { d->extension->show(); d->extension->setEnabled(true); } else { // show a disabled ext btn in the case where widgets in // the toolbar are hidden but not put into the ext menu - // this indicates that some items in the tb is hidden d->extension->show(); d->extension->setEnabled(false); }#endif // QT_NO_MENU } else if (!d->extension->isHidden()) { if (d->extension->menu()) d->extension->menu()->clear(); d->extension->hide(); } QWidget::resizeEvent(event); d->inResizeEvent = 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); break; case QEvent::ParentChange: d->handle->setVisible(d->movable && (qobject_cast<QMainWindow *>(parentWidget()) != 0)); break; case QEvent::StyleChange: if (!d->explicitIconSize) setIconSize(QSize()); break; default: break; } return QWidget::event(event);}/*! 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.*/#include "moc_qtoolbar.cpp"#endif // QT_NO_TOOLBAR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -