1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
44 #ifndef QT_NO_GRAPHICSVIEW
46 #include "qgraphicswidget.h"
47 #include "qgraphicswidget_p.h"
48 #include "qgraphicslayout.h"
49 #include "qgraphicslayout_p.h"
50 #include "qgraphicsscene.h"
51 #include "qgraphicssceneevent.h"
54 #include <private/qaction_p.h>
56 #include <private/qapplication_p.h>
57 #include <private/qgraphicsscene_p.h>
58 #ifndef QT_NO_SHORTCUT
59 #include <private/qshortcutmap_p.h>
61 #include <QtCore/qmutex.h>
62 #include <QtWidgets/qapplication.h>
63 #include <QtWidgets/qgraphicsview.h>
64 #include <QtWidgets/qgraphicsproxywidget.h>
65 #include <QtGui/qpalette.h>
66 #include <QtWidgets/qstyleoption.h>
73 \class QGraphicsWidget
74 \brief The QGraphicsWidget class is the base class for all widget
75 items in a QGraphicsScene.
77 \ingroup graphicsview-api
80 QGraphicsWidget is an extended base item that provides extra functionality
81 over QGraphicsItem. It is similar to QWidget in many ways:
84 \li Provides a \l palette, a \l font and a \l style().
85 \li Has a defined geometry().
86 \li Supports layouts with setLayout() and layout().
87 \li Supports shortcuts and actions with grabShortcut() and insertAction()
90 Unlike QGraphicsItem, QGraphicsWidget is not an abstract class; you can
91 create instances of a QGraphicsWidget without having to subclass it.
92 This approach is useful for widgets that only serve the purpose of
93 organizing child widgets into a layout.
95 QGraphicsWidget can be used as a base item for your own custom item if
96 you require advanced input focus handling, e.g., tab focus and activation, or
99 Since QGraphicsWidget resembles QWidget and has similar API, it is
100 easier to port a widget from QWidget to QGraphicsWidget, instead of
103 \note QWidget-based widgets can be directly embedded into a
104 QGraphicsScene using QGraphicsProxyWidget.
106 Noticeable differences between QGraphicsWidget and QWidget are:
109 \header \li QGraphicsWidget
111 \row \li Coordinates and geometry are defined with qreals (doubles or
112 floats, depending on the platform).
113 \li QWidget uses integer geometry (QPoint, QRect).
114 \row \li The widget is already visible by default; you do not have to
115 call show() to display the widget.
116 \li QWidget is hidden by default until you call show().
117 \row \li A subset of widget attributes are supported.
118 \li All widget attributes are supported.
119 \row \li A top-level item's style defaults to QGraphicsScene::style
120 \li A top-level widget's style defaults to QApplication::style
121 \row \li Graphics View provides a custom drag and drop framework, different
123 \li Standard drag and drop framework.
124 \row \li Widget items do not support modality.
125 \li Full modality support.
128 QGraphicsWidget supports a subset of Qt's widget attributes,
129 (Qt::WidgetAttribute), as shown in the table below. Any attributes not
130 listed in this table are unsupported, or otherwise unused.
133 \header \li Widget Attribute \li Usage
134 \row \li Qt::WA_SetLayoutDirection
135 \li Set by setLayoutDirection(), cleared by
136 unsetLayoutDirection(). You can test this attribute to
137 check if the widget has been explicitly assigned a
138 \l{QGraphicsWidget::layoutDirection()}
139 {layoutDirection}. If the attribute is not set, the
140 \l{QGraphicsWidget::layoutDirection()}
141 {layoutDirection()} is inherited.
142 \row \li Qt::WA_RightToLeft
143 \li Toggled by setLayoutDirection(). Inherited from the
144 parent/scene. If set, the widget's layout will order
145 horizontally arranged widgets from right to left.
146 \row \li Qt::WA_SetStyle
147 \li Set and cleared by setStyle(). If this attribute is
148 set, the widget has been explicitly assigned a style.
149 If it is unset, the widget will use the scene's or the
151 \row \li Qt::WA_Resized
152 \li Set by setGeometry() and resize().
153 \row \li Qt::WA_SetPalette
154 \li Set by setPalette().
155 \row \li Qt::WA_SetFont
156 \li Set by setFont().
157 \row \li Qt::WA_WindowPropagation
158 \li Enables propagation to window widgets.
161 Although QGraphicsWidget inherits from both QObject and QGraphicsItem,
162 you should use the functions provided by QGraphicsItem, \e not QObject, to
163 manage the relationships between parent and child items. These functions
164 control the stacking order of items as well as their ownership.
166 \note The QObject::parent() should always return 0 for QGraphicsWidgets,
167 but this policy is not strictly defined.
169 \sa QGraphicsProxyWidget, QGraphicsItem, {Widgets and Layouts}
173 Constructs a QGraphicsWidget instance. The optional \a parent argument is
174 passed to QGraphicsItem's constructor. The optional \a wFlags argument
175 specifies the widget's window flags (e.g., whether the widget should be a
176 window, a tool, a popup, etc).
178 QGraphicsWidget::QGraphicsWidget(QGraphicsItem *parent, Qt::WindowFlags wFlags)
179 : QGraphicsObject(*new QGraphicsWidgetPrivate, 0), QGraphicsLayoutItem(0, false)
181 Q_D(QGraphicsWidget);
182 d->init(parent, wFlags);
188 Constructs a new QGraphicsWidget, using \a dd as parent.
190 QGraphicsWidget::QGraphicsWidget(QGraphicsWidgetPrivate &dd, QGraphicsItem *parent, Qt::WindowFlags wFlags)
191 : QGraphicsObject(dd, 0), QGraphicsLayoutItem(0, false)
193 Q_D(QGraphicsWidget);
194 d->init(parent, wFlags);
199 \class QGraphicsWidgetStyles
201 We use this thread-safe class to maintain a hash of styles for widgets
202 styles. Note that QApplication::style() itself isn't thread-safe, QStyle
203 isn't thread-safe, and we don't have a thread-safe factory for creating
204 the default style, nor cloning a style.
206 class QGraphicsWidgetStyles
209 QStyle *styleForWidget(const QGraphicsWidget *widget) const
211 QMutexLocker locker(&mutex);
212 return styles.value(widget, 0);
215 void setStyleForWidget(QGraphicsWidget *widget, QStyle *style)
217 QMutexLocker locker(&mutex);
219 styles[widget] = style;
221 styles.remove(widget);
225 QHash<const QGraphicsWidget *, QStyle *> styles;
226 mutable QMutex mutex;
228 Q_GLOBAL_STATIC(QGraphicsWidgetStyles, widgetStyles)
231 Destroys the QGraphicsWidget instance.
233 QGraphicsWidget::~QGraphicsWidget()
235 Q_D(QGraphicsWidget);
237 // Remove all actions from this widget
238 for (int i = 0; i < d->actions.size(); ++i) {
239 QActionPrivate *apriv = d->actions.at(i)->d_func();
240 apriv->graphicsWidgets.removeAll(this);
245 if (QGraphicsScene *scn = scene()) {
246 QGraphicsScenePrivate *sceneD = scn->d_func();
247 if (sceneD->tabFocusFirst == this)
248 sceneD->tabFocusFirst = (d->focusNext == this ? 0 : d->focusNext);
250 d->focusPrev->d_func()->focusNext = d->focusNext;
251 d->focusNext->d_func()->focusPrev = d->focusPrev;
253 // Play it really safe
259 //we check if we have a layout previously
261 QGraphicsLayout *temp = d->layout;
262 foreach (QGraphicsItem * item, childItems()) {
263 // In case of a custom layout which doesn't remove and delete items, we ensure that
264 // the parent layout item does not point to the deleted layout. This code is here to
265 // avoid regression from 4.4 to 4.5, because according to 4.5 docs it is not really needed.
266 if (item->isWidget()) {
267 QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item);
268 if (widget->parentLayoutItem() == d->layout)
269 widget->setParentLayoutItem(0);
276 // Remove this graphics widget from widgetStyles
277 widgetStyles()->setStyleForWidget(this, 0);
281 \property QGraphicsWidget::size
282 \brief the size of the widget
284 Calling resize() resizes the widget to a \a size bounded by minimumSize()
285 and maximumSize(). This property only affects the widget's width and
286 height (e.g., its right and bottom edges); the widget's position and
287 top-left corner remains unaffected.
289 Resizing a widget triggers the widget to immediately receive a
290 \l{QEvent::GraphicsSceneResize}{GraphicsSceneResize} event with the
291 widget's old and new size. If the widget has a layout assigned when this
292 event arrives, the layout will be activated and it will automatically
293 update any child widgets's geometry.
295 This property does not affect any layout of the parent widget. If the
296 widget itself is managed by a parent layout; e.g., it has a parent widget
297 with a layout assigned, that layout will not activate.
299 By default, this property contains a size with zero width and height.
301 \sa setGeometry(), QGraphicsSceneResizeEvent, QGraphicsLayout
303 QSizeF QGraphicsWidget::size() const
305 return QGraphicsLayoutItem::geometry().size();
308 void QGraphicsWidget::resize(const QSizeF &size)
310 setGeometry(QRectF(pos(), size));
314 \fn void QGraphicsWidget::resize(qreal w, qreal h)
316 This convenience function is equivalent to calling resize(QSizeF(w, h)).
318 \sa setGeometry(), setTransform()
322 \property QGraphicsWidget::sizePolicy
323 \brief the size policy for the widget
324 \sa sizePolicy(), setSizePolicy(), QWidget::sizePolicy()
328 \fn QGraphicsWidget::geometryChanged()
330 This signal gets emitted whenever the geometry is changed in setGeometry().
334 \property QGraphicsWidget::geometry
335 \brief the geometry of the widget
337 Sets the item's geometry to \a rect. The item's position and size are
338 modified as a result of calling this function. The item is first moved,
341 A side effect of calling this function is that the widget will receive
342 a move event and a resize event. Also, if the widget has a layout
343 assigned, the layout will activate.
345 \sa geometry(), resize()
347 void QGraphicsWidget::setGeometry(const QRectF &rect)
349 QGraphicsWidgetPrivate *wd = QGraphicsWidget::d_func();
350 QGraphicsLayoutItemPrivate *d = QGraphicsLayoutItem::d_ptr.data();
352 QPointF oldPos = d->geom.topLeft();
354 setAttribute(Qt::WA_Resized);
356 newGeom.setSize(rect.size().expandedTo(effectiveSizeHint(Qt::MinimumSize))
357 .boundedTo(effectiveSizeHint(Qt::MaximumSize)));
359 if (newGeom == d->geom) {
360 goto relayoutChildrenAndReturn;
363 // setPos triggers ItemPositionChange, which can adjust position
364 wd->inSetGeometry = 1;
365 setPos(newGeom.topLeft());
366 wd->inSetGeometry = 0;
367 newGeom.moveTopLeft(pos());
369 if (newGeom == d->geom) {
370 goto relayoutChildrenAndReturn;
373 // Update and prepare to change the geometry (remove from index) if the size has changed.
375 if (rect.topLeft() == d->geom.topLeft()) {
376 prepareGeometryChange();
381 // Update the layout item geometry
383 bool moved = oldPos != pos();
386 QGraphicsSceneMoveEvent event;
387 event.setOldPos(oldPos);
388 event.setNewPos(pos());
389 QApplication::sendEvent(this, &event);
392 d->geom.moveTopLeft(pos());
393 emit geometryChanged();
394 goto relayoutChildrenAndReturn;
397 QSizeF oldSize = size();
398 QGraphicsLayoutItem::setGeometry(newGeom);
400 bool resized = newGeom.size() != oldSize;
402 QGraphicsSceneResizeEvent re;
403 re.setOldSize(oldSize);
404 re.setNewSize(newGeom.size());
405 if (oldSize.width() != newGeom.size().width())
407 if (oldSize.height() != newGeom.size().height())
408 emit heightChanged();
409 QGraphicsLayout *lay = wd->layout;
410 if (QGraphicsLayout::instantInvalidatePropagation()) {
411 if (!lay || lay->isActivated()) {
412 QApplication::sendEvent(this, &re);
415 QApplication::sendEvent(this, &re);
420 emit geometryChanged();
421 relayoutChildrenAndReturn:
422 if (QGraphicsLayout::instantInvalidatePropagation()) {
423 if (QGraphicsLayout *lay = wd->layout) {
424 if (!lay->isActivated()) {
425 QEvent layoutRequest(QEvent::LayoutRequest);
426 QApplication::sendEvent(this, &layoutRequest);
433 \fn QRectF QGraphicsWidget::rect() const
435 Returns the item's local rect as a QRectF. This function is equivalent
436 to QRectF(QPointF(), size()).
438 \sa setGeometry(), resize()
442 \fn void QGraphicsWidget::setGeometry(qreal x, qreal y, qreal w, qreal h)
444 This convenience function is equivalent to calling setGeometry(QRectF(
445 \a x, \a y, \a w, \a h)).
447 \sa geometry(), resize()
451 \property QGraphicsWidget::minimumSize
452 \brief the minimum size of the widget
454 \sa setMinimumSize(), minimumSize(), preferredSize, maximumSize
458 \property QGraphicsWidget::preferredSize
459 \brief the preferred size of the widget
461 \sa setPreferredSize(), preferredSize(), minimumSize, maximumSize
465 \property QGraphicsWidget::maximumSize
466 \brief the maximum size of the widget
468 \sa setMaximumSize(), maximumSize(), minimumSize, preferredSize
472 Sets the widget's contents margins to \a left, \a top, \a right and \a
475 Contents margins are used by the assigned layout to define the placement
476 of subwidgets and layouts. Margins are particularly useful for widgets
477 that constrain subwidgets to only a section of its own geometry. For
478 example, a group box with a layout will place subwidgets inside its frame,
481 Changing a widget's contents margins will always trigger an update(), and
482 any assigned layout will be activated automatically. The widget will then
483 receive a \l{QEvent::ContentsRectChange}{ContentsRectChange} event.
485 \sa getContentsMargins(), setGeometry()
487 void QGraphicsWidget::setContentsMargins(qreal left, qreal top, qreal right, qreal bottom)
489 Q_D(QGraphicsWidget);
491 if (!d->margins && left == 0 && top == 0 && right == 0 && bottom == 0)
494 if (left == d->margins[d->Left]
495 && top == d->margins[d->Top]
496 && right == d->margins[d->Right]
497 && bottom == d->margins[d->Bottom])
500 d->margins[d->Left] = left;
501 d->margins[d->Top] = top;
502 d->margins[d->Right] = right;
503 d->margins[d->Bottom] = bottom;
505 if (QGraphicsLayout *l = d->layout)
510 QEvent e(QEvent::ContentsRectChange);
511 QApplication::sendEvent(this, &e);
515 Gets the widget's contents margins. The margins are stored in \a left, \a
516 top, \a right and \a bottom, as pointers to qreals. Each argument can
517 be \e {omitted} by passing 0.
519 \sa setContentsMargins()
521 void QGraphicsWidget::getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
523 Q_D(const QGraphicsWidget);
524 if (left || top || right || bottom)
527 *left = d->margins[d->Left];
529 *top = d->margins[d->Top];
531 *right = d->margins[d->Right];
533 *bottom = d->margins[d->Bottom];
537 Sets the widget's window frame margins to \a left, \a top, \a right and
538 \a bottom. The default frame margins are provided by the style, and they
539 depend on the current window flags.
541 If you would like to draw your own window decoration, you can set your
542 own frame margins to override the default margins.
544 \sa unsetWindowFrameMargins(), getWindowFrameMargins(), windowFrameRect()
546 void QGraphicsWidget::setWindowFrameMargins(qreal left, qreal top, qreal right, qreal bottom)
548 Q_D(QGraphicsWidget);
550 if (!d->windowFrameMargins && left == 0 && top == 0 && right == 0 && bottom == 0)
552 d->ensureWindowFrameMargins();
554 d->windowFrameMargins[d->Left] == left
555 && d->windowFrameMargins[d->Top] == top
556 && d->windowFrameMargins[d->Right] == right
557 && d->windowFrameMargins[d->Bottom] == bottom;
558 if (d->setWindowFrameMargins && unchanged)
561 prepareGeometryChange();
562 d->windowFrameMargins[d->Left] = left;
563 d->windowFrameMargins[d->Top] = top;
564 d->windowFrameMargins[d->Right] = right;
565 d->windowFrameMargins[d->Bottom] = bottom;
566 d->setWindowFrameMargins = true;
570 Gets the widget's window frame margins. The margins are stored in \a left,
571 \a top, \a right and \a bottom as pointers to qreals. Each argument can
572 be \e {omitted} by passing 0.
574 \sa setWindowFrameMargins(), windowFrameRect()
576 void QGraphicsWidget::getWindowFrameMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
578 Q_D(const QGraphicsWidget);
579 if (left || top || right || bottom)
580 d->ensureWindowFrameMargins();
582 *left = d->windowFrameMargins[d->Left];
584 *top = d->windowFrameMargins[d->Top];
586 *right = d->windowFrameMargins[d->Right];
588 *bottom = d->windowFrameMargins[d->Bottom];
592 Resets the window frame margins to the default value, provided by the style.
594 \sa setWindowFrameMargins(), getWindowFrameMargins(), windowFrameRect()
596 void QGraphicsWidget::unsetWindowFrameMargins()
598 Q_D(QGraphicsWidget);
599 if ((d->windowFlags & Qt::Window) && (d->windowFlags & Qt::WindowType_Mask) != Qt::Popup &&
600 (d->windowFlags & Qt::WindowType_Mask) != Qt::ToolTip && !(d->windowFlags & Qt::FramelessWindowHint)) {
601 QStyleOptionTitleBar bar;
602 d->initStyleOptionTitleBar(&bar);
603 QStyle *style = this->style();
604 qreal margin = style->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth);
605 qreal titleBarHeight = d->titleBarHeight(bar);
606 setWindowFrameMargins(margin, titleBarHeight, margin, margin);
608 setWindowFrameMargins(0, 0, 0, 0);
610 d->setWindowFrameMargins = false;
614 Returns the widget's geometry in parent coordinates including any window
617 \sa windowFrameRect(), getWindowFrameMargins(), setWindowFrameMargins()
619 QRectF QGraphicsWidget::windowFrameGeometry() const
621 Q_D(const QGraphicsWidget);
622 return d->windowFrameMargins
623 ? geometry().adjusted(-d->windowFrameMargins[d->Left], -d->windowFrameMargins[d->Top],
624 d->windowFrameMargins[d->Right], d->windowFrameMargins[d->Bottom])
629 Returns the widget's local rect including any window frame.
631 \sa windowFrameGeometry(), getWindowFrameMargins(), setWindowFrameMargins()
633 QRectF QGraphicsWidget::windowFrameRect() const
635 Q_D(const QGraphicsWidget);
636 return d->windowFrameMargins
637 ? rect().adjusted(-d->windowFrameMargins[d->Left], -d->windowFrameMargins[d->Top],
638 d->windowFrameMargins[d->Right], d->windowFrameMargins[d->Bottom])
643 Populates a style option object for this widget based on its current
644 state, and stores the output in \a option. The default implementation
645 populates \a option with the following properties.
649 \li Style Option Property
652 \li state & QStyle::State_Enabled
653 \li Corresponds to QGraphicsItem::isEnabled().
655 \li state & QStyle::State_HasFocus
656 \li Corresponds to QGraphicsItem::hasFocus().
658 \li state & QStyle::State_MouseOver
659 \li Corresponds to QGraphicsItem::isUnderMouse().
662 \li Corresponds to QGraphicsWidget::layoutDirection().
665 \li Corresponds to QGraphicsWidget::rect().toRect().
668 \li Corresponds to QGraphicsWidget::palette().
671 \li Corresponds to QFontMetrics(QGraphicsWidget::font()).
674 Subclasses of QGraphicsWidget should call the base implementation, and
675 then test the type of \a option using qstyleoption_cast<>() or test
676 QStyleOption::Type before storing widget-specific options.
680 \snippet code/src_gui_graphicsview_qgraphicswidget.cpp 0
682 \sa QStyleOption::initFrom()
684 void QGraphicsWidget::initStyleOption(QStyleOption *option) const
688 option->state = QStyle::State_None;
690 option->state |= QStyle::State_Enabled;
692 option->state |= QStyle::State_HasFocus;
693 // if (window->testAttribute(Qt::WA_KeyboardFocusChange)) // ### Window
694 // option->state |= QStyle::State_KeyboardFocusChange;
696 option->state |= QStyle::State_MouseOver;
697 if (QGraphicsWidget *w = window()) {
698 if (w->isActiveWindow())
699 option->state |= QStyle::State_Active;
702 option->state |= QStyle::State_Window;
706 extern bool qt_mac_can_clickThrough(const QGraphicsWidget *w); //qwidget_mac.cpp
707 if (!(option->state & QStyle::State_Active) && !qt_mac_can_clickThrough(widget))
708 option->state &= ~QStyle::State_Enabled;
710 switch (QMacStyle::widgetSizePolicy(widget)) {
711 case QMacStyle::SizeSmall:
712 option->state |= QStyle::State_Small;
714 case QMacStyle::SizeMini:
715 option->state |= QStyle::State_Mini;
721 #ifdef QT_KEYPAD_NAVIGATION
722 if (widget->hasEditFocus())
723 state |= QStyle::State_HasEditFocus;
726 option->direction = layoutDirection();
727 option->rect = rect().toRect(); // ### truncation!
728 option->palette = palette();
730 option->palette.setCurrentColorGroup(QPalette::Disabled);
731 } else if (isActiveWindow()) {
732 option->palette.setCurrentColorGroup(QPalette::Active);
734 option->palette.setCurrentColorGroup(QPalette::Inactive);
736 option->fontMetrics = QFontMetrics(font());
742 QSizeF QGraphicsWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
744 Q_D(const QGraphicsWidget);
747 QSizeF marginSize(0,0);
749 marginSize = QSizeF(d->margins[d->Left] + d->margins[d->Right],
750 d->margins[d->Top] + d->margins[d->Bottom]);
752 sh = d->layout->effectiveSizeHint(which, constraint - marginSize);
756 case Qt::MinimumSize:
759 case Qt::PreferredSize:
760 sh = QSizeF(50, 50); //rather arbitrary
762 case Qt::MaximumSize:
763 sh = QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
766 qWarning("QGraphicsWidget::sizeHint(): Don't know how to handle the value of 'which'");
774 \property QGraphicsWidget::layout
775 \brief The layout of the widget
777 Any existing layout manager is deleted before the new layout is assigned. If
778 \a layout is 0, the widget is left without a layout. Existing subwidgets'
779 geometries will remain unaffected.
781 QGraphicsWidget takes ownership of \a layout.
783 All widgets that are currently managed by \a layout or all of its
784 sublayouts, are automatically reparented to this item. The layout is then
785 invalidated, and the child widget geometries are adjusted according to
786 this item's geometry() and contentsMargins(). Children who are not
787 explicitly managed by \a layout remain unaffected by the layout after
788 it has been assigned to this widget.
790 If no layout is currently managing this widget, layout() will return 0.
795 \fn void QGraphicsWidget::layoutChanged()
796 This signal gets emitted whenever the layout of the item changes
801 Returns this widget's layout, or 0 if no layout is currently managing this
806 QGraphicsLayout *QGraphicsWidget::layout() const
808 Q_D(const QGraphicsWidget);
813 \fn void QGraphicsWidget::setLayout(QGraphicsLayout *layout)
815 Sets the layout for this widget to \a layout. Any existing layout manager
816 is deleted before the new layout is assigned. If \a layout is 0, the
817 widget is left without a layout. Existing subwidgets' geometries will
820 All widgets that are currently managed by \a layout or all of its
821 sublayouts, are automatically reparented to this item. The layout is then
822 invalidated, and the child widget geometries are adjusted according to
823 this item's geometry() and contentsMargins(). Children who are not
824 explicitly managed by \a layout remain unaffected by the layout after
825 it has been assigned to this widget.
827 QGraphicsWidget takes ownership of \a layout.
829 \sa layout(), QGraphicsLinearLayout::addItem(), QGraphicsLayout::invalidate()
831 void QGraphicsWidget::setLayout(QGraphicsLayout *l)
833 Q_D(QGraphicsWidget);
836 d->setLayout_helper(l);
840 // Prevent assigning a layout that is already assigned to another widget.
841 QGraphicsLayoutItem *oldParent = l->parentLayoutItem();
842 if (oldParent && oldParent != this) {
843 qWarning("QGraphicsWidget::setLayout: Attempting to set a layout on %s"
844 " \"%s\", when the layout already has a parent",
845 metaObject()->className(), qPrintable(objectName()));
849 // Install and activate the layout.
850 l->setParentLayoutItem(this);
851 l->d_func()->reparentChildItems(this);
853 emit layoutChanged();
857 Adjusts the size of the widget to its effective preferred size hint.
859 This function is called implicitly when the item is shown for the first
862 \sa effectiveSizeHint(), Qt::MinimumSize
864 void QGraphicsWidget::adjustSize()
866 QSizeF sz = effectiveSizeHint(Qt::PreferredSize);
867 // What if sz is not valid?!
873 \property QGraphicsWidget::layoutDirection
874 \brief the layout direction for this widget.
876 This property modifies this widget's and all of its descendants'
877 Qt::WA_RightToLeft attribute. It also sets this widget's
878 Qt::WA_SetLayoutDirection attribute.
880 The widget's layout direction determines the order in which the layout
881 manager horizontally arranges subwidgets of this widget. The default
882 value depends on the language and locale of the application, and is
883 typically in the same direction as words are read and written. With
884 Qt::LeftToRight, the layout starts placing subwidgets from the left
885 side of this widget towards the right. Qt::RightToLeft does the opposite -
886 the layout will place widgets starting from the right edge moving towards
889 Subwidgets inherit their layout direction from the parent. Top-level
890 widget items inherit their layout direction from
891 QGraphicsScene::layoutDirection. If you change a widget's layout direction
892 by calling setLayoutDirection(), the widget will send itself a
893 \l{QEvent::LayoutDirectionChange}{LayoutDirectionChange} event, and then
894 propagate the new layout direction to all its descendants.
896 \sa QWidget::layoutDirection, QApplication::layoutDirection
898 Qt::LayoutDirection QGraphicsWidget::layoutDirection() const
900 return testAttribute(Qt::WA_RightToLeft) ? Qt::RightToLeft : Qt::LeftToRight;
902 void QGraphicsWidget::setLayoutDirection(Qt::LayoutDirection direction)
904 Q_D(QGraphicsWidget);
905 setAttribute(Qt::WA_SetLayoutDirection, true);
906 d->setLayoutDirection_helper(direction);
908 void QGraphicsWidget::unsetLayoutDirection()
910 Q_D(QGraphicsWidget);
911 setAttribute(Qt::WA_SetLayoutDirection, false);
912 d->resolveLayoutDirection();
916 Returns a pointer to the widget's style. If this widget does not have any
917 explicitly assigned style, the scene's style is returned instead. In turn,
918 if the scene does not have any assigned style, this function returns
919 QApplication::style().
923 QStyle *QGraphicsWidget::style() const
925 if (QStyle *style = widgetStyles()->styleForWidget(this))
927 // ### This is not thread-safe. QApplication::style() is not thread-safe.
928 return scene() ? scene()->style() : QApplication::style();
932 Sets the widget's style to \a style. QGraphicsWidget does \e not take
933 ownership of \a style.
935 If no style is assigned, or \a style is 0, the widget will use
936 QGraphicsScene::style() (if this has been set). Otherwise the widget will
937 use QApplication::style().
939 This function sets the Qt::WA_SetStyle attribute if \a style is not 0;
940 otherwise it clears the attribute.
944 void QGraphicsWidget::setStyle(QStyle *style)
946 setAttribute(Qt::WA_SetStyle, style != 0);
947 widgetStyles()->setStyleForWidget(this, style);
949 // Deliver StyleChange to the widget itself (doesn't propagate).
950 QEvent event(QEvent::StyleChange);
951 QApplication::sendEvent(this, &event);
955 \property QGraphicsWidget::font
956 \brief the widgets' font
958 This property provides the widget's font.
960 QFont consists of font properties that have been explicitly defined and
961 properties implicitly inherited from the widget's parent. Hence, font()
962 can return a different font compared to the one set with setFont().
963 This scheme allows you to define single entries in a font without
964 affecting the font's inherited entries.
966 When a widget's font changes, it resolves its entries against its
967 parent widget. If the widget does not have a parent widget, it resolves
968 its entries against the scene. The widget then sends itself a
969 \l{QEvent::FontChange}{FontChange} event and notifies all its
970 descendants so that they can resolve their fonts as well.
972 By default, this property contains the application's default font.
974 \sa QApplication::font(), QGraphicsScene::font, QFont::resolve()
976 QFont QGraphicsWidget::font() const
978 Q_D(const QGraphicsWidget);
980 fnt.resolve(fnt.resolve() | d->inheritedFontResolveMask);
983 void QGraphicsWidget::setFont(const QFont &font)
985 Q_D(QGraphicsWidget);
986 setAttribute(Qt::WA_SetFont, font.resolve() != 0);
988 QFont naturalFont = d->naturalWidgetFont();
989 QFont resolvedFont = font.resolve(naturalFont);
990 d->setFont_helper(resolvedFont);
994 \property QGraphicsWidget::palette
995 \brief the widget's palette
997 This property provides the widget's palette. The palette provides colors
998 and brushes for color groups (e.g., QPalette::Button) and states (e.g.,
999 QPalette::Inactive), loosely defining the general look of the widget and
1002 QPalette consists of color groups that have been explicitly defined, and
1003 groups that are implicitly inherited from the widget's parent. Because of
1004 this, palette() can return a different palette than what has been set with
1005 setPalette(). This scheme allows you to define single entries in a palette
1006 without affecting the palette's inherited entries.
1008 When a widget's palette changes, it resolves its entries against its
1009 parent widget, or if it doesn't have a parent widget, it resolves against
1010 the scene. It then sends itself a \l{QEvent::PaletteChange}{PaletteChange}
1011 event, and notifies all its descendants so they can resolve their palettes
1014 By default, this property contains the application's default palette.
1016 \sa QApplication::palette(), QGraphicsScene::palette, QPalette::resolve()
1018 QPalette QGraphicsWidget::palette() const
1020 Q_D(const QGraphicsWidget);
1023 void QGraphicsWidget::setPalette(const QPalette &palette)
1025 Q_D(QGraphicsWidget);
1026 setAttribute(Qt::WA_SetPalette, palette.resolve() != 0);
1028 QPalette naturalPalette = d->naturalWidgetPalette();
1029 QPalette resolvedPalette = palette.resolve(naturalPalette);
1030 d->setPalette_helper(resolvedPalette);
1034 \property QGraphicsWidget::autoFillBackground
1035 \brief whether the widget background is filled automatically
1038 If enabled, this property will cause Qt to fill the background of the
1039 widget before invoking the paint() method. The color used is defined by the
1040 QPalette::Window color role from the widget's \l{QPalette}{palette}.
1042 In addition, Windows are always filled with QPalette::Window, unless the
1043 WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set.
1045 By default, this property is false.
1047 \sa Qt::WA_OpaquePaintEvent, Qt::WA_NoSystemBackground,
1049 bool QGraphicsWidget::autoFillBackground() const
1051 Q_D(const QGraphicsWidget);
1052 return d->autoFillBackground;
1054 void QGraphicsWidget::setAutoFillBackground(bool enabled)
1056 Q_D(QGraphicsWidget);
1057 if (d->autoFillBackground != enabled) {
1058 d->autoFillBackground = enabled;
1064 If this widget is currently managed by a layout, this function notifies
1065 the layout that the widget's size hints have changed and the layout
1066 may need to resize and reposition the widget accordingly.
1068 Call this function if the widget's sizeHint() has changed.
1070 \sa QGraphicsLayout::invalidate()
1072 void QGraphicsWidget::updateGeometry()
1074 QGraphicsLayoutItem::updateGeometry();
1075 QGraphicsLayoutItem *parentItem = parentLayoutItem();
1077 if (parentItem && parentItem->isLayout()) {
1078 if (QGraphicsLayout::instantInvalidatePropagation()) {
1079 static_cast<QGraphicsLayout *>(parentItem)->invalidate();
1081 parentItem->updateGeometry();
1085 // This is for custom layouting
1086 QGraphicsWidget *parentWid = parentWidget(); //###
1087 if (parentWid->isVisible())
1088 QApplication::postEvent(parentWid, new QEvent(QEvent::LayoutRequest));
1091 * If this is the topmost widget, post a LayoutRequest event to the widget.
1092 * When the event is received, it will start flowing all the way down to the leaf
1093 * widgets in one go. This will make a relayout flicker-free.
1095 if (QGraphicsLayout::instantInvalidatePropagation())
1096 QApplication::postEvent(static_cast<QGraphicsWidget *>(this), new QEvent(QEvent::LayoutRequest));
1098 if (!QGraphicsLayout::instantInvalidatePropagation()) {
1099 bool wasResized = testAttribute(Qt::WA_Resized);
1100 resize(size()); // this will restrict the size
1101 setAttribute(Qt::WA_Resized, wasResized);
1109 QGraphicsWidget uses the base implementation of this function to catch and
1110 deliver events related to state changes in the item. Because of this, it is
1111 very important that subclasses call the base implementation.
1113 \a change specifies the type of change, and \a value is the new value.
1115 For example, QGraphicsWidget uses ItemVisibleChange to deliver
1116 \l{QEvent::Show} {Show} and \l{QEvent::Hide}{Hide} events,
1117 ItemPositionHasChanged to deliver \l{QEvent::Move}{Move} events,
1118 and ItemParentChange both to deliver \l{QEvent::ParentChange}
1119 {ParentChange} events, and for managing the focus chain.
1121 QGraphicsWidget enables the ItemSendsGeometryChanges flag by default in
1122 order to track position changes.
1124 \sa QGraphicsItem::itemChange()
1126 QVariant QGraphicsWidget::itemChange(GraphicsItemChange change, const QVariant &value)
1128 Q_D(QGraphicsWidget);
1130 case ItemEnabledHasChanged: {
1131 // Send EnabledChange after the enabled state has changed.
1132 QEvent event(QEvent::EnabledChange);
1133 QApplication::sendEvent(this, &event);
1136 case ItemVisibleChange:
1137 if (value.toBool()) {
1138 // Send Show event before the item has been shown.
1140 QApplication::sendEvent(this, &event);
1141 bool resized = testAttribute(Qt::WA_Resized);
1144 setAttribute(Qt::WA_Resized, false);
1148 case ItemVisibleHasChanged:
1149 if (!value.toBool()) {
1150 // Send Hide event after the item has been hidden.
1152 QApplication::sendEvent(this, &event);
1155 case ItemPositionHasChanged:
1156 d->setGeometryFromSetPos();
1158 case ItemParentChange: {
1159 // Deliver ParentAboutToChange.
1160 QEvent event(QEvent::ParentAboutToChange);
1161 QApplication::sendEvent(this, &event);
1164 case ItemParentHasChanged: {
1165 // Deliver ParentChange.
1166 QEvent event(QEvent::ParentChange);
1167 QApplication::sendEvent(this, &event);
1170 case ItemCursorHasChanged: {
1171 // Deliver CursorChange.
1172 QEvent event(QEvent::CursorChange);
1173 QApplication::sendEvent(this, &event);
1176 case ItemToolTipHasChanged: {
1177 // Deliver ToolTipChange.
1178 QEvent event(QEvent::ToolTipChange);
1179 QApplication::sendEvent(this, &event);
1185 return QGraphicsItem::itemChange(change, value);
1191 This virtual function is used to notify changes to any property (both
1192 dynamic properties, and registered with Q_PROPERTY) in the
1193 widget. Depending on the property itself, the notification can be
1194 delivered before or after the value has changed.
1196 \a propertyName is the name of the property (e.g., "size" or "font"), and
1197 \a value is the (proposed) new value of the property. The function returns
1198 the new value, which may be different from \a value if the notification
1199 supports adjusting the property value. The base implementation simply
1200 returns \a value for any \a propertyName.
1202 QGraphicsWidget delivers notifications for the following properties:
1205 \header \li propertyName \li Property
1206 \row \li layoutDirection \li QGraphicsWidget::layoutDirection
1207 \row \li size \li QGraphicsWidget::size
1208 \row \li font \li QGraphicsWidget::font
1209 \row \li palette \li QGraphicsWidget::palette
1214 QVariant QGraphicsWidget::propertyChange(const QString &propertyName, const QVariant &value)
1216 Q_UNUSED(propertyName);
1221 QGraphicsWidget's implementation of sceneEvent() simply passes \a event to
1222 QGraphicsWidget::event(). You can handle all events for your widget in
1223 event() or in any of the convenience functions; you should not have to
1224 reimplement this function in a subclass of QGraphicsWidget.
1226 \sa QGraphicsItem::sceneEvent()
1228 bool QGraphicsWidget::sceneEvent(QEvent *event)
1230 return QGraphicsItem::sceneEvent(event);
1234 This event handler, for \a event, receives events for the window frame if
1235 this widget is a window. Its base implementation provides support for
1236 default window frame interaction such as moving, resizing, etc.
1238 You can reimplement this handler in a subclass of QGraphicsWidget to
1239 provide your own custom window frame interaction support.
1241 Returns true if \a event has been recognized and processed; otherwise,
1246 bool QGraphicsWidget::windowFrameEvent(QEvent *event)
1248 Q_D(QGraphicsWidget);
1249 switch (event->type()) {
1250 case QEvent::GraphicsSceneMousePress:
1251 d->windowFrameMousePressEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
1253 case QEvent::GraphicsSceneMouseMove:
1254 d->ensureWindowData();
1255 if (d->windowData->grabbedSection != Qt::NoSection) {
1256 d->windowFrameMouseMoveEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
1260 case QEvent::GraphicsSceneMouseRelease:
1261 d->windowFrameMouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
1263 case QEvent::GraphicsSceneHoverMove:
1264 d->windowFrameHoverMoveEvent(static_cast<QGraphicsSceneHoverEvent *>(event));
1266 case QEvent::GraphicsSceneHoverLeave:
1267 d->windowFrameHoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent *>(event));
1272 return event->isAccepted();
1278 Returns the window frame section at position \a pos, or
1279 Qt::NoSection if there is no window frame section at this
1282 This function is used in QGraphicsWidget's base implementation for window
1285 You can reimplement this function if you want to customize how a window
1286 can be interactively moved or resized. For instance, if you only want to
1287 allow a window to be resized by the bottom right corner, you can
1288 reimplement this function to return Qt::NoSection for all sections except
1289 Qt::BottomRightSection.
1291 \sa windowFrameEvent(), paintWindowFrame(), windowFrameGeometry()
1293 Qt::WindowFrameSection QGraphicsWidget::windowFrameSectionAt(const QPointF &pos) const
1295 Q_D(const QGraphicsWidget);
1297 const QRectF r = windowFrameRect();
1298 if (!r.contains(pos))
1299 return Qt::NoSection;
1301 const qreal left = r.left();
1302 const qreal top = r.top();
1303 const qreal right = r.right();
1304 const qreal bottom = r.bottom();
1305 const qreal x = pos.x();
1306 const qreal y = pos.y();
1308 const qreal cornerMargin = 20;
1309 //### Not sure of this one, it should be the same value for all edges.
1310 const qreal windowFrameWidth = d->windowFrameMargins
1311 ? d->windowFrameMargins[d->Left] : 0;
1313 Qt::WindowFrameSection s = Qt::NoSection;
1314 if (x <= left + cornerMargin) {
1315 if (y <= top + windowFrameWidth || (x <= left + windowFrameWidth && y <= top + cornerMargin)) {
1316 s = Qt::TopLeftSection;
1317 } else if (y >= bottom - windowFrameWidth || (x <= left + windowFrameWidth && y >= bottom - windowFrameWidth)) {
1318 s = Qt::BottomLeftSection;
1319 } else if (x <= left + windowFrameWidth) {
1320 s = Qt::LeftSection;
1322 } else if (x >= right - cornerMargin) {
1323 if (y <= top + windowFrameWidth || (x >= right - windowFrameWidth && y <= top + cornerMargin)) {
1324 s = Qt::TopRightSection;
1325 } else if (y >= bottom - windowFrameWidth || (x >= right - windowFrameWidth && y >= bottom - windowFrameWidth)) {
1326 s = Qt::BottomRightSection;
1327 } else if (x >= right - windowFrameWidth) {
1328 s = Qt::RightSection;
1330 } else if (y <= top + windowFrameWidth) {
1332 } else if (y >= bottom - windowFrameWidth) {
1333 s = Qt::BottomSection;
1335 if (s == Qt::NoSection) {
1337 r1.setHeight(d->windowFrameMargins
1338 ? d->windowFrameMargins[d->Top] : 0);
1339 if (r1.contains(pos))
1340 s = Qt::TitleBarArea;
1348 Handles the \a event. QGraphicsWidget handles the following
1352 \header \li Event \li Usage
1354 \li Delivered to the widget some time after it has been
1356 \row \li GraphicsSceneMove
1357 \li Delivered to the widget after its local position has
1359 \row \li GraphicsSceneResize
1360 \li Delivered to the widget after its size has changed.
1362 \li Delivered to the widget before it has been shown.
1364 \li Delivered to the widget after it has been hidden.
1365 \row \li PaletteChange
1366 \li Delivered to the widget after its palette has changed.
1368 \li Delivered to the widget after its font has changed.
1369 \row \li EnabledChange
1370 \li Delivered to the widget after its enabled state has
1372 \row \li StyleChange
1373 \li Delivered to the widget after its style has changed.
1374 \row \li LayoutDirectionChange
1375 \li Delivered to the widget after its layout direction has
1377 \row \li ContentsRectChange
1378 \li Delivered to the widget after its contents margins/
1379 contents rect has changed.
1382 bool QGraphicsWidget::event(QEvent *event)
1384 Q_D(QGraphicsWidget);
1385 // Forward the event to the layout first.
1387 d->layout->widgetEvent(event);
1389 // Handle the event itself.
1390 switch (event->type()) {
1391 case QEvent::GraphicsSceneMove:
1392 moveEvent(static_cast<QGraphicsSceneMoveEvent *>(event));
1394 case QEvent::GraphicsSceneResize:
1395 resizeEvent(static_cast<QGraphicsSceneResizeEvent *>(event));
1398 showEvent(static_cast<QShowEvent *>(event));
1401 hideEvent(static_cast<QHideEvent *>(event));
1403 case QEvent::Polish:
1406 if (!d->font.isCopyOf(QApplication::font()))
1407 d->updateFont(d->font);
1409 case QEvent::WindowActivate:
1410 case QEvent::WindowDeactivate:
1411 case QEvent::StyleAnimationUpdate:
1414 // Taken from QWidget::event
1415 case QEvent::ActivationChange:
1416 case QEvent::EnabledChange:
1417 case QEvent::FontChange:
1418 case QEvent::StyleChange:
1419 case QEvent::PaletteChange:
1420 case QEvent::ParentChange:
1421 case QEvent::ContentsRectChange:
1422 case QEvent::LayoutDirectionChange:
1426 closeEvent((QCloseEvent *)event);
1428 case QEvent::GrabMouse:
1429 grabMouseEvent(event);
1431 case QEvent::UngrabMouse:
1432 ungrabMouseEvent(event);
1434 case QEvent::GrabKeyboard:
1435 grabKeyboardEvent(event);
1437 case QEvent::UngrabKeyboard:
1438 ungrabKeyboardEvent(event);
1440 case QEvent::GraphicsSceneMousePress:
1441 if (d->hasDecoration() && windowFrameEvent(event))
1443 case QEvent::GraphicsSceneMouseMove:
1444 case QEvent::GraphicsSceneMouseRelease:
1445 case QEvent::GraphicsSceneMouseDoubleClick:
1446 d->ensureWindowData();
1447 if (d->hasDecoration() && d->windowData->grabbedSection != Qt::NoSection)
1448 return windowFrameEvent(event);
1450 case QEvent::GraphicsSceneHoverEnter:
1451 case QEvent::GraphicsSceneHoverMove:
1452 case QEvent::GraphicsSceneHoverLeave:
1453 if (d->hasDecoration()) {
1454 windowFrameEvent(event);
1455 // Filter out hover events if they were sent to us only because of the
1456 // decoration (special case in QGraphicsScenePrivate::dispatchHoverEvent).
1457 if (!acceptHoverEvents())
1464 return QObject::event(event);
1468 This event handler can be reimplemented to handle state changes.
1470 The state being changed in this event can be retrieved through \a event.
1472 Change events include: QEvent::ActivationChange, QEvent::EnabledChange,
1473 QEvent::FontChange, QEvent::StyleChange, QEvent::PaletteChange,
1474 QEvent::ParentChange, QEvent::LayoutDirectionChange, and
1475 QEvent::ContentsRectChange.
1477 void QGraphicsWidget::changeEvent(QEvent *event)
1479 Q_D(QGraphicsWidget);
1480 switch (event->type()) {
1481 case QEvent::StyleChange:
1482 // ### Don't unset if the margins are explicitly set.
1483 unsetWindowFrameMargins();
1485 d->layout->invalidate();
1486 case QEvent::FontChange:
1490 case QEvent::PaletteChange:
1493 case QEvent::ParentChange:
1494 d->resolveFont(d->inheritedFontResolveMask);
1495 d->resolvePalette(d->inheritedPaletteResolveMask);
1503 This event handler, for \a event, can be reimplemented in a subclass to
1504 receive widget close events. The default implementation accepts the
1507 \sa close(), QCloseEvent
1509 void QGraphicsWidget::closeEvent(QCloseEvent *event)
1517 void QGraphicsWidget::focusInEvent(QFocusEvent *event)
1520 if (focusPolicy() != Qt::NoFocus)
1525 Finds a new widget to give the keyboard focus to, as appropriate for Tab
1526 and Shift+Tab, and returns true if it can find a new widget; returns false
1527 otherwise. If \a next is true, this function searches forward; if \a next
1528 is false, it searches backward.
1530 Sometimes, you will want to reimplement this function to provide special
1531 focus handling for your widget and its subwidgets. For example, a web
1532 browser might reimplement it to move its current active link forward or
1533 backward, and call the base implementation only when it reaches the last
1534 or first link on the page.
1536 Child widgets call focusNextPrevChild() on their parent widgets, but only
1537 the window that contains the child widgets decides where to redirect
1538 focus. By reimplementing this function for an object, you gain control of
1539 focus traversal for all child widgets.
1543 bool QGraphicsWidget::focusNextPrevChild(bool next)
1545 Q_D(QGraphicsWidget);
1546 // Let the parent's focusNextPrevChild implementation decide what to do.
1547 QGraphicsWidget *parent = 0;
1548 if (!isWindow() && (parent = parentWidget()))
1549 return parent->focusNextPrevChild(next);
1552 if (d->scene->focusNextPrevChild(next))
1555 setFocus(next ? Qt::TabFocusReason : Qt::BacktabFocusReason);
1565 void QGraphicsWidget::focusOutEvent(QFocusEvent *event)
1568 if (focusPolicy() != Qt::NoFocus)
1573 This event handler, for \l{QEvent::Hide}{Hide} events, is delivered after
1574 the widget has been hidden, for example, setVisible(false) has been called
1575 for the widget or one of its ancestors when the widget was previously
1578 You can reimplement this event handler to detect when your widget is
1579 hidden. Calling QEvent::accept() or QEvent::ignore() on \a event has no
1582 \sa showEvent(), QWidget::hideEvent(), ItemVisibleChange
1584 void QGraphicsWidget::hideEvent(QHideEvent *event)
1586 ///### focusNextPrevChild(true), don't lose focus when the focus widget
1592 This event handler, for \l{QEvent::GraphicsSceneMove}{GraphicsSceneMove}
1593 events, is delivered after the widget has moved (e.g., its local position
1596 This event is only delivered when the item is moved locally. Calling
1597 setTransform() or moving any of the item's ancestors does not affect the
1598 item's local position.
1600 You can reimplement this event handler to detect when your widget has
1601 moved. Calling QEvent::accept() or QEvent::ignore() on \a event has no
1604 \sa ItemPositionChange, ItemPositionHasChanged
1606 void QGraphicsWidget::moveEvent(QGraphicsSceneMoveEvent *event)
1608 // ### Last position is always == current position
1613 This event is delivered to the item by the scene at some point after it
1614 has been constructed, but before it is shown or otherwise accessed through
1615 the scene. You can use this event handler to do last-minute initializations
1616 of the widget which require the item to be fully constructed.
1618 The base implementation does nothing.
1620 void QGraphicsWidget::polishEvent()
1625 This event handler, for
1626 \l{QEvent::GraphicsSceneResize}{GraphicsSceneResize} events, is
1627 delivered after the widget has been resized (i.e., its local size has
1628 changed). \a event contains both the old and the new size.
1630 This event is only delivered when the widget is resized locally; calling
1631 setTransform() on the widget or any of its ancestors or view, does not
1632 affect the widget's local size.
1634 You can reimplement this event handler to detect when your widget has been
1635 resized. Calling QEvent::accept() or QEvent::ignore() on \a event has no
1638 \sa geometry(), setGeometry()
1640 void QGraphicsWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
1646 This event handler, for \l{QEvent::Show}{Show} events, is delivered before
1647 the widget has been shown, for example, setVisible(true) has been called
1648 for the widget or one of its ancestors when the widget was previously
1651 You can reimplement this event handler to detect when your widget is
1652 shown. Calling QEvent::accept() or QEvent::ignore() on \a event has no
1655 \sa hideEvent(), QWidget::showEvent(), ItemVisibleChange
1657 void QGraphicsWidget::showEvent(QShowEvent *event)
1665 void QGraphicsWidget::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
1673 void QGraphicsWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
1679 This event handler, for \a event, can be reimplemented in a subclass to
1680 receive notifications for QEvent::GrabMouse events.
1682 \sa grabMouse(), grabKeyboard()
1684 void QGraphicsWidget::grabMouseEvent(QEvent *event)
1690 This event handler, for \a event, can be reimplemented in a subclass to
1691 receive notifications for QEvent::UngrabMouse events.
1693 \sa ungrabMouse(), ungrabKeyboard()
1695 void QGraphicsWidget::ungrabMouseEvent(QEvent *event)
1701 This event handler, for \a event, can be reimplemented in a subclass to
1702 receive notifications for QEvent::GrabKeyboard events.
1704 \sa grabKeyboard(), grabMouse()
1706 void QGraphicsWidget::grabKeyboardEvent(QEvent *event)
1712 This event handler, for \a event, can be reimplemented in a subclass to
1713 receive notifications for QEvent::UngrabKeyboard events.
1715 \sa ungrabKeyboard(), ungrabMouse()
1717 void QGraphicsWidget::ungrabKeyboardEvent(QEvent *event)
1723 Returns the widgets window type.
1725 \sa windowFlags(), isWindow(), isPanel()
1727 Qt::WindowType QGraphicsWidget::windowType() const
1729 return Qt::WindowType(int(windowFlags()) & Qt::WindowType_Mask);
1733 \property QGraphicsWidget::windowFlags
1734 \brief the widget's window flags
1736 Window flags are a combination of a window type (e.g., Qt::Dialog) and
1737 several flags giving hints on the behavior of the window. The behavior
1738 is platform-dependent.
1740 By default, this property contains no window flags.
1742 Windows are panels. If you set the Qt::Window flag, the ItemIsPanel flag
1743 will be set automatically. If you clear the Qt::Window flag, the
1744 ItemIsPanel flag is also cleared. Note that the ItemIsPanel flag can be
1745 set independently of Qt::Window.
1747 \sa isWindow(), isPanel()
1749 Qt::WindowFlags QGraphicsWidget::windowFlags() const
1751 Q_D(const QGraphicsWidget);
1752 return d->windowFlags;
1754 void QGraphicsWidget::setWindowFlags(Qt::WindowFlags wFlags)
1756 Q_D(QGraphicsWidget);
1757 if (d->windowFlags == wFlags)
1759 bool wasPopup = (d->windowFlags & Qt::WindowType_Mask) == Qt::Popup;
1761 d->adjustWindowFlags(&wFlags);
1762 d->windowFlags = wFlags;
1763 if (!d->setWindowFrameMargins)
1764 unsetWindowFrameMargins();
1766 setFlag(ItemIsPanel, d->windowFlags & Qt::Window);
1768 bool isPopup = (d->windowFlags & Qt::WindowType_Mask) == Qt::Popup;
1769 if (d->scene && isVisible() && wasPopup != isPopup) {
1770 // Popup state changed; update implicit mouse grab.
1772 d->scene->d_func()->removePopup(this);
1774 d->scene->d_func()->addPopup(this);
1777 if (d->scene && d->scene->d_func()->allItemsIgnoreHoverEvents && d->hasDecoration()) {
1778 d->scene->d_func()->allItemsIgnoreHoverEvents = false;
1779 d->scene->d_func()->enableMouseTrackingOnViews();
1784 Returns true if this widget's window is in the active window, or if the
1785 widget does not have a window but is in an active scene (i.e., a scene
1786 that currently has focus).
1788 The active window is the window that either contains a child widget that
1789 currently has input focus, or that itself has input focus.
1791 \sa QGraphicsScene::activeWindow(), QGraphicsScene::setActiveWindow(), isActive()
1793 bool QGraphicsWidget::isActiveWindow() const
1799 \property QGraphicsWidget::windowTitle
1800 \brief This property holds the window title (caption).
1802 This property is only used for windows.
1804 By default, if no title has been set, this property contains an
1807 void QGraphicsWidget::setWindowTitle(const QString &title)
1809 Q_D(QGraphicsWidget);
1810 d->ensureWindowData();
1811 d->windowData->windowTitle = title;
1813 QString QGraphicsWidget::windowTitle() const
1815 Q_D(const QGraphicsWidget);
1816 return d->windowData ? d->windowData->windowTitle : QString();
1820 \property QGraphicsWidget::focusPolicy
1821 \brief the way the widget accepts keyboard focus
1823 The focus policy is Qt::TabFocus if the widget accepts keyboard focus by
1824 tabbing, Qt::ClickFocus if the widget accepts focus by clicking,
1825 Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if it
1826 does not accept focus at all.
1828 You must enable keyboard focus for a widget if it processes keyboard
1829 events. This is normally done from the widget's constructor. For instance,
1830 the QLineEdit constructor calls setFocusPolicy(Qt::StrongFocus).
1832 If you enable a focus policy (i.e., not Qt::NoFocus), QGraphicsWidget will
1833 automatically enable the ItemIsFocusable flag. Setting Qt::NoFocus on a
1834 widget will clear the ItemIsFocusable flag. If the widget currently has
1835 keyboard focus, the widget will automatically lose focus.
1837 \sa focusInEvent(), focusOutEvent(), keyPressEvent(), keyReleaseEvent(), enabled
1839 Qt::FocusPolicy QGraphicsWidget::focusPolicy() const
1841 Q_D(const QGraphicsWidget);
1842 return d->focusPolicy;
1844 void QGraphicsWidget::setFocusPolicy(Qt::FocusPolicy policy)
1846 Q_D(QGraphicsWidget);
1847 if (d->focusPolicy == policy)
1849 d->focusPolicy = policy;
1850 if (hasFocus() && policy == Qt::NoFocus)
1852 setFlag(ItemIsFocusable, policy != Qt::NoFocus);
1856 If this widget, a child or descendant of this widget currently has input
1857 focus, this function will return a pointer to that widget. If
1858 no descendant widget has input focus, 0 is returned.
1860 \sa QGraphicsItem::focusItem(), QWidget::focusWidget()
1862 QGraphicsWidget *QGraphicsWidget::focusWidget() const
1864 Q_D(const QGraphicsWidget);
1865 if (d->subFocusItem && d->subFocusItem->d_ptr->isWidget)
1866 return static_cast<QGraphicsWidget *>(d->subFocusItem);
1870 #ifndef QT_NO_SHORTCUT
1874 Adds a shortcut to Qt's shortcut system that watches for the given key \a
1875 sequence in the given \a context. If the \a context is
1876 Qt::ApplicationShortcut, the shortcut applies to the application as a
1877 whole. Otherwise, it is either local to this widget, Qt::WidgetShortcut,
1878 or to the window itself, Qt::WindowShortcut. For widgets that are not part
1879 of a window (i.e., top-level widgets and their children),
1880 Qt::WindowShortcut shortcuts apply to the scene.
1882 If the same key \a sequence has been grabbed by several widgets,
1883 when the key \a sequence occurs a QEvent::Shortcut event is sent
1884 to all the widgets to which it applies in a non-deterministic
1885 order, but with the ``ambiguous'' flag set to true.
1887 \warning You should not normally need to use this function;
1888 instead create \l{QAction}s with the shortcut key sequences you
1889 require (if you also want equivalent menu options and toolbar
1890 buttons), or create \l{QShortcut}s if you just need key sequences.
1891 Both QAction and QShortcut handle all the event filtering for you,
1892 and provide signals which are triggered when the user triggers the
1893 key sequence, so are much easier to use than this low-level
1896 \sa releaseShortcut(), setShortcutEnabled(), QWidget::grabShortcut()
1898 int QGraphicsWidget::grabShortcut(const QKeySequence &sequence, Qt::ShortcutContext context)
1901 if (sequence.isEmpty())
1903 // ### setAttribute(Qt::WA_GrabbedShortcut);
1904 return qApp->d_func()->shortcutMap.addShortcut(this, sequence, context, qWidgetShortcutContextMatcher);
1910 Removes the shortcut with the given \a id from Qt's shortcut
1911 system. The widget will no longer receive QEvent::Shortcut events
1912 for the shortcut's key sequence (unless it has other shortcuts
1913 with the same key sequence).
1915 \warning You should not normally need to use this function since
1916 Qt's shortcut system removes shortcuts automatically when their
1917 parent widget is destroyed. It is best to use QAction or
1918 QShortcut to handle shortcuts, since they are easier to use than
1919 this low-level function. Note also that this is an expensive
1922 \sa grabShortcut(), setShortcutEnabled(), QWidget::releaseShortcut()
1924 void QGraphicsWidget::releaseShortcut(int id)
1928 qApp->d_func()->shortcutMap.removeShortcut(id, this, 0);
1934 If \a enabled is true, the shortcut with the given \a id is
1935 enabled; otherwise the shortcut is disabled.
1937 \warning You should not normally need to use this function since
1938 Qt's shortcut system enables/disables shortcuts automatically as
1939 widgets become hidden/visible and gain or lose focus. It is best
1940 to use QAction or QShortcut to handle shortcuts, since they are
1941 easier to use than this low-level function.
1943 \sa grabShortcut(), releaseShortcut(), QWidget::setShortcutEnabled()
1945 void QGraphicsWidget::setShortcutEnabled(int id, bool enabled)
1949 qApp->d_func()->shortcutMap.setShortcutEnabled(enabled, id, this, 0);
1955 If \a enabled is true, auto repeat of the shortcut with the
1956 given \a id is enabled; otherwise it is disabled.
1958 \sa grabShortcut(), releaseShortcut(), QWidget::setShortcutAutoRepeat()
1960 void QGraphicsWidget::setShortcutAutoRepeat(int id, bool enabled)
1964 qApp->d_func()->shortcutMap.setShortcutAutoRepeat(enabled, id, this, 0);
1968 #ifndef QT_NO_ACTION
1972 Appends the action \a action to this widget's list of actions.
1974 All QGraphicsWidgets have a list of \l{QAction}s, however they can be
1975 represented graphically in many different ways. The default use of the
1976 QAction list (as returned by actions()) is to create a context QMenu.
1978 A QGraphicsWidget should only have one of each action and adding an action
1979 it already has will not cause the same action to be in the widget twice.
1981 \sa removeAction(), insertAction(), actions(), QWidget::addAction()
1983 void QGraphicsWidget::addAction(QAction *action)
1985 insertAction(0, action);
1991 Appends the actions \a actions to this widget's list of actions.
1993 \sa removeAction(), QMenu, addAction(), QWidget::addActions()
1995 void QGraphicsWidget::addActions(QList<QAction *> actions)
1997 for (int i = 0; i < actions.count(); ++i)
1998 insertAction(0, actions.at(i));
2004 Inserts the action \a action to this widget's list of actions,
2005 before the action \a before. It appends the action if \a before is 0 or
2006 \a before is not a valid action for this widget.
2008 A QGraphicsWidget should only have one of each action.
2010 \sa removeAction(), addAction(), QMenu, actions(),
2011 QWidget::insertActions()
2013 void QGraphicsWidget::insertAction(QAction *before, QAction *action)
2016 qWarning("QWidget::insertAction: Attempt to insert null action");
2020 Q_D(QGraphicsWidget);
2021 int index = d->actions.indexOf(action);
2023 d->actions.removeAt(index);
2025 int pos = d->actions.indexOf(before);
2028 pos = d->actions.size();
2030 d->actions.insert(pos, action);
2033 QActionPrivate *apriv = action->d_func();
2034 apriv->graphicsWidgets.append(this);
2037 QActionEvent e(QEvent::ActionAdded, action, before);
2038 QApplication::sendEvent(this, &e);
2044 Inserts the actions \a actions to this widget's list of actions,
2045 before the action \a before. It appends the action if \a before is 0 or
2046 \a before is not a valid action for this widget.
2048 A QGraphicsWidget can have at most one of each action.
2050 \sa removeAction(), QMenu, insertAction(), QWidget::insertActions()
2052 void QGraphicsWidget::insertActions(QAction *before, QList<QAction *> actions)
2054 for (int i = 0; i < actions.count(); ++i)
2055 insertAction(before, actions.at(i));
2061 Removes the action \a action from this widget's list of actions.
2063 \sa insertAction(), actions(), insertAction(), QWidget::removeAction()
2065 void QGraphicsWidget::removeAction(QAction *action)
2070 Q_D(QGraphicsWidget);
2072 QActionPrivate *apriv = action->d_func();
2073 apriv->graphicsWidgets.removeAll(this);
2075 if (d->actions.removeAll(action)) {
2076 QActionEvent e(QEvent::ActionRemoved, action);
2077 QApplication::sendEvent(this, &e);
2084 Returns the (possibly empty) list of this widget's actions.
2086 \sa insertAction(), removeAction(), QWidget::actions(),
2087 QAction::associatedWidgets(), QAction::associatedGraphicsWidgets()
2089 QList<QAction *> QGraphicsWidget::actions() const
2091 Q_D(const QGraphicsWidget);
2097 Moves the \a second widget around the ring of focus widgets so that
2098 keyboard focus moves from the \a first widget to the \a second widget when
2099 the Tab key is pressed.
2101 Note that since the tab order of the \a second widget is changed, you
2102 should order a chain like this:
2104 \snippet code/src_gui_graphicsview_qgraphicswidget.cpp 1
2108 \snippet code/src_gui_graphicsview_qgraphicswidget.cpp 2
2110 If \a first is 0, this indicates that \a second should be the first widget
2111 to receive input focus should the scene gain Tab focus (i.e., the user
2112 hits Tab so that focus passes into the scene). If \a second is 0, this
2113 indicates that \a first should be the first widget to gain focus if the
2114 scene gained BackTab focus.
2116 By default, tab order is defined implicitly using widget creation order.
2118 \sa focusPolicy, {Keyboard Focus}
2120 void QGraphicsWidget::setTabOrder(QGraphicsWidget *first, QGraphicsWidget *second)
2122 if (!first && !second) {
2123 qWarning("QGraphicsWidget::setTabOrder(0, 0) is undefined");
2126 if ((first && second) && first->scene() != second->scene()) {
2127 qWarning("QGraphicsWidget::setTabOrder: scenes %p and %p are different",
2128 first->scene(), second->scene());
2131 QGraphicsScene *scene = first ? first->scene() : second->scene();
2132 if (!scene && (!first || !second)) {
2133 qWarning("QGraphicsWidget::setTabOrder: assigning tab order from/to the"
2134 " scene requires the item to be in a scene.");
2138 // If either first or second are 0, the scene's tabFocusFirst is updated
2139 // to point to the first item in the scene's focus chain. Then first or
2140 // second are set to point to tabFocusFirst.
2141 QGraphicsScenePrivate *sceneD = scene->d_func();
2143 sceneD->tabFocusFirst = second;
2147 sceneD->tabFocusFirst = first->d_func()->focusNext;
2151 // Both first and second are != 0.
2152 QGraphicsWidget *firstFocusNext = first->d_func()->focusNext;
2153 if (firstFocusNext == second) {
2158 // Update the focus chain.
2159 QGraphicsWidget *secondFocusPrev = second->d_func()->focusPrev;
2160 QGraphicsWidget *secondFocusNext = second->d_func()->focusNext;
2161 firstFocusNext->d_func()->focusPrev = second;
2162 first->d_func()->focusNext = second;
2163 second->d_func()->focusNext = firstFocusNext;
2164 second->d_func()->focusPrev = first;
2165 secondFocusPrev->d_func()->focusNext = secondFocusNext;
2166 secondFocusNext->d_func()->focusPrev = secondFocusPrev;
2168 Q_ASSERT(first->d_func()->focusNext->d_func()->focusPrev == first);
2169 Q_ASSERT(first->d_func()->focusPrev->d_func()->focusNext == first);
2171 Q_ASSERT(second->d_func()->focusNext->d_func()->focusPrev == second);
2172 Q_ASSERT(second->d_func()->focusPrev->d_func()->focusNext == second);
2177 If \a on is true, this function enables \a attribute; otherwise
2178 \a attribute is disabled.
2180 See the class documentation for QGraphicsWidget for a complete list of
2181 which attributes are supported, and what they are for.
2183 \sa testAttribute(), QWidget::setAttribute()
2185 void QGraphicsWidget::setAttribute(Qt::WidgetAttribute attribute, bool on)
2187 Q_D(QGraphicsWidget);
2188 // ### most flags require some immediate action
2189 // ### we might want to qWarn use of unsupported attributes
2190 // ### we might want to not use Qt::WidgetAttribute, but roll our own instead
2191 d->setAttribute(attribute, on);
2195 Returns true if \a attribute is enabled for this widget; otherwise,
2200 bool QGraphicsWidget::testAttribute(Qt::WidgetAttribute attribute) const
2202 Q_D(const QGraphicsWidget);
2203 return d->testAttribute(attribute);
2209 int QGraphicsWidget::type() const
2217 void QGraphicsWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
2225 This virtual function is called by QGraphicsScene to draw the window frame
2226 for windows using \a painter, \a option, and \a widget, in local
2227 coordinates. The base implementation uses the current style to render the
2228 frame and title bar.
2230 You can reimplement this function in a subclass of QGraphicsWidget to
2231 provide custom rendering of the widget's window frame.
2233 \sa QGraphicsItem::paint()
2235 void QGraphicsWidget::paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option,
2238 const bool fillBackground = !testAttribute(Qt::WA_OpaquePaintEvent)
2239 && !testAttribute(Qt::WA_NoSystemBackground);
2240 QGraphicsProxyWidget *proxy = qobject_cast<QGraphicsProxyWidget *>(this);
2241 const bool embeddedWidgetFillsOwnBackground = proxy && proxy->widget();
2243 if (rect().contains(option->exposedRect)) {
2244 if (fillBackground && !embeddedWidgetFillsOwnBackground)
2245 painter->fillRect(option->exposedRect, palette().window());
2249 Q_D(QGraphicsWidget);
2251 QRect windowFrameRect = QRect(QPoint(), windowFrameGeometry().size().toSize());
2252 QStyleOptionTitleBar bar;
2253 bar.QStyleOption::operator=(*option);
2254 d->initStyleOptionTitleBar(&bar); // this clear flags in bar.state
2255 d->ensureWindowData();
2256 if (d->windowData->buttonMouseOver)
2257 bar.state |= QStyle::State_MouseOver;
2259 bar.state &= ~QStyle::State_MouseOver;
2260 if (d->windowData->buttonSunken)
2261 bar.state |= QStyle::State_Sunken;
2263 bar.state &= ~QStyle::State_Sunken;
2265 bar.rect = windowFrameRect;
2267 // translate painter to make the style happy
2268 const QPointF styleOrigin = this->windowFrameRect().topLeft();
2269 painter->translate(styleOrigin);
2272 const QSize pixmapSize = windowFrameRect.size();
2273 if (pixmapSize.width() <= 0 || pixmapSize.height() <= 0)
2275 QPainter *realPainter = painter;
2276 QPixmap pm(pixmapSize);
2277 painter = new QPainter(&pm);
2281 QStyleHintReturnMask mask;
2282 bool setMask = style()->styleHint(QStyle::SH_WindowFrame_Mask, &bar, widget, &mask) && !mask.region.isEmpty();
2283 bool hasBorder = !style()->styleHint(QStyle::SH_TitleBar_NoBorder, &bar, widget);
2284 int frameWidth = style()->pixelMetric(QStyle::PM_MDIFrameWidth, &bar, widget);
2287 painter->setClipRegion(mask.region, Qt::IntersectClip);
2289 if (fillBackground) {
2290 if (embeddedWidgetFillsOwnBackground) {
2291 // Don't fill the background twice.
2292 QPainterPath windowFrameBackground;
2293 windowFrameBackground.addRect(windowFrameRect);
2294 // Adjust with 0.5 to avoid border artifacts between
2295 // widget background and frame background.
2296 windowFrameBackground.addRect(rect().translated(-styleOrigin).adjusted(0.5, 0.5, -0.5, -0.5));
2297 painter->fillPath(windowFrameBackground, palette().window());
2299 painter->fillRect(windowFrameRect, palette().window());
2302 painter->setRenderHint(QPainter::NonCosmeticDefaultPen);
2305 int height = (int)d->titleBarHeight(bar);
2306 bar.rect.setHeight(height);
2307 if (hasBorder) // Frame is painted by PE_FrameWindow
2308 bar.rect.adjust(frameWidth, frameWidth, -frameWidth, 0);
2311 painter->setFont(QApplication::font("QMdiSubWindowTitleBar"));
2312 style()->drawComplexControl(QStyle::CC_TitleBar, &bar, painter, widget);
2316 // Draw window frame
2317 QStyleOptionFrame frameOptions;
2318 frameOptions.QStyleOption::operator=(*option);
2319 initStyleOption(&frameOptions);
2321 painter->setClipRect(windowFrameRect.adjusted(0, +height, 0, 0), Qt::IntersectClip);
2323 frameOptions.state |= QStyle::State_HasFocus;
2325 frameOptions.state &= ~QStyle::State_HasFocus;
2327 bool isActive = isActiveWindow();
2329 frameOptions.state |= QStyle::State_Active;
2331 frameOptions.state &= ~QStyle::State_Active;
2334 frameOptions.palette.setCurrentColorGroup(isActive ? QPalette::Active : QPalette::Normal);
2335 frameOptions.rect = windowFrameRect;
2336 frameOptions.lineWidth = style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth, 0, widget);
2337 frameOptions.midLineWidth = 1;
2338 style()->drawPrimitive(QStyle::PE_FrameWindow, &frameOptions, painter, widget);
2341 realPainter->drawPixmap(QPoint(), pm);
2349 QRectF QGraphicsWidget::boundingRect() const
2351 return windowFrameRect();
2357 QPainterPath QGraphicsWidget::shape() const
2360 path.addRect(rect());
2365 Call this function to close the widget.
2367 Returns true if the widget was closed; otherwise returns false.
2368 This slot will first send a QCloseEvent to the widget, which may or may
2369 not accept the event. If the event was ignored, nothing happens. If the
2370 event was accepted, it will hide() the widget.
2372 If the widget has the Qt::WA_DeleteOnClose attribute set it will be
2375 bool QGraphicsWidget::close()
2377 QCloseEvent closeEvent;
2378 QApplication::sendEvent(this, &closeEvent);
2379 if (!closeEvent.isAccepted()) {
2386 if (testAttribute(Qt::WA_DeleteOnClose)) {
2392 #ifdef Q_NO_USING_KEYWORD
2394 \fn const QObjectList &QGraphicsWidget::children() const
2397 This function returns the same value as QObject::children(). It's
2398 provided to differentiate between the obsolete member
2399 QGraphicsItem::children() and QObject::children(). QGraphicsItem now
2400 provides childItems() instead.
2405 void QGraphicsWidget::dumpFocusChain()
2407 qDebug() << "=========== Dumping focus chain ==============";
2409 QGraphicsWidget *next = this;
2410 QSet<QGraphicsWidget*> visited;
2413 qWarning("Found a focus chain that is not circular, (next == 0)");
2416 qDebug() << i++ << QString::number(uint(next), 16) << next->className() << next->data(0) << QString::fromLatin1("focusItem:%1").arg(next->hasFocus() ? '1' : '0') << QLatin1String("next:") << next->d_func()->focusNext->data(0) << QLatin1String("prev:") << next->d_func()->focusPrev->data(0);
2417 if (visited.contains(next)) {
2418 qWarning("Already visited this node. However, I expected to dump until I found myself.");
2422 next = next->d_func()->focusNext;
2423 } while (next != this);
2429 #endif //QT_NO_GRAPHICSVIEW