📄 qtabbar.cpp
字号:
void QTabBar::setCurrentIndex(int index){ Q_D(QTabBar); if (d->validIndex(index) && d->currentIndex != index) { d->currentIndex = index; update(); d->makeVisible(index);#ifdef QT3_SUPPORT emit selected(index);#endif emit currentChanged(index); }}/*! \property QTabBar::iconSize \brief The size for icons in the tab bar \since 4.1 The default value is style-dependent.*/QSize QTabBar::iconSize() const{ Q_D(const QTabBar); if (d->iconSize.isValid()) return d->iconSize; 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.unite(d->tabList.at(i).rect); QSize sz = QApplication::globalStrut(); return r.size().expandedTo(sz);}/*!\reimp */QSize QTabBar::minimumSizeHint() const{ Q_D(const QTabBar); if (style()->styleHint(QStyle::SH_TabBar_PreferNoArrows, 0, this)) return sizeHint(); 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());}/*! 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 = d->getStyleOption(index); 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(); return style()->sizeFromContents(QStyle::CT_TabBarTab, &opt, csz, this); } 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(d->indexAtPos(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) { QPainter::setRedirected(theParent, this, pos()); 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; QPaintEvent e(rect); QApplication::sendEvent(theParent, &e); QPainter::restoreRedirected(theParent); } 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 = d->getStyleOption(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 = d->getStyleOption(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); d->refresh(); QWidget::changeEvent(e);}/*! \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 + -