Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtoolbarlayout.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
5#include <qapplication.h>
6#include <qaction.h>
7#include <qwidgetaction.h>
8#include <qtoolbar.h>
9#include <qstyleoption.h>
10#if QT_CONFIG(toolbutton)
11#include <qtoolbutton.h>
12#endif
13#include <qmenu.h>
14#include <qdebug.h>
15#include <qmath.h>
16#ifdef Q_OS_MACOS
17#include <qpa/qplatformnativeinterface.h>
18#endif
19
21#if QT_CONFIG(toolbutton)
22#include "qtoolbarextension_p.h"
23#endif
26
28
29// qmainwindow.cpp
30extern QMainWindowLayout *qt_mainwindow_layout(const QMainWindow *window);
31
32/******************************************************************************
33** QToolBarItem
34*/
35
36QToolBarItem::QToolBarItem(QWidget *widget)
37 : QWidgetItem(widget), action(nullptr), customWidget(false)
38{
39}
40
41bool QToolBarItem::isEmpty() const
42{
43 return action == nullptr || !action->isVisible();
44}
45
46/******************************************************************************
47** QToolBarLayout
48*/
49
50QToolBarLayout::QToolBarLayout(QWidget *parent)
51 : QLayout(parent), expanded(false), animating(false), dirty(true),
52 expanding(false), empty(true), expandFlag(false), popupMenu(nullptr)
53{
54 QToolBar *tb = qobject_cast<QToolBar*>(parent);
55 if (!tb)
56 return;
57
58 extension = new QToolBarExtension(tb);
59 extension->setFocusPolicy(Qt::NoFocus);
60 extension->hide();
61 QObject::connect(tb, SIGNAL(orientationChanged(Qt::Orientation)),
62 extension, SLOT(setOrientation(Qt::Orientation)));
63
64 setUsePopupMenu(qobject_cast<QMainWindow*>(tb->parentWidget()) == 0);
65}
66
67QToolBarLayout::~QToolBarLayout()
68{
69 while (!items.isEmpty()) {
70 QToolBarItem *item = items.takeFirst();
71 if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction*>(item->action)) {
72 if (item->customWidget)
73 widgetAction->releaseWidget(item->widget());
74 }
75 delete item;
76 }
77}
78
79void QToolBarLayout::updateMarginAndSpacing()
80{
81 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
82 if (!tb)
83 return;
84 QStyle *style = tb->style();
85 QStyleOptionToolBar opt;
86 tb->initStyleOption(&opt);
87 const int margin = style->pixelMetric(QStyle::PM_ToolBarItemMargin, &opt, tb)
88 + style->pixelMetric(QStyle::PM_ToolBarFrameWidth, &opt, tb);
89 setContentsMargins(margin, margin, margin, margin);
90 setSpacing(style->pixelMetric(QStyle::PM_ToolBarItemSpacing, &opt, tb));
91}
92
93bool QToolBarLayout::hasExpandFlag() const
94{
95 return expandFlag;
96}
97
98void QToolBarLayout::setUsePopupMenu(bool set)
99{
100 if (!dirty && ((popupMenu == nullptr) == set))
101 invalidate();
102 if (!set) {
103 QObject::connect(extension, SIGNAL(clicked(bool)),
104 this, SLOT(setExpanded(bool)), Qt::UniqueConnection);
105 extension->setPopupMode(QToolButton::DelayedPopup);
106 extension->setMenu(nullptr);
107 delete popupMenu;
108 popupMenu = nullptr;
109 } else {
110 QObject::disconnect(extension, SIGNAL(clicked(bool)),
111 this, SLOT(setExpanded(bool)));
112 extension->setPopupMode(QToolButton::InstantPopup);
113 if (!popupMenu) {
114 popupMenu = new QMenu(extension);
115 }
116 extension->setMenu(popupMenu);
117 }
118}
119
120void QToolBarLayout::checkUsePopupMenu()
121{
122 QToolBar *tb = static_cast<QToolBar *>(parent());
123 QMainWindow *mw = qobject_cast<QMainWindow *>(tb->parent());
124 Qt::Orientation o = tb->orientation();
125 setUsePopupMenu(!mw || tb->isFloating() || perp(o, expandedSize(mw->size())) >= perp(o, mw->size()));
126}
127
128void QToolBarLayout::addItem(QLayoutItem*)
129{
130 qWarning("QToolBarLayout::addItem(): please use addAction() instead");
131 return;
132}
133
134QLayoutItem *QToolBarLayout::itemAt(int index) const
135{
136 if (index < 0 || index >= items.size())
137 return nullptr;
138 return items.at(index);
139}
140
141QLayoutItem *QToolBarLayout::takeAt(int index)
142{
143 if (index < 0 || index >= items.size())
144 return nullptr;
145 QToolBarItem *item = items.takeAt(index);
146
147 if (popupMenu)
148 popupMenu->removeAction(item->action);
149
150 QWidgetAction *widgetAction = qobject_cast<QWidgetAction*>(item->action);
151 if (widgetAction != nullptr && item->customWidget) {
152 widgetAction->releaseWidget(item->widget());
153 } else {
154 // destroy the QToolButton/QToolBarSeparator
155 item->widget()->hide();
156 item->widget()->deleteLater();
157 }
158
159 invalidate();
160 return item;
161}
162
163void QToolBarLayout::insertAction(int index, QAction *action)
164{
165 index = qMax(0, index);
166 index = qMin(items.size(), index);
167
168 QToolBarItem *item = createItem(action);
169 if (item) {
170 items.insert(index, item);
171 invalidate();
172 }
173}
174
175int QToolBarLayout::indexOf(const QAction *action) const
176{
177 for (int i = 0; i < items.size(); ++i) {
178 if (items.at(i)->action == action)
179 return i;
180 }
181 return -1;
182}
183
184int QToolBarLayout::count() const
185{
186 return items.size();
187}
188
189bool QToolBarLayout::isEmpty() const
190{
191 if (dirty)
192 updateGeomArray();
193 return empty;
194}
195
196void QToolBarLayout::invalidate()
197{
198 dirty = true;
199 QLayout::invalidate();
200}
201
202Qt::Orientations QToolBarLayout::expandingDirections() const
203{
204 if (dirty)
205 updateGeomArray();
206 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
207 if (!tb)
208 return {};
209 Qt::Orientation o = tb->orientation();
210 return expanding ? Qt::Orientations(o) : Qt::Orientations{};
211}
212
213bool QToolBarLayout::movable() const
214{
215 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
216 if (!tb)
217 return false;
218 QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget());
219 return tb->isMovable() && win != nullptr;
220}
221
222void QToolBarLayout::updateGeomArray() const
223{
224 if (!dirty)
225 return;
226
227 QToolBarLayout *that = const_cast<QToolBarLayout*>(this);
228
229 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
230 if (!tb)
231 return;
232 QStyle *style = tb->style();
233 QStyleOptionToolBar opt;
234 tb->initStyleOption(&opt);
235 const int handleExtent = movable()
236 ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0;
237 const QMargins margins = contentsMargins();
238 const int spacing = this->spacing();
239 const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb);
240 Qt::Orientation o = tb->orientation();
241
242 that->minSize = QSize(0, 0);
243 that->hint = QSize(0, 0);
244 rperp(o, that->minSize) = style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb);
245 rperp(o, that->hint) = style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb);
246
247 that->expanding = false;
248 that->empty = false;
249
250 QList<QLayoutStruct> a(items.size() + 1); // + 1 for the stretch
251
252 int count = 0;
253 for (int i = 0; i < items.size(); ++i) {
254 QToolBarItem *item = items.at(i);
255
256 QSize max = item->maximumSize();
257 QSize min = item->minimumSize();
258 QSize hint = item->sizeHint();
259 Qt::Orientations exp = item->expandingDirections();
260 bool empty = item->isEmpty();
261
262 that->expanding = expanding || exp & o;
263
264
265 if (item->widget()) {
266 if ((item->widget()->sizePolicy().horizontalPolicy() & QSizePolicy::ExpandFlag)) {
267 that->expandFlag = true;
268 }
269 }
270
271 if (!empty) {
272 if (count == 0) // the minimum size only displays one widget
273 rpick(o, that->minSize) += pick(o, min);
274 int s = perp(o, minSize);
275 rperp(o, that->minSize) = qMax(s, perp(o, min));
276
277 //we only add spacing before item (ie never before the first one)
278 rpick(o, that->hint) += (count == 0 ? 0 : spacing) + pick(o, hint);
279 s = perp(o, that->hint);
280 rperp(o, that->hint) = qMax(s, perp(o, hint));
281 ++count;
282 }
283
284 a[i].sizeHint = pick(o, hint);
285 a[i].maximumSize = pick(o, max);
286 a[i].minimumSize = pick(o, min);
287 a[i].expansive = exp & o;
288 if (o == Qt::Horizontal)
289 a[i].stretch = item->widget()->sizePolicy().horizontalStretch();
290 else
291 a[i].stretch = item->widget()->sizePolicy().verticalStretch();
292 a[i].empty = empty;
293 }
294
295 that->geomArray = a;
296 that->empty = count == 0;
297
298 rpick(o, that->minSize) += handleExtent;
299 that->minSize += QSize(pick(Qt::Horizontal, margins), pick(Qt::Vertical, margins));
300 if (items.size() > 1)
301 rpick(o, that->minSize) += spacing + extensionExtent;
302
303 rpick(o, that->hint) += handleExtent;
304 that->hint += QSize(pick(Qt::Horizontal, margins), pick(Qt::Vertical, margins));
305 that->dirty = false;
306}
307
309{
310 QWidgetAction *a = qobject_cast<QWidgetAction*>(item->action);
311 return a != nullptr && a->defaultWidget() == item->widget();
312}
313
314void QToolBarLayout::updateMacBorderMetrics()
315{
316#ifdef Q_OS_MACOS
317 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
318 if (!tb)
319 return;
320
321 QRect rect = geometry();
322
323 QMainWindow *mainWindow = qobject_cast<QMainWindow*>(tb->parentWidget());
324 if (!mainWindow || !mainWindow->isWindow() || !mainWindow->unifiedTitleAndToolBarOnMac())
325 return;
326
327 QPlatformNativeInterface *nativeInterface = QApplication::platformNativeInterface();
328 if (!nativeInterface)
329 return; // Not Cocoa platform plugin.
330 QPlatformNativeInterface::NativeResourceForIntegrationFunction function =
331 nativeInterface->nativeResourceFunctionForIntegration("registerContentBorderArea");
332 if (!function)
333 return; // Not Cocoa platform plugin.
334
335 QPoint upper = tb->mapToParent(rect.topLeft());
336 QPoint lower = tb->mapToParent(rect.bottomLeft() + QPoint(0, 1));
337
338 typedef void (*RegisterContentBorderAreaFunction)(QWindow *window, void *identifier, int upper, int lower);
339 if (mainWindow->toolBarArea(tb) == Qt::TopToolBarArea) {
340 (reinterpret_cast<RegisterContentBorderAreaFunction>(QFunctionPointer(function)))(
341 tb->window()->windowHandle(), tb, upper.y(), lower.y());
342 } else {
343 (reinterpret_cast<RegisterContentBorderAreaFunction>(QFunctionPointer(function)))(
344 tb->window()->windowHandle(), tb, 0, 0);
345 }
346#endif
347}
348
349void QToolBarLayout::setGeometry(const QRect &rect)
350{
351 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
352 if (!tb)
353 return;
354 QStyle *style = tb->style();
355 QStyleOptionToolBar opt;
356 tb->initStyleOption(&opt);
357 const QMargins margins = contentsMargins();
358 const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb);
359 Qt::Orientation o = tb->orientation();
360
361 QLayout::setGeometry(rect);
362
363 updateMacBorderMetrics();
364
365 bool ranOutOfSpace = false;
366 if (!animating)
367 ranOutOfSpace = layoutActions(rect.size());
368
369 if (expanded || animating || ranOutOfSpace) {
370 Qt::ToolBarArea area = Qt::TopToolBarArea;
371 if (QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget()))
372 area = win->toolBarArea(tb);
373 QSize hint = sizeHint();
374
375 QPoint pos;
376 rpick(o, pos) = pick(o, rect.bottomRight()) -
377 pick(o, QSize(margins.bottom(), margins.right())) - extensionExtent + 2;
378 if (area == Qt::LeftToolBarArea || area == Qt::TopToolBarArea)
379 rperp(o, pos) = perp(o, rect.topLeft()) +
380 perp(o, QSize(margins.top(), margins.left()));
381 else
382 rperp(o, pos) = perp(o, rect.bottomRight()) -
383 perp(o, QSize(margins.bottom(), margins.right())) -
384 (perp(o, hint) - perp(o, margins)) + 1;
385 QSize size;
386 rpick(o, size) = extensionExtent;
387 rperp(o, size) = perp(o, hint) - perp(o, margins);
388 QRect r(pos, size);
389
390 if (o == Qt::Horizontal)
391 r = QStyle::visualRect(parentWidget()->layoutDirection(), rect, r);
392
393 extension->setGeometry(r);
394
395 if (extension->isHidden())
396 extension->show();
397 } else {
398 if (!extension->isHidden())
399 extension->hide();
400 }
401}
402
403bool QToolBarLayout::layoutActions(const QSize &size)
404{
405 if (dirty)
406 updateGeomArray();
407
408 QRect rect(0, 0, size.width(), size.height());
409
410 QList<QWidget*> showWidgets, hideWidgets;
411
412 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
413 if (!tb)
414 return false;
415 QStyle *style = tb->style();
416 QStyleOptionToolBar opt;
417 tb->initStyleOption(&opt);
418 const int handleExtent = movable()
419 ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0;
420 const QMargins margins = contentsMargins();
421 const int spacing = this->spacing();
422 const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb);
423 Qt::Orientation o = tb->orientation();
424 bool extensionMenuContainsOnlyWidgetActions = true;
425
426 int space = pick(o, rect.size()) - pick(o, margins) - handleExtent;
427 if (space <= 0)
428 return false; // nothing to do.
429
430 if (popupMenu)
431 popupMenu->clear();
432
433 bool ranOutOfSpace = false;
434 int rows = 0;
435 int rowPos = perp(o, rect.topLeft()) + perp(o, QSize(margins.top(), margins.left()));
436 int i = 0;
437 while (i < items.size()) {
438 QList<QLayoutStruct> a = geomArray;
439
440 int start = i;
441 int size = 0;
442 int prev = -1;
443 int rowHeight = 0;
444 int count = 0;
445 int maximumSize = 0;
446 bool expansiveRow = false;
447 for (; i < items.size(); ++i) {
448 if (a[i].empty)
449 continue;
450
451 int newSize = size + (count == 0 ? 0 : spacing) + a[i].minimumSize;
452 if (prev != -1 && newSize > space) {
453 if (rows == 0)
454 ranOutOfSpace = true;
455 // do we have to move the previous item to the next line to make space for
456 // the extension button?
457 if (count > 1 && size + spacing + extensionExtent > space)
458 i = prev;
459 break;
460 }
461
462 if (expanded)
463 rowHeight = qMax(rowHeight, perp(o, items.at(i)->sizeHint()));
464 expansiveRow = expansiveRow || a[i].expansive;
465 size = newSize;
466 maximumSize += spacing + (a[i].expansive ? a[i].maximumSize : a[i].smartSizeHint());
467 prev = i;
468 ++count;
469 }
470
471 // stretch at the end
472 a[i].sizeHint = 0;
473 a[i].maximumSize = QWIDGETSIZE_MAX;
474 a[i].minimumSize = 0;
475 a[i].expansive = true;
476 a[i].stretch = 0;
477 a[i].empty = true;
478
479 if (expansiveRow && maximumSize < space) {
480 expansiveRow = false;
481 a[i].maximumSize = space - maximumSize;
482 }
483
484 qGeomCalc(a, start, i - start + (expansiveRow ? 0 : 1), 0,
485 space - (ranOutOfSpace ? (extensionExtent + spacing) : 0),
486 spacing);
487
488 for (int j = start; j < i; ++j) {
489 QToolBarItem *item = items.at(j);
490
491 if (a[j].empty) {
492 if (!item->widget()->isHidden())
493 hideWidgets << item->widget();
494 continue;
495 }
496
497 QPoint pos;
498 rpick(o, pos) = pick(o, QSize(margins.top(), margins.left())) + handleExtent + a[j].pos;
499 rperp(o, pos) = rowPos;
500 QSize size;
501 rpick(o, size) = a[j].size;
502 if (expanded)
503 rperp(o, size) = rowHeight;
504 else
505 rperp(o, size) = perp(o, rect.size()) - perp(o, margins);
506 QRect r(pos, size);
507
508 if (o == Qt::Horizontal)
509 r = QStyle::visualRect(parentWidget()->layoutDirection(), rect, r);
510
511 item->setGeometry(r);
512
513 if (item->widget()->isHidden())
514 showWidgets << item->widget();
515 }
516
517 if (!expanded) {
518 for (int j = i; j < items.size(); ++j) {
519 QToolBarItem *item = items.at(j);
520 if (!item->widget()->isHidden())
521 hideWidgets << item->widget();
522 if (popupMenu) {
523 if (!defaultWidgetAction(item)) {
524 popupMenu->addAction(item->action);
525 extensionMenuContainsOnlyWidgetActions = false;
526 }
527 }
528 }
529 break;
530 }
531
532 rowPos += rowHeight + spacing;
533 ++rows;
534 }
535
536 // if we are using a popup menu, not the expadning toolbar effect, we cannot move custom
537 // widgets into the menu. If only custom widget actions are chopped off, the popup menu
538 // is empty. So we show the little extension button to show something is chopped off,
539 // but we make it disabled.
540 extension->setEnabled(popupMenu == nullptr || !extensionMenuContainsOnlyWidgetActions);
541
542 // we have to do the show/hide here, because it triggers more calls to setGeometry :(
543 for (int i = 0; i < showWidgets.size(); ++i)
544 showWidgets.at(i)->show();
545 for (int i = 0; i < hideWidgets.size(); ++i)
546 hideWidgets.at(i)->hide();
547
548 return ranOutOfSpace;
549}
550
551QSize QToolBarLayout::expandedSize(const QSize &size) const
552{
553 if (dirty)
554 updateGeomArray();
555
556 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
557 if (!tb)
558 return QSize(0, 0);
559 QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget());
560 Qt::Orientation o = tb->orientation();
561 QStyle *style = tb->style();
562 QStyleOptionToolBar opt;
563 tb->initStyleOption(&opt);
564 const int handleExtent = movable()
565 ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0;
566 const QMargins margins = contentsMargins();
567 const int spacing = this->spacing();
568 const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb);
569
570 int total_w = 0;
571 int count = 0;
572 for (int x = 0; x < items.size(); ++x) {
573 if (!geomArray[x].empty) {
574 total_w += (count == 0 ? 0 : spacing) + geomArray[x].minimumSize;
575 ++count;
576 }
577 }
578 if (count == 0)
579 return QSize(0, 0);
580
581 int min_w = pick(o, size);
582 int rows = (int)qSqrt(qreal(count));
583 if (rows == 1)
584 ++rows; // we want to expand to at least two rows
585 int space = total_w/rows + spacing + extensionExtent;
586 space = qMax(space, min_w - pick(o, margins) - handleExtent);
587 if (win != nullptr)
588 space = qMin(space, pick(o, win->size()) - pick(o, margins) - handleExtent);
589
590 int w = 0;
591 int h = 0;
592 int i = 0;
593 while (i < items.size()) {
594 int count = 0;
595 int size = 0;
596 int prev = -1;
597 int rowHeight = 0;
598 for (; i < items.size(); ++i) {
599 if (geomArray[i].empty)
600 continue;
601
602 int newSize = size + (count == 0 ? 0 : spacing) + geomArray[i].minimumSize;
603 rowHeight = qMax(rowHeight, perp(o, items.at(i)->sizeHint()));
604 if (prev != -1 && newSize > space) {
605 if (count > 1 && size + spacing + extensionExtent > space) {
606 size -= spacing + geomArray[prev].minimumSize;
607 i = prev;
608 }
609 break;
610 }
611
612 size = newSize;
613 prev = i;
614 ++count;
615 }
616
617 w = qMax(size, w);
618 h += rowHeight + spacing;
619 }
620
621 w += pick(Qt::Horizontal, margins) + handleExtent + spacing + extensionExtent;
622 w = qMax(w, min_w);
623 if (win != nullptr)
624 w = qMin(w, pick(o, win->size()));
625 h += pick(Qt::Vertical, margins) - spacing; //there is no spacing before the first row
626
627 QSize result;
628 rpick(o, result) = w;
629 rperp(o, result) = h;
630 return result;
631}
632
633void QToolBarLayout::setExpanded(bool exp)
634{
635 QWidget *tb = qobject_cast<QToolBar*>(parentWidget());
636 if (!tb)
637 return;
638 if (exp == expanded && !tb->isWindow())
639 return;
640
641 expanded = exp;
642 extension->setChecked(expanded);
643
644 if (QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget())) {
645#if !QT_CONFIG(dockwidget)
646 animating = false;
647#else
648 animating = !tb->isWindow() && win->isAnimated();
649#endif
650 QMainWindowLayout *layout = qt_mainwindow_layout(win);
651 if (expanded) {
652 tb->raise();
653 } else {
654 QList<int> path = layout->layoutState.indexOf(tb);
655 if (!path.isEmpty()) {
656 QRect rect = layout->layoutState.itemRect(path);
657 layoutActions(rect.size());
658 }
659 }
660 layout->layoutState.toolBarAreaLayout.apply(animating);
661 }
662}
663
664QSize QToolBarLayout::minimumSize() const
665{
666 if (dirty)
667 updateGeomArray();
668 return minSize;
669}
670
671QSize QToolBarLayout::sizeHint() const
672{
673 if (dirty)
674 updateGeomArray();
675 return hint;
676}
677
678QToolBarItem *QToolBarLayout::createItem(QAction *action)
679{
680 bool customWidget = false;
681 bool standardButtonWidget = false;
682 QWidget *widget = nullptr;
683 QToolBar *tb = qobject_cast<QToolBar*>(parentWidget());
684 if (!tb)
685 return (QToolBarItem *)nullptr;
686
687 if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(action)) {
688 widget = widgetAction->requestWidget(tb);
689 if (widget != nullptr) {
690 widget->setAttribute(Qt::WA_LayoutUsesWidgetRect);
691 customWidget = true;
692 }
693 } else if (action->isSeparator()) {
694 QToolBarSeparator *sep = new QToolBarSeparator(tb);
695 connect(tb, SIGNAL(orientationChanged(Qt::Orientation)),
696 sep, SLOT(setOrientation(Qt::Orientation)));
697 widget = sep;
698 }
699
700 if (!widget) {
701 QToolButton *button = new QToolButton(tb);
702 button->setAutoRaise(true);
703 button->setFocusPolicy(Qt::NoFocus);
704 button->setIconSize(tb->iconSize());
705 button->setToolButtonStyle(tb->toolButtonStyle());
706 QObject::connect(tb, SIGNAL(iconSizeChanged(QSize)),
707 button, SLOT(setIconSize(QSize)));
708 QObject::connect(tb, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
709 button, SLOT(setToolButtonStyle(Qt::ToolButtonStyle)));
710 button->setDefaultAction(action);
711 QObject::connect(button, SIGNAL(triggered(QAction*)), tb, SIGNAL(actionTriggered(QAction*)));
712 widget = button;
713 standardButtonWidget = true;
714 }
715
716 widget->hide();
717 QToolBarItem *result = new QToolBarItem(widget);
718 if (standardButtonWidget)
719 result->setAlignment(Qt::AlignJustify);
720 result->customWidget = customWidget;
721 result->action = action;
722 return result;
723}
724
725QT_END_NAMESPACE
726
727#include "moc_qtoolbarlayout_p.cpp"
bool isEmpty() const override
Implemented in subclasses to return whether this item is empty, i.e.
QToolBarItem(QWidget *widget)
QMainWindowLayout * qt_mainwindow_layout(const QMainWindow *window)
static bool defaultWidgetAction(QToolBarItem *item)