📄 qtabwidget.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qtabwidget.h"#ifndef QT_NO_TABWIDGET#include "private/qwidget_p.h"#include "qapplication.h"#include "qbitmap.h"#include "qdesktopwidget.h"#include "qevent.h"#include "qlayout.h"#include "qstackedwidget.h"#include "qstyle.h"#include "qstyleoption.h"#include "qstylepainter.h"#include "qtabbar.h"#include "qtoolbutton.h"/*! \class QTabWidget \brief The QTabWidget class provides a stack of tabbed widgets. \ingroup organizers \ingroup advanced \mainclass A tab widget provides a tab bar (see QTabBar) and a "page area" that is used to display pages related to each tab. By default, the tab bar is shown above the page area, but different configurations are available (see \l{TabPosition}). Each tab is associated with a different widget (called a page). Only the current page is shown in the page area; all the other pages are hidden. The user can show a different page by clicking on its tab or by pressing its Alt+\e{letter} shortcut if it has one. The normal way to use QTabWidget is to do the following: \list 1 \i Create a QTabWidget. \i Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them. \i Insert child widgets into the page widget, using layouts to position them as normal. \i Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut. \endlist The position of the tabs is defined by \l tabPosition, their shape by \l tabShape. The signal currentChanged() is emitted when the user selects a page. The current page index is available as currentIndex(), the current page widget with currentWidget(). You can retrieve a pointer to a page widget with a given index using widget(), and can find the index position of a widget with indexOf(). Use setCurrentWidget() or setCurrentIndex() to show a particular page. You can change a tab's text and icon using setTabText() or setTabIcon(). A tab and its associated page can be removed with removeTab(). Each tab is either enabled or disabled at any given time (see setTabEnabled()). If a tab is enabled, the tab text is drawn normally and the user can select that tab. If it is disabled, the tab is drawn in a different way and the user cannot select that tab. Note that even if a tab is disabled, the page can still be visible, for example if all of the tabs happen to be disabled. Tab widgets can be a very good way to split up a complex dialog. An alternative is to use a QStackedWidget for which you provide some means of navigating between pages, for example, a QToolBar or a QListWidget. Most of the functionality in QTabWidget is provided by a QTabBar (at the top, providing the tabs) and a QStackedWidget (most of the area, organizing the individual pages). \table 100% \row \o \inlineimage windowsxp-tabwidget.png Screenshot of a Windows XP style tab widget \o \inlineimage macintosh-tabwidget.png Screenshot of a Macintosh style tab widget \o \inlineimage plastique-tabwidget.png Screenshot of a Plastique style tab widget \row \o A Windows XP style tab widget. \o A Macintosh style tab widget. \o A Plastique style tab widget. \endtable \sa QTabBar, QStackedWidget, QToolBox, {Tab Dialog Example}*//*! \enum QTabWidget::TabPosition This enum type defines where QTabWidget draws the tab row: \value North The tabs are drawn above the pages. \value South The tabs are drawn below the pages. \value West The tabs are drawn to the left of the pages. \value East The tabs are drawn to the right of the pages. \omitvalue Bottom \omitvalue Top*//*! \enum QTabWidget::TabShape This enum type defines the shape of the tabs: \value Rounded The tabs are drawn with a rounded look. This is the default shape. \value Triangular The tabs are drawn with a triangular look.*//*! \fn void QTabWidget::selected(const QString &tabLabel) This signal is emitted whenever a tab is selected (raised), including during the first show(). You can normally use currentChanged() instead.*//*! \fn void QTabWidget::currentChanged(int index) This signal is emitted whenever the current page index changes. The parameter is the new current page \a index position. \sa currentWidget() currentIndex*/class QTabWidgetPrivate : public QWidgetPrivate{ Q_DECLARE_PUBLIC(QTabWidget)public: QTabWidgetPrivate(); ~QTabWidgetPrivate(); void updateTabBarPosition(); void _q_showTab(int); void _q_removeTab(int); void init(); QTabBar *tabs; QStackedWidget *stack; QRect panelRect; bool dirty; QTabWidget::TabPosition pos; QTabWidget::TabShape shape; int alignment; QWidget *leftCornerWidget; QWidget *rightCornerWidget;};QTabWidgetPrivate::QTabWidgetPrivate() : tabs(0), stack(0), dirty(true), pos(QTabWidget::North), shape(QTabWidget::Rounded), leftCornerWidget(0), rightCornerWidget(0){}QTabWidgetPrivate::~QTabWidgetPrivate(){}void QTabWidgetPrivate::init(){ Q_Q(QTabWidget); stack = new QStackedWidget(q); stack->setObjectName(QLatin1String("qt_tabwidget_stackedwidget")); stack->setLineWidth(0); // hack so that QMacStyle::layoutSpacing() can detect tab widget pages stack->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::TabWidget)); QObject::connect(stack, SIGNAL(widgetRemoved(int)), q, SLOT(_q_removeTab(int))); QTabBar *tabBar = new QTabBar(q); tabBar->setObjectName(QLatin1String("qt_tabwidget_tabbar")); tabBar->setDrawBase(false); q->setTabBar(tabBar);#ifdef Q_OS_TEMP pos = QTabWidget::South;#endif q->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding, QSizePolicy::TabWidget));#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled()) q->setFocusPolicy(Qt::NoFocus); else#endif q->setFocusPolicy(Qt::TabFocus); q->setFocusProxy(tabs);}/*! Initialize \a option with the values from this QTabWidget. This method is useful for subclasses when they need a QStyleOptionTabWidgetFrame, but don't want to fill in all the information themselves. \sa QStyleOption::initFrom() QTabBar::initStyleOption()*/void QTabWidget::initStyleOption(QStyleOptionTabWidgetFrame *option) const{ if (!option) return; Q_D(const QTabWidget); option->initFrom(this); int exth = style()->pixelMetric(QStyle::PM_TabBarBaseHeight, 0, this); QSize t(0, d->stack->frameWidth()); if (d->tabs->isVisibleTo(const_cast<QTabWidget *>(this))) t = d->tabs->sizeHint(); if (d->rightCornerWidget) option->rightCornerWidgetSize = d->rightCornerWidget->sizeHint().boundedTo(t - QSize(exth, exth)); else option->rightCornerWidgetSize = QSize(0, 0); if (d->leftCornerWidget) option->leftCornerWidgetSize = d->leftCornerWidget->sizeHint().boundedTo(t - QSize(exth, exth)); else option->leftCornerWidgetSize = QSize(0, 0); switch (d->pos) { case QTabWidget::North: option->shape = d->shape == QTabWidget::Rounded ? QTabBar::RoundedNorth : QTabBar::TriangularNorth; break; case QTabWidget::South: option->shape = d->shape == QTabWidget::Rounded ? QTabBar::RoundedSouth : QTabBar::TriangularSouth; break; case QTabWidget::West: option->shape = d->shape == QTabWidget::Rounded ? QTabBar::RoundedWest : QTabBar::TriangularWest; break; case QTabWidget::East: option->shape = d->shape == QTabWidget::Rounded ? QTabBar::RoundedEast : QTabBar::TriangularEast; break; } option->tabBarSize = t;}/*! Constructs a tabbed widget with parent \a parent.*/QTabWidget::QTabWidget(QWidget *parent) : QWidget(*new QTabWidgetPrivate, parent, 0){ Q_D(QTabWidget); d->init();}#ifdef QT3_SUPPORT/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QTabWidget::QTabWidget(QWidget *parent, const char *name, Qt::WindowFlags f) : QWidget(*new QTabWidgetPrivate, parent, f){ Q_D(QTabWidget); setObjectName(QString::fromAscii(name)); d->init();}#endif/*! Destroys the tabbed widget.*/QTabWidget::~QTabWidget(){}/*! \fn int QTabWidget::addTab(QWidget *page, const QString &label) Adds a tab with the given \a page and \a label to the tab widget, and returns the index of the tab in the tab bar. If the tab's \a label contains an ampersand, the letter following the ampersand is used as a shortcut for the tab, e.g. if the label is "Bro\&wse" then Alt+W becomes a shortcut which will move the focus to this tab. \note If you call addTab() after show(), the layout system will try to adjust to the changes in it's widgets hierarchy and may cause flicker. To prevent this, you can set the QWidget::updatesEnabled property to false prior to changes; remember to set the property to true when the changes are done, making the widget receive paint events again. \sa insertTab()*/int QTabWidget::addTab(QWidget *child, const QString &label){ return insertTab(-1, child, label);}/*! \fn int QTabWidget::addTab(QWidget *page, const QIcon &icon, const QString &label) \overload Adds a tab with the given \a page, \a icon, and \a label to the tab widget, and returns the index of the tab in the tab bar. This function is the same as addTab(), but with an additional \a icon.*/int QTabWidget::addTab(QWidget *child, const QIcon& icon, const QString &label){ return insertTab(-1, child, icon, label);}/*! \fn int QTabWidget::insertTab(int index, QWidget *page, const QString &label) Inserts a tab with the given \a label and \a page into the tab widget at the specified \a index, and returns the index of the inserted tab in the tab bar. The label is displayed in the tab and may vary in appearance depending on the configuration of the tab widget. If the tab's \a label contains an ampersand, the letter following the ampersand is used as a shortcut for the tab, e.g. if the label is "Bro\&wse" then Alt+W becomes a shortcut which will move the focus to this tab. If \a index is out of range, the tab is simply appended. Otherwise it is inserted at the specified position. If the QTabWidget was empty before this function is called, the new page becomes the current page. Inserting a new tab at an index less than or equal to the current index will increment the current index, but keep the current page. \note If you call insertTab() after show(), the layout system will try to adjust to the changes in it's widgets hierarchy and may cause flicker. To prevent this, you can set the QWidget::updatesEnabled property to false prior to changes; remember to set the property to true when the changes are done, making the widget receive paint events again. \sa addTab()*/int QTabWidget::insertTab(int index, QWidget *w, const QString &label){ return insertTab(index, w, QIcon(), label);}/*! \fn int QTabWidget::insertTab(int index, QWidget *page, const QIcon& icon, const QString &label) \overload Inserts a tab with the given \a label, \a page, and \a icon into the tab widget at the specified \a index, and returns the index of the inserted tab in the tab bar. This function is the same as insertTab(), but with an additional \a icon.*/int QTabWidget::insertTab(int index, QWidget *w, const QIcon& icon, const QString &label){ Q_D(QTabWidget); if(!w) return -1; index = d->tabs->insertTab(index, icon, label); d->stack->insertWidget(index, w); setUpLayout(); tabInserted(index); return index;}/*! Defines a new \a label for the page at position \a index's tab. If the provided text contains an ampersand character ('&'), a shortcut is automatically created for it. The character that follows the '&' will be used as the shortcut key. Any previous shortcut will be overwritten, or cleared if no shortcut is defined by the text. See the \l {QShortcut#mnemonic}{QShortcut} documentation for details (to display an actual ampersand, use '&&').*/void QTabWidget::setTabText(int index, const QString &label)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -