Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquicktabbar.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquicktabbar_p.h"
5#include "qquicktabbutton_p.h"
7
9
14
68{
69public:
70 Q_DECLARE_PUBLIC(QQuickTabBar)
71
72 void updateCurrentItem();
73 void updateCurrentIndex();
74 void updateLayout();
75
76 qreal getContentWidth() const override;
77 qreal getContentHeight() const override;
78
79 void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) override;
82
84
85 bool updatingLayout = false;
87#if QT_CONFIG(wheelevent)
88 QPoint accumulatedAngleDelta;
89#endif
90};
91
93{
94 Q_DECLARE_PUBLIC(QQuickTabBarAttached)
95
96public:
98 {
99 return attached->d_func();
100 }
101
102 void update(QQuickTabBar *tabBar, int index);
103
104 int index = -1;
106};
107
109{
110 QQuickTabButton *button = qobject_cast<QQuickTabButton *>(contentModel->get(currentIndex));
111 if (button)
112 button->setChecked(true);
113}
114
116{
117 Q_Q(QQuickTabBar);
118 QQuickTabButton *button = qobject_cast<QQuickTabButton *>(q->sender());
119 if (button && button->isChecked())
120 q->setCurrentIndex(contentModel->indexOf(button, nullptr));
121}
122
124{
125 Q_Q(QQuickTabBar);
126 const int count = contentModel->count();
127 if (count <= 0 || !contentItem)
128 return;
129
130 qreal reservedWidth = 0;
131 int resizableCount = 0;
132
133 QList<QQuickItem *> allItems;
134 allItems.reserve(count);
135
136 for (int i = 0; i < count; ++i) {
137 QQuickItem *item = q->itemAt(i);
138 if (item) {
140 if (!p->widthValid())
141 ++resizableCount;
142 else
143 reservedWidth += item->width();
144 allItems += item;
145 }
146 }
147
148 const qreal totalSpacing = qMax(0, count - 1) * spacing;
149 const qreal itemWidth = (contentItem->width() - reservedWidth - totalSpacing) / qMax(1, resizableCount);
150
151 updatingLayout = true;
152 for (QQuickItem *item : std::as_const(allItems)) {
154 if (!p->widthValid()) {
155 item->setWidth(itemWidth);
156 p->widthValidFlag = false;
157 }
158 if (!p->heightValid()) {
159 item->setHeight(contentHeight);
160 p->heightValidFlag = false;
161 } else {
162 item->setY((contentHeight - item->height()) / 2);
163 }
164 }
165 updatingLayout = false;
166}
167
169{
170 Q_Q(const QQuickTabBar);
171 const int count = contentModel->count();
172 qreal totalWidth = qMax(0, count - 1) * spacing;
173 for (int i = 0; i < count; ++i) {
174 QQuickItem *item = q->itemAt(i);
175 if (item) {
177 if (!p->widthValid())
178 totalWidth += item->implicitWidth();
179 else
180 totalWidth += item->width();
181 }
182 }
183 return totalWidth;
184}
185
187{
188 Q_Q(const QQuickTabBar);
189 const int count = contentModel->count();
190 qreal maxHeight = 0;
191 for (int i = 0; i < count; ++i) {
192 QQuickItem *item = q->itemAt(i);
193 if (item)
194 maxHeight = qMax(maxHeight, item->implicitHeight());
195 }
196 return maxHeight;
197}
198
208
215
222
231
249{
250 Q_D(const QQuickTabBar);
251 return d->position;
252}
253
255{
256 Q_D(QQuickTabBar);
257 if (d->position == position)
258 return;
259
260 d->position = position;
262}
263
294
296{
297 Q_D(QQuickTabBar);
299 d->updateLayout();
300}
301
303{
304 Q_D(QQuickTabBar);
306 d->updateCurrentItem();
307 d->updateLayout();
308}
309
310void QQuickTabBar::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
311{
312 Q_D(QQuickTabBar);
313 QQuickContainer::geometryChange(newGeometry, oldGeometry);
314 d->updateLayout();
315}
316
318{
319 return qobject_cast<QQuickTabButton *>(item);
320}
321
323{
324 Q_D(QQuickTabBar);
326 QQuickItemPrivate::get(item)->setCulled(true); // QTBUG-55129
327 if (QQuickTabButton *button = qobject_cast<QQuickTabButton *>(item))
329 QQuickTabBarAttached *attached = qobject_cast<QQuickTabBarAttached *>(qmlAttachedPropertiesObject<QQuickTabBar>(item));
330 if (attached)
331 QQuickTabBarAttachedPrivate::get(attached)->update(this, index);
332 d->updateImplicitContentSize();
334 polish();
335}
336
338{
339 QQuickTabBarAttached *attached = qobject_cast<QQuickTabBarAttached *>(qmlAttachedPropertiesObject<QQuickTabBar>(item));
340 if (attached)
341 QQuickTabBarAttachedPrivate::get(attached)->update(this, index);
342}
343
345{
346 Q_D(QQuickTabBar);
348 if (QQuickTabButton *button = qobject_cast<QQuickTabButton *>(item))
350 QQuickTabBarAttached *attached = qobject_cast<QQuickTabBarAttached *>(qmlAttachedPropertiesObject<QQuickTabBar>(item));
351 if (attached)
352 QQuickTabBarAttachedPrivate::get(attached)->update(nullptr, -1);
353 d->updateImplicitContentSize();
355 polish();
356}
357
358#if QT_CONFIG(wheelevent)
359void QQuickTabBar::wheelEvent(QWheelEvent *event)
360{
361 Q_D(QQuickTabBar);
362 QQuickContainer::wheelEvent(event);
363 if (d->wheelEnabled) {
364 d->accumulatedAngleDelta += event->angleDelta();
365 int xSteps = d->accumulatedAngleDelta.x() / QWheelEvent::DefaultDeltasPerStep;
366 int ySteps = d->accumulatedAngleDelta.y() / QWheelEvent::DefaultDeltasPerStep;
367 if (xSteps > 0 || ySteps > 0) {
368 decrementCurrentIndex();
369 d->accumulatedAngleDelta = QPoint();
370 } else if (xSteps < 0 || ySteps < 0) {
371 incrementCurrentIndex();
372 d->accumulatedAngleDelta = QPoint();
373 }
374 }
375}
376#endif
377
382
383#if QT_CONFIG(accessibility)
384QAccessible::Role QQuickTabBar::accessibleRole() const
385{
386 return QAccessible::PageTabList;
387}
388#endif
389
425{
427 const int oldIndex = index;
428 const QQuickTabBar *oldTabBar = tabBar;
429 const QQuickTabBar::Position oldPos = q->position();
430
431 index = newIndex;
432 tabBar = newTabBar;
433
434 if (oldTabBar != newTabBar) {
435 if (oldTabBar)
437 if (newTabBar)
439 emit q->tabBarChanged();
440 }
441
442 if (oldIndex != newIndex)
443 emit q->indexChanged();
444 if (oldPos != q->position())
445 emit q->positionChanged();
446}
447
452
454{
455 Q_D(const QQuickTabBarAttached);
456 return d->index;
457}
458
460{
461 Q_D(const QQuickTabBarAttached);
462 return d->tabBar;
463}
464
466{
467 Q_D(const QQuickTabBarAttached);
468 if (!d->tabBar)
470 return d->tabBar->position();
471}
472
474
475#include "moc_qquicktabbar_p.cpp"
bool isChecked() const
\reentrant
Definition qfont.h:22
void setY(qreal y)
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:299
static bool disconnect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot)
Definition qobject_p.h:328
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
\inmodule QtCore\reentrant
Definition qpoint.h:25
int count() const override
\qmlproperty int QtQml.Models::ObjectModel::count
int indexOf(QObject *object, QObject *objectContext) const override
QQmlObjectModel * contentModel
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void currentIndexChanged()
QQuickDeferredPointer< QQuickItem > contentItem
virtual void itemGeometryChanged(QQuickItem *, QQuickGeometryChange, const QRectF &)
virtual void itemImplicitWidthChanged(QQuickItem *)
virtual void itemImplicitHeightChanged(QQuickItem *)
static QQuickItemPrivate * get(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void setFlag(Flag flag, bool enabled=true)
Enables the specified flag for this item if enabled is true; if enabled is false, the flag is disable...
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
bool isComponentComplete() const
Returns true if construction of the QML component is complete; otherwise returns false.
virtual void updatePolish()
This function should perform any layout as required for this item.
void polish()
Schedules a polish event for this item.
void update(QQuickTabBar *tabBar, int index)
\qmlattachedproperty int QtQuick.Controls::TabBar::index
static QQuickTabBarAttachedPrivate * get(QQuickTabBarAttached *attached)
QQuickTabBarAttached(QObject *parent=nullptr)
QQuickTabBar::Position position
QQuickTabBar * tabBar
Allows the user to switch between different views or subtasks.
void itemImplicitHeightChanged(QQuickItem *item) override
QPalette defaultPalette() const override
qreal getContentHeight() const override
QQuickTabBar::Position position
void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) override
void itemImplicitWidthChanged(QQuickItem *item) override
qreal getContentWidth() const override
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void itemMoved(int index, QQuickItem *item) override
void itemRemoved(int index, QQuickItem *item) override
void setPosition(Position position)
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override
Position position
QFont defaultFont() const override
void updatePolish() override
This function should perform any layout as required for this item.
static QQuickTabBarAttached * qmlAttachedProperties(QObject *object)
QQuickTabBar(QQuickItem *parent=nullptr)
bool isContent(QQuickItem *item) const override
void itemAdded(int index, QQuickItem *item) override
void positionChanged()
static QPalette palette(Scope scope)
static QFont font(Scope scope)
\inmodule QtCore\reentrant
Definition qrect.h:484
QPushButton * button
[2]
Combined button and popup list for selecting options.
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLuint index
[2]
GLenum GLenum GLsizei count
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat p
[1]
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define emit
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
QGraphicsItem * item