138 const int count = contentModel->count();
139 if (count <= 0 || !contentItem)
142 const bool horizontal = orientation == Qt::Horizontal;
143 qreal reservedSize = 0;
144 int resizableCount = 0;
146 QList<QQuickItem *> allItems;
147 allItems.reserve(count);
149 for (
int i = 0; i < count; ++i) {
150 QQuickItem *item = q->itemAt(i);
152 QQuickItemPrivate *p = QQuickItemPrivate::get(item);
153 const bool sizeValid = horizontal ? p->widthValid() : p->heightValid();
157 reservedSize += horizontal ? item->width() : item->height();
162 const qreal totalSpacing = qMax(0, count - 1) * spacing;
163 const qreal availableSize = horizontal ? contentItem->width() : contentItem->height();
164 const qreal itemSize = (availableSize - reservedSize - totalSpacing) / qMax(1, resizableCount);
167 for (QQuickItem *item : std::as_const(allItems)) {
168 QQuickItemPrivate *p = QQuickItemPrivate::get(item);
170 if (!p->widthValid()) {
171 item->setWidth(itemSize);
172 p->widthValidFlag =
false;
174 if (!p->heightValid()) {
175 item->setHeight(contentHeight);
176 p->heightValidFlag =
false;
178 item->setY((contentHeight - item->height()) / 2);
181 if (!p->heightValid()) {
182 item->setHeight(itemSize);
183 p->heightValidFlag =
false;
185 if (!p->widthValid()) {
186 item->setWidth(contentWidth);
187 p->widthValidFlag =
false;
189 item->setX((contentWidth - item->width()) / 2);
201 Q_Q(
const QQuickTabBar);
202 const int count = contentModel->count();
203 if (orientation == Qt::Vertical) {
205 for (
int i = 0; i < count; ++i) {
206 QQuickItem *item = q->itemAt(i);
208 maxWidth = qMax(maxWidth, item->implicitWidth());
213 qreal totalWidth = qMax(0, count - 1) * spacing;
214 for (
int i = 0; i < count; ++i) {
215 QQuickItem *item = q->itemAt(i);
217 QQuickItemPrivate *p = QQuickItemPrivate::get(item);
218 if (!p->widthValid())
219 totalWidth += item->implicitWidth();
221 totalWidth += item->width();
229 if (hasContentHeight)
230 return contentHeight;
232 Q_Q(
const QQuickTabBar);
233 const int count = contentModel->count();
234 if (orientation == Qt::Vertical) {
235 qreal totalHeight = qMax(0, count - 1) * spacing;
236 for (
int i = 0; i < count; ++i) {
237 QQuickItem *item = q->itemAt(i);
239 QQuickItemPrivate *p = QQuickItemPrivate::get(item);
240 if (!p->heightValid())
241 totalHeight += item->implicitHeight();
243 totalHeight += item->height();
250 for (
int i = 0; i < count; ++i) {
251 QQuickItem *item = q->itemAt(i);
253 maxHeight = qMax(maxHeight, item->implicitHeight());
282QQuickTabBar::QQuickTabBar(QQuickItem *parent)
283 : QQuickContainer(*(
new QQuickTabBarPrivate), parent)
286 d->changeTypes |= QQuickItemPrivate::Geometry | QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight;
287 setFlag(ItemIsFocusScope);
288 QObjectPrivate::connect(
this, &QQuickTabBar::currentIndexChanged, d, &QQuickTabBarPrivate::updateCurrentItem);
341void QQuickTabBar::setOrientation(Qt::Orientation orientation)
344 if (d->orientation == orientation)
347 d->orientation = orientation;
348 d->updateImplicitContentSize();
349 if (isComponentComplete())
351 emit orientationChanged();
442void QQuickTabBar::itemAdded(
int index, QQuickItem *item)
446 QQuickItemPrivate::get(item)->setCulled(
true);
447 if (QQuickTabButton *button = qobject_cast<QQuickTabButton *>(item))
448 QObjectPrivate::connect(button, &QQuickTabButton::checkedChanged, d, &QQuickTabBarPrivate::updateCurrentIndex);
449 QQuickTabBarAttached *attached = qobject_cast<QQuickTabBarAttached *>(qmlAttachedPropertiesObject<QQuickTabBar>(item));
451 QQuickTabBarAttachedPrivate::get(attached)->update(
this, index);
452 d->updateImplicitContentSize();
453 if (isComponentComplete())
464void QQuickTabBar::itemRemoved(
int index, QQuickItem *item)
468 if (QQuickTabButton *button = qobject_cast<QQuickTabButton *>(item))
469 QObjectPrivate::disconnect(button, &QQuickTabButton::checkedChanged, d, &QQuickTabBarPrivate::updateCurrentIndex);
470 QQuickTabBarAttached *attached = qobject_cast<QQuickTabBarAttached *>(qmlAttachedPropertiesObject<QQuickTabBar>(item));
472 QQuickTabBarAttachedPrivate::get(attached)->update(
nullptr, -1);
473 d->updateImplicitContentSize();
474 if (isComponentComplete())
546 Q_Q(QQuickTabBarAttached);
547 const int oldIndex = index;
548 const QQuickTabBar *oldTabBar = tabBar;
549 const QQuickTabBar::Position oldPos = q->position();
554 if (oldTabBar != newTabBar) {
556 QObject::disconnect(oldTabBar, &QQuickTabBar::positionChanged, q, &QQuickTabBarAttached::positionChanged);
558 QObject::connect(newTabBar, &QQuickTabBar::positionChanged, q, &QQuickTabBarAttached::positionChanged);
559 emit q->tabBarChanged();
562 if (oldIndex != newIndex)
563 emit q->indexChanged();
564 if (oldPos != q->position())
565 emit q->positionChanged();