📄 qtabbar.cpp
字号:
int iconExtent = style()->pixelMetric(QStyle::PM_TabBarIconSize); return QSize(iconExtent, iconExtent);}void QTabBar::setIconSize(const QSize &size){ Q_D(QTabBar); d->iconSize = size; d->layoutDirty = true; update(); updateGeometry();}/*! \property QTabBar::count \brief the number of tabs in the tab bar*/int QTabBar::count() const{ Q_D(const QTabBar); return d->tabList.count();}/*!\reimp */QSize QTabBar::sizeHint() const{ Q_D(const QTabBar); if (d->layoutDirty) const_cast<QTabBarPrivate*>(d)->layoutTabs(); QRect r; for (int i = 0; i < d->tabList.count(); ++i) r = r.united(d->tabList.at(i).maxRect); QSize sz = QApplication::globalStrut(); return r.size().expandedTo(sz);}/*!\reimp */QSize QTabBar::minimumSizeHint() const{ Q_D(const QTabBar); if (!d->useScrollButtons) { QRect r; for (int i = 0; i < d->tabList.count(); ++i) r = r.united(d->tabList.at(i).minRect); return r.size().expandedTo(QApplication::globalStrut()); } if (verticalTabs(d->shape)) return QSize(sizeHint().width(), d->rightB->sizeHint().height() * 2 + 75); else return QSize(d->rightB->sizeHint().width() * 2 + 75, sizeHint().height());}static QString computeElidedText(Qt::TextElideMode mode, const QString &text){ if (text.length() <= 7) return text; static const QLatin1String Ellipses("..."); QString ret; switch (mode) { case Qt::ElideRight: ret = text.left(4) + Ellipses; break; case Qt::ElideMiddle: ret = text.left(2) + Ellipses + text.right(2); break; case Qt::ElideLeft: ret = Ellipses + text.right(4); break; case Qt::ElideNone: ret = text; break; } return ret;}QSize QTabBarPrivate::minimumTabSizeHint(int index){ Q_Q(QTabBar); // ### Qt 5: make this a protected virtual function in QTabBar Tab &tab = tabList[index]; QString oldText = tab.text; tab.text = computeElidedText(elideMode, oldText); QSize size = q->tabSizeHint(index); tab.text = oldText; return size;}/*! Returns the size hint for the tab at position \a index.*/QSize QTabBar::tabSizeHint(int index) const{ Q_D(const QTabBar); if (const QTabBarPrivate::Tab *tab = d->at(index)) { QStyleOptionTabV2 opt; initStyleOption(&opt, index); opt.text = d->tabList.at(index).text; QSize iconSize = tab->icon.isNull() ? QSize() : opt.iconSize; int hframe = style()->pixelMetric(QStyle::PM_TabBarTabHSpace, &opt, this); int vframe = style()->pixelMetric(QStyle::PM_TabBarTabVSpace, &opt, this); const QFontMetrics fm = fontMetrics(); QSize csz(fm.size(Qt::TextShowMnemonic, tab->text).width() + iconSize.width() + hframe, qMax(fm.height(), iconSize.height()) + vframe); if (verticalTabs(d->shape)) csz.transpose(); QSize retSize = style()->sizeFromContents(QStyle::CT_TabBarTab, &opt, csz, this); return retSize; } return QSize();}/*! This virtual handler is called after a new tab was added or inserted at position \a index. \sa tabRemoved() */void QTabBar::tabInserted(int index){ Q_UNUSED(index)}/*! This virtual handler is called after a tab was removed from position \a index. \sa tabInserted() */void QTabBar::tabRemoved(int index){ Q_UNUSED(index)}/*! This virtual handler is called whenever the tab layout changes. \sa tabRect() */void QTabBar::tabLayoutChange(){}/*!\reimp */void QTabBar::showEvent(QShowEvent *){ Q_D(QTabBar); if (d->layoutDirty) d->layoutTabs(); if (!d->validIndex(d->currentIndex)) setCurrentIndex(0);}/*!\reimp */bool QTabBar::event(QEvent *e){ Q_D(QTabBar); if (e->type() == QEvent::HoverMove || e->type() == QEvent::HoverEnter) { QHoverEvent *he = static_cast<QHoverEvent *>(e); if (!d->hoverRect.contains(he->pos())) { QRect oldHoverRect = d->hoverRect; for (int i = 0; i < d->tabList.count(); ++i) { QRect area = tabRect(i); if (area.contains(he->pos())) { d->hoverRect = area; break; } } if (he->oldPos() != QPoint(-1, -1)) update(oldHoverRect); update(d->hoverRect); } return true; } else if (e->type() == QEvent::HoverLeave ) { QRect oldHoverRect = d->hoverRect; d->hoverRect = QRect(); update(oldHoverRect); return true;#ifndef QT_NO_TOOLTIP } else if (e->type() == QEvent::ToolTip) { if (const QTabBarPrivate::Tab *tab = d->at(tabAt(static_cast<QHelpEvent*>(e)->pos()))) { if (!tab->toolTip.isEmpty()) { QToolTip::showText(static_cast<QHelpEvent*>(e)->globalPos(), tab->toolTip, this); return true; } }#endif // QT_NO_TOOLTIP#ifndef QT_NO_WHATSTHIS } else if (e->type() == QEvent::QueryWhatsThis) { const QTabBarPrivate::Tab *tab = d->at(d->indexAtPos(static_cast<QHelpEvent*>(e)->pos())); if (!tab || tab->whatsThis.isEmpty()) e->ignore(); return true; } else if (e->type() == QEvent::WhatsThis) { if (const QTabBarPrivate::Tab *tab = d->at(d->indexAtPos(static_cast<QHelpEvent*>(e)->pos()))) { if (!tab->whatsThis.isEmpty()) { QWhatsThis::showText(static_cast<QHelpEvent*>(e)->globalPos(), tab->whatsThis, this); return true; } }#endif // QT_NO_WHATSTHIS#ifndef QT_NO_SHORTCUT } else if (e->type() == QEvent::Shortcut) { QShortcutEvent *se = static_cast<QShortcutEvent *>(e); for (int i = 0; i < d->tabList.count(); ++i) { const QTabBarPrivate::Tab *tab = &d->tabList.at(i); if (tab->shortcutId == se->shortcutId()) { setCurrentIndex(i); return true; } }#endif } return QWidget::event(e);}/*!\reimp */void QTabBar::resizeEvent(QResizeEvent *){ Q_D(QTabBar); d->layoutTabs(); d->makeVisible(d->currentIndex);}/*!\reimp */void QTabBar::paintEvent(QPaintEvent *){ Q_D(QTabBar); QStyleOptionTab tabOverlap; tabOverlap.shape = d->shape; int overlap = style()->pixelMetric(QStyle::PM_TabBarBaseOverlap, &tabOverlap, this); QWidget *theParent = parentWidget(); QStyleOptionTabBarBase optTabBase; optTabBase.init(this); optTabBase.shape = d->shape; if (theParent && overlap > 0) { QRect rect; switch (tabOverlap.shape) { case QTabBar::RoundedNorth: case QTabBar::TriangularNorth: rect.setRect(0, height()-overlap, width(), overlap); break; case QTabBar::RoundedSouth: case QTabBar::TriangularSouth: rect.setRect(0, 0, width(), overlap); break; case QTabBar::RoundedEast: case QTabBar::TriangularEast: rect.setRect(0, 0, overlap, height()); break; case QTabBar::RoundedWest: case QTabBar::TriangularWest: rect.setRect(width()-overlap, 0, overlap, height()); break; } optTabBase.rect = rect; } QStylePainter p(this); int selected = -1; int cut = -1; bool rtl = optTabBase.direction == Qt::RightToLeft; bool verticalTabs = (d->shape == QTabBar::RoundedWest || d->shape == QTabBar::RoundedEast || d->shape == QTabBar::TriangularWest || d->shape == QTabBar::TriangularEast); QStyleOptionTab cutTab; QStyleOptionTab selectedTab; for (int i = 0; i < d->tabList.count(); ++i) { QStyleOptionTabV2 tab; initStyleOption(&tab, i); if (!(tab.state & QStyle::State_Enabled)) { tab.palette.setCurrentColorGroup(QPalette::Disabled); } // If this tab is partially obscured, make a note of it so that we can pass the information // along when we draw the tear. if ((!verticalTabs && (!rtl && tab.rect.left() < 0) || (rtl && tab.rect.right() > width())) || (verticalTabs && tab.rect.top() < 0)) { cut = i; cutTab = tab; } // Don't bother drawing a tab if the entire tab is outside of the visible tab bar. if ((!verticalTabs && (tab.rect.right() < 0 || tab.rect.left() > width())) || (verticalTabs && (tab.rect.bottom() < 0 || tab.rect.top() > height()))) continue; optTabBase.tabBarRect |= tab.rect; if (i == d->currentIndex) { selected = i; selectedTab = tab; optTabBase.selectedTabRect = tab.rect; continue; } p.drawControl(QStyle::CE_TabBarTab, tab); } // Draw the selected tab last to get it "on top" if (selected >= 0) { QStyleOptionTabV2 tab; initStyleOption(&tab, selected); p.drawControl(QStyle::CE_TabBarTab, tab); } if (d->drawBase) p.drawPrimitive(QStyle::PE_FrameTabBarBase, optTabBase); // Only draw the tear indicator if necessary. Most of the time we don't need too. if (d->leftB->isVisible() && cut >= 0) { cutTab.rect = rect(); cutTab.rect = style()->subElementRect(QStyle::SE_TabBarTearIndicator, &cutTab, this); p.drawPrimitive(QStyle::PE_IndicatorTabTear, cutTab); }}/*!\reimp */void QTabBar::mousePressEvent (QMouseEvent *e){ Q_D(QTabBar); if (e->button() != Qt::LeftButton) { e->ignore(); return; } d->pressedIndex = d->indexAtPos(e->pos()); if (d->pressedIndex >= 0) { if (e->type() == style()->styleHint(QStyle::SH_TabBar_SelectMouseType, 0, this)) setCurrentIndex(d->pressedIndex); else repaint(tabRect(d->pressedIndex)); }}/*!\reimp */void QTabBar::mouseMoveEvent (QMouseEvent *e){ Q_D(QTabBar); if (e->buttons() != Qt::LeftButton) { e->ignore(); return; } if (style()->styleHint(QStyle::SH_TabBar_SelectMouseType, 0, this) == QEvent::MouseButtonRelease) { int i = d->indexAtPos(e->pos()); if (i != d->pressedIndex) { int oldIndex = d->pressedIndex; d->pressedIndex = -1; if (oldIndex >= 0) repaint(tabRect(oldIndex)); if ((d->pressedIndex = i) >= 0) repaint(tabRect(i)); } }}/*!\reimp */void QTabBar::mouseReleaseEvent (QMouseEvent *e){ Q_D(QTabBar); if (e->button() != Qt::LeftButton) e->ignore(); int i = d->indexAtPos(e->pos()) == d->pressedIndex ? d->pressedIndex : -1; d->pressedIndex = -1; if (e->type() == style()->styleHint(QStyle::SH_TabBar_SelectMouseType, 0, this)) setCurrentIndex(i);}/*!\reimp */void QTabBar::keyPressEvent(QKeyEvent *e){ Q_D(QTabBar); if (e->key() != Qt::Key_Left && e->key() != Qt::Key_Right) { e->ignore(); return; } int dx = e->key() == (isRightToLeft() ? Qt::Key_Right : Qt::Key_Left) ? -1 : 1; for (int index = d->currentIndex + dx; d->validIndex(index); index += dx) { if (d->tabList.at(index).enabled) { setCurrentIndex(index); break; } }}/*!\reimp */void QTabBar::changeEvent(QEvent *e){ Q_D(QTabBar); if (e->type() == QEvent::StyleChange) { d->elideMode = Qt::TextElideMode(style()->styleHint(QStyle::SH_TabBar_ElideMode, 0, this)); d->useScrollButtons = !style()->styleHint(QStyle::SH_TabBar_PreferNoArrows, 0, this); } d->refresh(); QWidget::changeEvent(e);}/*! \property QTabBar::elideMode \brief how to elide text in the tab bar \since 4.2 This property controls how items are elided when there is not enough space to show them for a given tab bar size. By default the value is style dependent. \sa QTabWidget::elideMode usesScrollButtons QStyle::SH_TabBar_ElideMode*/Qt::TextElideMode QTabBar::elideMode() const{ Q_D(const QTabBar); return d->elideMode;}void QTabBar::setElideMode(Qt::TextElideMode mode){ Q_D(QTabBar); d->elideMode = mode;}/*! \property QTabBar::usesScrollButtons \brief Whether or not a tab bar should use buttons to scroll tabs when it has many tabs. \since 4.2 When there are too many tabs in a tab bar for its size, the tab bar can either choose to expand it's size or to add buttons that allow you to scroll through the tabs. By default the value is style dependant. \sa elideMode QTabWidget::usesScrollButtons QStyle::SH_TabBar_PreferNoArrows*/bool QTabBar::usesScrollButtons() const{ return d_func()->useScrollButtons;}void QTabBar::setUsesScrollButtons(bool useButtons){ Q_D(QTabBar); if (d->useScrollButtons == useButtons) return; d->useScrollButtons = useButtons; d->refresh();}/*! \fn void QTabBar::setCurrentTab(int index) Use setCurrentIndex() instead.*//*! \fn void QTabBar::selected(int index); Use currentChanged() instead.*/#include "moc_qtabbar.cpp"#endif // QT_NO_TABBAR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -