1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qapplication.h"
43 #include "qapplication_p.h"
46 #include "qdesktopwidget.h"
51 #include "qmetaobject.h"
56 #include "qstylefactory.h"
59 #include "qstyleoption.h"
60 #ifndef QT_NO_ACCESSIBILITY
61 # include "qaccessible.h"
64 # include "qt_windows.h"
67 # include "qt_mac_p.h"
68 # include "qt_cocoa_helpers_mac_p.h"
69 # include "qmainwindow.h"
70 # include "qtoolbar.h"
71 # include <private/qmainwindowlayout_p.h>
74 # include "qwsdisplay_qws.h"
75 # include "qwsmanager_qws.h"
76 # include "qpaintengine.h" // for PorterDuff
77 # include "private/qwindowsurface_qws_p.h"
80 #include "qplatformwindow_qpa.h"
84 #include "qwhatsthis.h"
86 #include "private/qstylesheetstyle_p.h"
87 #include "private/qstyle_p.h"
88 #include "private/qinputcontext_p.h"
89 #include "qfileinfo.h"
90 #include "private/qsoftkeymanager_p.h"
92 #if defined (Q_WS_WIN)
93 # include <private/qwininputcontext_p.h>
97 # include <private/qpaintengine_x11_p.h>
98 # include "qx11info_x11.h"
101 #include <private/qgraphicseffect_p.h>
102 #include <private/qwindowsurface_p.h>
103 #include <private/qbackingstore_p.h>
105 # include <private/qpaintengine_mac_p.h>
107 #include <private/qpaintengine_raster_p.h>
109 #if defined(Q_OS_SYMBIAN)
110 #include "private/qt_s60_p.h"
113 #include "qwidget_p.h"
114 #include "qaction_p.h"
115 #include "qlayout_p.h"
116 #include "QtGui/qgraphicsproxywidget.h"
117 #include "QtGui/qgraphicsscene.h"
118 #include "private/qgraphicsproxywidget_p.h"
119 #include "QtGui/qabstractscrollarea.h"
120 #include "private/qabstractscrollarea_p.h"
121 #include "private/qevent_p.h"
123 #include "private/qgraphicssystem_p.h"
124 #include "private/qgesturemanager_p.h"
126 #ifdef QT_KEYPAD_NAVIGATION
127 #include "qtabwidget.h" // Needed in inTabWidget()
128 #endif // QT_KEYPAD_NAVIGATION
130 // widget/widget data creation count
131 //#define QWIDGET_EXTRA_DEBUG
132 //#define ALIEN_DEBUG
136 #if !defined(Q_WS_QWS)
137 static bool qt_enable_backingstore = true;
140 // for compatibility with Qt 4.0
141 Q_GUI_EXPORT void qt_x11_set_global_double_buffer(bool enable)
143 qt_enable_backingstore = enable;
147 static inline bool qRectIntersects(const QRect &r1, const QRect &r2)
149 return (qMax(r1.left(), r2.left()) <= qMin(r1.right(), r2.right()) &&
150 qMax(r1.top(), r2.top()) <= qMin(r1.bottom(), r2.bottom()));
153 static inline bool hasBackingStoreSupport()
156 return QApplicationPrivate::graphicsSystem() != 0;
163 # define QT_NO_PAINT_DEBUG
166 extern bool qt_sendSpontaneousEvent(QObject*, QEvent*); // qapplication.cpp
167 extern QDesktopWidget *qt_desktopWidget; // qapplication.cpp
171 \class QWidgetBackingStoreTracker
172 \brief Class which allows tracking of which widgets are using a given backing store
174 QWidgetBackingStoreTracker is a thin wrapper around a QWidgetBackingStore pointer,
175 which maintains a list of the QWidgets which are currently using the backing
176 store. This list is modified via the registerWidget and unregisterWidget functions.
179 QWidgetBackingStoreTracker::QWidgetBackingStoreTracker()
185 QWidgetBackingStoreTracker::~QWidgetBackingStoreTracker()
192 Destroy the contained QWidgetBackingStore, if not null, and clear the list of
193 widgets using the backing store, then create a new QWidgetBackingStore, providing
196 void QWidgetBackingStoreTracker::create(QWidget *widget)
199 m_ptr = new QWidgetBackingStore(widget);
204 Destroy the contained QWidgetBackingStore, if not null, and clear the list of
205 widgets using the backing store.
207 void QWidgetBackingStoreTracker::destroy()
216 Add the widget to the list of widgets currently using the backing store.
217 If the widget was already in the list, this function is a no-op.
219 void QWidgetBackingStoreTracker::registerWidget(QWidget *w)
222 Q_ASSERT(w->internalWinId());
223 Q_ASSERT(qt_widget_private(w)->maybeBackingStore() == m_ptr);
229 Remove the widget from the list of widgets currently using the backing store.
230 If the widget was in the list, and removing it causes the list to be empty,
231 the backing store is deleted.
232 If the widget was not in the list, this function is a no-op.
234 void QWidgetBackingStoreTracker::unregisterWidget(QWidget *w)
236 if (m_widgets.remove(w) && m_widgets.isEmpty()) {
244 Recursively remove widget and all of its descendents.
246 void QWidgetBackingStoreTracker::unregisterWidgetSubtree(QWidget *widget)
248 unregisterWidget(widget);
249 foreach (QObject *child, widget->children())
250 if (QWidget *childWidget = qobject_cast<QWidget *>(child))
251 unregisterWidgetSubtree(childWidget);
254 QWidgetPrivate::QWidgetPrivate(int version)
255 : QObjectPrivate(version)
264 , extraPaintEngine(0)
267 #if !defined(QT_NO_IM)
268 , imHints(Qt::ImhNone)
270 , inheritedFontResolveMask(0)
271 , inheritedPaletteResolveMask(0)
276 , leftLayoutItemMargin(0)
277 , topLayoutItemMargin(0)
278 , rightLayoutItemMargin(0)
279 , bottomLayoutItemMargin(0)
281 , size_policy(QSizePolicy::Preferred, QSizePolicy::Preferred)
282 , fg_role(QPalette::NoRole)
283 , bg_role(QPalette::NoRole)
284 , dirtyOpaqueChildren(1)
290 , usesDoubleBufferedGLContext(0)
292 , inheritsInputMethodHints(0)
294 #if defined(Q_WS_X11)
296 #elif defined(Q_WS_WIN)
298 #ifndef QT_NO_GESTURES
299 , nativeGesturePanEnabled(0)
301 #elif defined(Q_WS_MAC)
302 , needWindowChange(0)
303 , hasAlienChildren(0)
306 #elif defined (Q_WS_QPA)
311 qFatal("QWidget: Must construct a QApplication before a QPaintDevice");
315 if (version != QObjectPrivateVersion)
316 qFatal("Cannot mix incompatible Qt libraries");
319 memset(high_attributes, 0, sizeof(high_attributes));
321 drawRectOriginalAdded = false;
322 originalDrawMethod = true;
323 changeMethods = false;
324 hasOwnContext = false;
325 isInUnifiedToolbar = false;
327 #endif // QT_MAC_USE_COCOA
328 #ifdef QWIDGET_EXTRA_DEBUG
329 static int count = 0;
330 qDebug() << "widgets" << ++count;
335 QWidgetPrivate::~QWidgetPrivate()
343 #ifndef QT_NO_GRAPHICSEFFECT
344 delete graphicsEffect;
345 #endif //QT_NO_GRAPHICSEFFECT
348 class QDummyWindowSurface : public QWindowSurface
351 QDummyWindowSurface(QWidget *window) : QWindowSurface(window) {}
352 QPaintDevice *paintDevice() { return window(); }
353 void flush(QWidget *, const QRegion &, const QPoint &) {}
356 QWindowSurface *QWidgetPrivate::createDefaultWindowSurface()
360 QWindowSurface *surface;
361 if (q->property("_q_DummyWindowSurface").toBool()) {
362 surface = new QDummyWindowSurface(q);
364 if (QApplicationPrivate::graphicsSystem())
365 surface = QApplicationPrivate::graphicsSystem()->createWindowSurface(q);
367 surface = createDefaultWindowSurface_sys();
376 void QWidgetPrivate::scrollChildren(int dx, int dy)
379 if (q->children().size() > 0) { // scroll children
381 QObjectList childObjects = q->children();
382 for (int i = 0; i < childObjects.size(); ++i) { // move all children
383 QWidget *w = qobject_cast<QWidget*>(childObjects.at(i));
384 if (w && !w->isWindow()) {
385 QPoint oldp = w->pos();
386 QRect r(w->pos() + pd, w->size());
389 if (w->testAttribute(Qt::WA_WState_Created))
390 w->d_func()->setWSGeometry();
392 w->d_func()->setDirtyOpaqueRegion();
393 QMoveEvent e(r.topLeft(), oldp);
394 QApplication::sendEvent(w, &e);
400 QInputContext *QWidgetPrivate::inputContext() const
405 return qApp->inputContext();
412 This function returns the QInputContext for this widget. By
413 default the input context is inherited from the widgets
414 parent. For toplevels it is inherited from QApplication.
416 You can override this and set a special input context for this
417 widget by using the setInputContext() method.
419 \sa setInputContext()
421 QInputContext *QWidget::inputContext()
424 if (!testAttribute(Qt::WA_InputMethodEnabled))
427 return d->inputContext();
431 This function sets the input context \a context
434 Qt takes ownership of the given input \a context.
438 void QWidget::setInputContext(QInputContext *context)
441 if (!testAttribute(Qt::WA_InputMethodEnabled))
444 if (context == d->ic)
450 d->ic->setParent(this);
458 This function can be called on the widget that currently has focus
459 to reset the input method operating on it.
461 This function is providing for convenience, instead you should use
462 \l{QInputContext::}{reset()} on the input context that was
463 returned by inputContext().
465 \sa QInputContext, inputContext(), QInputContext::reset()
467 void QWidget::resetInputContext()
472 QInputContext *qic = this->inputContext();
478 #ifdef QT_KEYPAD_NAVIGATION
479 QPointer<QWidget> QWidgetPrivate::editingWidget;
482 Returns true if this widget currently has edit focus; otherwise false.
484 This feature is only available in Qt for Embedded Linux.
486 \sa setEditFocus(), QApplication::keypadNavigationEnabled()
488 bool QWidget::hasEditFocus() const
490 const QWidget* w = this;
491 while (w->d_func()->extra && w->d_func()->extra->focus_proxy)
492 w = w->d_func()->extra->focus_proxy;
493 return QWidgetPrivate::editingWidget == w;
497 \fn void QWidget::setEditFocus(bool enable)
499 If \a enable is true, make this widget have edit focus, in which
500 case Qt::Key_Up and Qt::Key_Down will be delivered to the widget
501 normally; otherwise, Qt::Key_Up and Qt::Key_Down are used to
504 This feature is only available in Qt for Embedded Linux and Qt
507 \sa hasEditFocus(), QApplication::keypadNavigationEnabled()
509 void QWidget::setEditFocus(bool on)
512 while (f->d_func()->extra && f->d_func()->extra->focus_proxy)
513 f = f->d_func()->extra->focus_proxy;
515 if (QWidgetPrivate::editingWidget && QWidgetPrivate::editingWidget != f)
516 QWidgetPrivate::editingWidget->setEditFocus(false);
518 if (on && !f->hasFocus())
521 if ((!on && !QWidgetPrivate::editingWidget)
522 || (on && QWidgetPrivate::editingWidget == f)) {
526 if (!on && QWidgetPrivate::editingWidget == f) {
527 QWidgetPrivate::editingWidget = 0;
528 QEvent event(QEvent::LeaveEditFocus);
529 QApplication::sendEvent(f, &event);
530 QApplication::sendEvent(f->style(), &event);
532 QWidgetPrivate::editingWidget = f;
533 QEvent event(QEvent::EnterEditFocus);
534 QApplication::sendEvent(f, &event);
535 QApplication::sendEvent(f->style(), &event);
541 \property QWidget::autoFillBackground
542 \brief whether the widget background is filled automatically
545 If enabled, this property will cause Qt to fill the background of the
546 widget before invoking the paint event. The color used is defined by the
547 QPalette::Window color role from the widget's \l{QPalette}{palette}.
549 In addition, Windows are always filled with QPalette::Window, unless the
550 WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set.
552 This property cannot be turned off (i.e., set to false) if a widget's
553 parent has a static gradient for its background.
555 \warning Use this property with caution in conjunction with
556 \l{Qt Style Sheets}. When a widget has a style sheet with a valid
557 background or a border-image, this property is automatically disabled.
559 By default, this property is false.
561 \sa Qt::WA_OpaquePaintEvent, Qt::WA_NoSystemBackground,
562 {QWidget#Transparency and Double Buffering}{Transparency and Double Buffering}
564 bool QWidget::autoFillBackground() const
567 return d->extra && d->extra->autoFillBackground;
570 void QWidget::setAutoFillBackground(bool enabled)
575 if (d->extra->autoFillBackground == enabled)
578 d->extra->autoFillBackground = enabled;
586 \brief The QWidget class is the base class of all user interface objects.
588 \ingroup basicwidgets
591 The widget is the atom of the user interface: it receives mouse, keyboard
592 and other events from the window system, and paints a representation of
593 itself on the screen. Every widget is rectangular, and they are sorted in a
594 Z-order. A widget is clipped by its parent and by the widgets in front of
597 A widget that is not embedded in a parent widget is called a window.
598 Usually, windows have a frame and a title bar, although it is also possible
599 to create windows without such decoration using suitable
600 \l{Qt::WindowFlags}{window flags}). In Qt, QMainWindow and the various
601 subclasses of QDialog are the most common window types.
603 Every widget's constructor accepts one or two standard arguments:
606 \i \c{QWidget *parent = 0} is the parent of the new widget. If it is 0
607 (the default), the new widget will be a window. If not, it will be
608 a child of \e parent, and be constrained by \e parent's geometry
609 (unless you specify Qt::Window as window flag).
610 \i \c{Qt::WindowFlags f = 0} (where available) sets the window flags;
611 the default is suitable for almost all widgets, but to get, for
612 example, a window without a window system frame, you must use
616 QWidget has many member functions, but some of them have little direct
617 functionality; for example, QWidget has a font property, but never uses
618 this itself. There are many subclasses which provide real functionality,
619 such as QLabel, QPushButton, QListWidget, and QTabWidget.
622 \section1 Top-Level and Child Widgets
624 A widget without a parent widget is always an independent window (top-level
625 widget). For these widgets, setWindowTitle() and setWindowIcon() set the
626 title bar and icon respectively.
628 Non-window widgets are child widgets, displayed within their parent
629 widgets. Most widgets in Qt are mainly useful as child widgets. For
630 example, it is possible to display a button as a top-level window, but most
631 people prefer to put their buttons inside other widgets, such as QDialog.
633 \image parent-child-widgets.png A parent widget containing various child widgets.
635 The diagram above shows a QGroupBox widget being used to hold various child
636 widgets in a layout provided by QGridLayout. The QLabel child widgets have
637 been outlined to indicate their full sizes.
639 If you want to use a QWidget to hold child widgets you will usually want to
640 add a layout to the parent QWidget. See \l{Layout Management} for more
644 \section1 Composite Widgets
646 When a widget is used as a container to group a number of child widgets, it
647 is known as a composite widget. These can be created by constructing a
648 widget with the required visual properties - a QFrame, for example - and
649 adding child widgets to it, usually managed by a layout. The above diagram
650 shows such a composite widget that was created using \l{Qt Designer}.
652 Composite widgets can also be created by subclassing a standard widget,
653 such as QWidget or QFrame, and adding the necessary layout and child
654 widgets in the constructor of the subclass. Many of the \l{Qt Examples}
655 {examples provided with Qt} use this approach, and it is also covered in
656 the Qt \l{Tutorials}.
659 \section1 Custom Widgets and Painting
661 Since QWidget is a subclass of QPaintDevice, subclasses can be used to
662 display custom content that is composed using a series of painting
663 operations with an instance of the QPainter class. This approach contrasts
664 with the canvas-style approach used by the \l{Graphics View}
665 {Graphics View Framework} where items are added to a scene by the
666 application and are rendered by the framework itself.
668 Each widget performs all painting operations from within its paintEvent()
669 function. This is called whenever the widget needs to be redrawn, either
670 as a result of some external change or when requested by the application.
672 The \l{widgets/analogclock}{Analog Clock example} shows how a simple widget
673 can handle paint events.
676 \section1 Size Hints and Size Policies
678 When implementing a new widget, it is almost always useful to reimplement
679 sizeHint() to provide a reasonable default size for the widget and to set
680 the correct size policy with setSizePolicy().
682 By default, composite widgets which do not provide a size hint will be
683 sized according to the space requirements of their child widgets.
685 The size policy lets you supply good default behavior for the layout
686 management system, so that other widgets can contain and manage yours
687 easily. The default size policy indicates that the size hint represents
688 the preferred size of the widget, and this is often good enough for many
691 \note The size of top-level widgets are constrained to 2/3 of the desktop's
692 height and width. You can resize() the widget manually if these bounds are
698 Widgets respond to events that are typically caused by user actions. Qt
699 delivers events to widgets by calling specific event handler functions with
700 instances of QEvent subclasses containing information about each event.
702 If your widget only contains child widgets, you probably do not need to
703 implement any event handlers. If you want to detect a mouse click in a
704 child widget call the child's underMouse() function inside the widget's
707 The \l{widgets/scribble}{Scribble example} implements a wider set of
708 events to handle mouse movement, button presses, and window resizing.
710 You will need to supply the behavior and content for your own widgets, but
711 here is a brief overview of the events that are relevant to QWidget,
712 starting with the most common ones:
715 \i paintEvent() is called whenever the widget needs to be repainted.
716 Every widget displaying custom content must implement it. Painting
717 using a QPainter can only take place in a paintEvent() or a
718 function called by a paintEvent().
719 \i resizeEvent() is called when the widget has been resized.
720 \i mousePressEvent() is called when a mouse button is pressed while
721 the mouse cursor is inside the widget, or when the widget has
722 grabbed the mouse using grabMouse(). Pressing the mouse without
723 releasing it is effectively the same as calling grabMouse().
724 \i mouseReleaseEvent() is called when a mouse button is released. A
725 widget receives mouse release events when it has received the
726 corresponding mouse press event. This means that if the user
727 presses the mouse inside \e your widget, then drags the mouse
728 somewhere else before releasing the mouse button, \e your widget
729 receives the release event. There is one exception: if a popup menu
730 appears while the mouse button is held down, this popup immediately
731 steals the mouse events.
732 \i mouseDoubleClickEvent() is called when the user double-clicks in
733 the widget. If the user double-clicks, the widget receives a mouse
734 press event, a mouse release event and finally this event instead
735 of a second mouse press event. (Some mouse move events may also be
736 received if the mouse is not held steady during this operation.) It
737 is \e{not possible} to distinguish a click from a double-click
738 until the second click arrives. (This is one reason why most GUI
739 books recommend that double-clicks be an extension of
740 single-clicks, rather than trigger a different action.)
743 Widgets that accept keyboard input need to reimplement a few more event
747 \i keyPressEvent() is called whenever a key is pressed, and again when
748 a key has been held down long enough for it to auto-repeat. The
749 \key Tab and \key Shift+Tab keys are only passed to the widget if
750 they are not used by the focus-change mechanisms. To force those
751 keys to be processed by your widget, you must reimplement
753 \i focusInEvent() is called when the widget gains keyboard focus
754 (assuming you have called setFocusPolicy()). Well-behaved widgets
755 indicate that they own the keyboard focus in a clear but discreet
757 \i focusOutEvent() is called when the widget loses keyboard focus.
760 You may be required to also reimplement some of the less common event
764 \i mouseMoveEvent() is called whenever the mouse moves while a mouse
765 button is held down. This can be useful during drag and drop
766 operations. If you call \l{setMouseTracking()}{setMouseTracking}(true),
767 you get mouse move events even when no buttons are held down.
768 (See also the \l{Drag and Drop} guide.)
769 \i keyReleaseEvent() is called whenever a key is released and while it
770 is held down (if the key is auto-repeating). In that case, the
771 widget will receive a pair of key release and key press event for
772 every repeat. The \key Tab and \key Shift+Tab keys are only passed
773 to the widget if they are not used by the focus-change mechanisms.
774 To force those keys to be processed by your widget, you must
775 reimplement QWidget::event().
776 \i wheelEvent() is called whenever the user turns the mouse wheel
777 while the widget has the focus.
778 \i enterEvent() is called when the mouse enters the widget's screen
779 space. (This excludes screen space owned by any of the widget's
781 \i leaveEvent() is called when the mouse leaves the widget's screen
782 space. If the mouse enters a child widget it will not cause a
784 \i moveEvent() is called when the widget has been moved relative to
786 \i closeEvent() is called when the user closes the widget (or when
790 There are also some rather obscure events described in the documentation
791 for QEvent::Type. To handle these events, you need to reimplement event()
794 The default implementation of event() handles \key Tab and \key Shift+Tab
795 (to move the keyboard focus), and passes on most of the other events to
796 one of the more specialized handlers above.
798 Events and the mechanism used to deliver them are covered in
799 \l{The Event System}.
801 \section1 Groups of Functions and Properties
804 \header \i Context \i Functions and Properties
806 \row \i Window functions \i
813 \row \i Top-level windows \i
814 \l windowModified, \l windowTitle, \l windowIcon, \l windowIconText,
815 \l isActiveWindow, activateWindow(), \l minimized, showMinimized(),
816 \l maximized, showMaximized(), \l fullScreen, showFullScreen(),
819 \row \i Window contents \i
825 \l pos, x(), y(), \l rect, \l size, width(), height(), move(), resize(),
826 \l sizePolicy, sizeHint(), minimumSizeHint(),
827 updateGeometry(), layout(),
828 \l frameGeometry, \l geometry, \l childrenRect, \l childrenRegion,
830 mapFromGlobal(), mapToGlobal(),
831 mapFromParent(), mapToParent(),
832 \l maximumSize, \l minimumSize, \l sizeIncrement,
833 \l baseSize, setFixedSize()
836 \l visible, isVisibleTo(),
837 \l enabled, isEnabledTo(),
844 \row \i Look and feel \i
851 backgroundRole(), setBackgroundRole(),
852 fontInfo(), fontMetrics().
854 \row \i Keyboard focus functions \i
855 \l focus, \l focusPolicy,
856 setFocus(), clearFocus(), setTabOrder(), setFocusProxy(),
857 focusNextChild(), focusPreviousChild().
859 \row \i Mouse and keyboard grabbing \i
860 grabMouse(), releaseMouse(),
861 grabKeyboard(), releaseKeyboard(),
862 mouseGrabber(), keyboardGrabber().
864 \row \i Event handlers \i
868 mouseDoubleClickEvent(),
891 \row \i System functions \i
892 parentWidget(), window(), setParent(), winId(),
895 \row \i Interactive help \i
896 setToolTip(), setWhatsThis()
901 \section1 Widget Style Sheets
903 In addition to the standard widget styles for each platform, widgets can
904 also be styled according to rules specified in a \l{styleSheet}
905 {style sheet}. This feature enables you to customize the appearance of
906 specific widgets to provide visual cues to users about their purpose. For
907 example, a button could be styled in a particular way to indicate that it
908 performs a destructive action.
910 The use of widget style sheets is described in more detail in the
911 \l{Qt Style Sheets} document.
914 \section1 Transparency and Double Buffering
916 Since Qt 4.0, QWidget automatically double-buffers its painting, so there
917 is no need to write double-buffering code in paintEvent() to avoid
920 Since Qt 4.1, the Qt::WA_ContentsPropagated widget attribute has been
921 deprecated. Instead, the contents of parent widgets are propagated by
922 default to each of their children as long as Qt::WA_PaintOnScreen is not
923 set. Custom widgets can be written to take advantage of this feature by
924 updating irregular regions (to create non-rectangular child widgets), or
925 painting with colors that have less than full alpha component. The
926 following diagram shows how attributes and properties of a custom widget
927 can be fine-tuned to achieve different effects.
929 \image propagation-custom.png
931 In the above diagram, a semi-transparent rectangular child widget with an
932 area removed is constructed and added to a parent widget (a QLabel showing
933 a pixmap). Then, different properties and widget attributes are set to
934 achieve different effects:
937 \i The left widget has no additional properties or widget attributes
938 set. This default state suits most custom widgets using
939 transparency, are irregularly-shaped, or do not paint over their
940 entire area with an opaque brush.
941 \i The center widget has the \l autoFillBackground property set. This
942 property is used with custom widgets that rely on the widget to
943 supply a default background, and do not paint over their entire
944 area with an opaque brush.
945 \i The right widget has the Qt::WA_OpaquePaintEvent widget attribute
946 set. This indicates that the widget will paint over its entire area
947 with opaque colors. The widget's area will initially be
948 \e{uninitialized}, represented in the diagram with a red diagonal
949 grid pattern that shines through the overpainted area. The
950 Qt::WA_OpaquePaintArea attribute is useful for widgets that need to
951 paint their own specialized contents quickly and do not need a
952 default filled background.
955 To rapidly update custom widgets with simple background colors, such as
956 real-time plotting or graphing widgets, it is better to define a suitable
957 background color (using setBackgroundRole() with the
958 QPalette::Window role), set the \l autoFillBackground property, and only
959 implement the necessary drawing functionality in the widget's paintEvent().
961 To rapidly update custom widgets that constantly paint over their entire
962 areas with opaque content, e.g., video streaming widgets, it is better to
963 set the widget's Qt::WA_OpaquePaintEvent, avoiding any unnecessary overhead
964 associated with repainting the widget's background.
966 If a widget has both the Qt::WA_OpaquePaintEvent widget attribute \e{and}
967 the \l autoFillBackground property set, the Qt::WA_OpaquePaintEvent
968 attribute takes precedence. Depending on your requirements, you should
969 choose either one of them.
971 Since Qt 4.1, the contents of parent widgets are also propagated to
972 standard Qt widgets. This can lead to some unexpected results if the
973 parent widget is decorated in a non-standard way, as shown in the diagram
976 \image propagation-standard.png
978 The scope for customizing the painting behavior of standard Qt widgets,
979 without resorting to subclassing, is slightly less than that possible for
980 custom widgets. Usually, the desired appearance of a standard widget can be
981 achieved by setting its \l autoFillBackground property.
984 \section1 Creating Translucent Windows
986 Since Qt 4.5, it has been possible to create windows with translucent regions
987 on window systems that support compositing.
989 To enable this feature in a top-level widget, set its Qt::WA_TranslucentBackground
990 attribute with setAttribute() and ensure that its background is painted with
991 non-opaque colors in the regions you want to be partially transparent.
996 \o X11: This feature relies on the use of an X server that supports ARGB visuals
997 and a compositing window manager.
998 \o Windows: The widget needs to have the Qt::FramelessWindowHint window flag set
999 for the translucency to work.
1003 \section1 Native Widgets vs Alien Widgets
1005 Introduced in Qt 4.4, alien widgets are widgets unknown to the windowing
1006 system. They do not have a native window handle associated with them. This
1007 feature significantly speeds up widget painting, resizing, and removes flicker.
1009 Should you require the old behavior with native windows, you can choose
1010 one of the following options:
1013 \i Use the \c{QT_USE_NATIVE_WINDOWS=1} in your environment.
1014 \i Set the Qt::AA_NativeWindows attribute on your application. All
1015 widgets will be native widgets.
1016 \i Set the Qt::WA_NativeWindow attribute on widgets: The widget itself
1017 and all of its ancestors will become native (unless
1018 Qt::WA_DontCreateNativeAncestors is set).
1019 \i Call QWidget::winId to enforce a native window (this implies 3).
1020 \i Set the Qt::WA_PaintOnScreen attribute to enforce a native window
1024 \sa QEvent, QPainter, QGridLayout, QBoxLayout
1028 Since Qt 4.6, Softkeys are usually physical keys on a device that have a corresponding label or
1029 other visual representation on the screen that is generally located next to its
1030 physical counterpart. They are most often found on mobile phone platforms. In
1031 modern touch based user interfaces it is also possible to have softkeys that do
1032 not correspond to any physical keys. Softkeys differ from other onscreen labels
1033 in that they are contextual.
1035 In Qt, contextual softkeys are added to a widget by calling addAction() and
1036 passing a \c QAction with a softkey role set on it. When the widget
1037 containing the softkey actions has focus, its softkeys should appear in
1038 the user interface. Softkeys are discovered by traversing the widget
1039 hierarchy so it is possible to define a single set of softkeys that are
1040 present at all times by calling addAction() for a given top level widget.
1042 On some platforms, this concept overlaps with \c QMenuBar such that if no
1043 other softkeys are found and the top level widget is a QMainWindow containing
1044 a QMenuBar, the menubar actions may appear on one of the softkeys.
1046 Note: Currently softkeys are only supported on the Symbian Platform.
1048 \sa addAction(), QAction, QMenuBar
1052 QWidgetMapper *QWidgetPrivate::mapper = 0; // widget with wid
1053 QWidgetSet *QWidgetPrivate::allWidgets = 0; // widgets with no wid
1056 /*****************************************************************************
1057 QWidget utility functions
1058 *****************************************************************************/
1060 QRegion qt_dirtyRegion(QWidget *widget)
1065 QWidgetBackingStore *bs = qt_widget_private(widget)->maybeBackingStore();
1069 return bs->dirtyRegion(widget);
1072 /*****************************************************************************
1073 QWidget member functions
1074 *****************************************************************************/
1079 \i Qt::WA_WState_Created The widget has a valid winId().
1080 \i Qt::WA_WState_Visible The widget is currently visible.
1081 \i Qt::WA_WState_Hidden The widget is hidden, i.e. it won't
1082 become visible unless you call show() on it. Qt::WA_WState_Hidden
1083 implies !Qt::WA_WState_Visible.
1084 \i Qt::WA_WState_CompressKeys Compress keyboard events.
1085 \i Qt::WA_WState_BlockUpdates Repaints and updates are disabled.
1086 \i Qt::WA_WState_InPaintEvent Currently processing a paint event.
1087 \i Qt::WA_WState_Reparented The widget has been reparented.
1088 \i Qt::WA_WState_ConfigPending A configuration (resize/move) event is pending.
1089 \i Qt::WA_WState_DND (Deprecated) The widget supports drag and drop, see setAcceptDrops().
1093 struct QWidgetExceptionCleaner
1095 /* this cleans up when the constructor throws an exception */
1096 static inline void cleanup(QWidget *that, QWidgetPrivate *d)
1098 #ifdef QT_NO_EXCEPTIONS
1102 QWidgetPrivate::allWidgets->remove(that);
1103 if (d->focus_next != that) {
1105 d->focus_next->d_func()->focus_prev = d->focus_prev;
1107 d->focus_prev->d_func()->focus_next = d->focus_next;
1114 Constructs a widget which is a child of \a parent, with widget
1117 If \a parent is 0, the new widget becomes a window. If
1118 \a parent is another widget, this widget becomes a child window
1119 inside \a parent. The new widget is deleted when its \a parent is
1122 The widget flags argument, \a f, is normally 0, but it can be set
1123 to customize the frame of a window (i.e. \a
1124 parent must be 0). To customize the frame, use a value composed
1125 from the bitwise OR of any of the \l{Qt::WindowFlags}{window flags}.
1127 If you add a child widget to an already visible widget you must
1128 explicitly show the child to make it visible.
1130 Note that the X11 version of Qt may not be able to deliver all
1131 combinations of style flags on all systems. This is because on
1132 X11, Qt can only ask the window manager, and the window manager
1133 can override the application's settings. On Windows, Qt can set
1134 whatever flags you want.
1138 QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
1139 : QObject(*new QWidgetPrivate, 0), QPaintDevice()
1142 d_func()->init(parent, f);
1144 QWidgetExceptionCleaner::cleanup(this, d_func());
1154 QWidget::QWidget(QWidget *parent, const char *name, Qt::WindowFlags f)
1155 : QObject(*new QWidgetPrivate, 0), QPaintDevice()
1158 d_func()->init(parent , f);
1159 setObjectName(QString::fromAscii(name));
1161 QWidgetExceptionCleaner::cleanup(this, d_func());
1169 QWidget::QWidget(QWidgetPrivate &dd, QWidget* parent, Qt::WindowFlags f)
1170 : QObject(dd, 0), QPaintDevice()
1176 QWidgetExceptionCleaner::cleanup(this, d_func());
1184 int QWidget::devType() const
1186 return QInternal::Widget;
1190 //### w is a "this" ptr, passed as a param because QWorkspace needs special logic
1191 void QWidgetPrivate::adjustFlags(Qt::WindowFlags &flags, QWidget *w)
1193 bool customize = (flags & (Qt::CustomizeWindowHint
1194 | Qt::FramelessWindowHint
1195 | Qt::WindowTitleHint
1196 | Qt::WindowSystemMenuHint
1197 | Qt::WindowMinimizeButtonHint
1198 | Qt::WindowMaximizeButtonHint
1199 | Qt::WindowCloseButtonHint
1200 | Qt::WindowContextHelpButtonHint));
1202 uint type = (flags & Qt::WindowType_Mask);
1204 if ((type == Qt::Widget || type == Qt::SubWindow) && w && !w->parent()) {
1206 flags |= Qt::Window;
1209 if (flags & Qt::CustomizeWindowHint) {
1210 // modify window flags to make them consistent.
1211 // Only enable this on non-Mac platforms. Since the old way of doing this would
1212 // interpret WindowSystemMenuHint as a close button and we can't change that behavior
1213 // we can't just add this in.
1215 if (flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint)) {
1216 flags |= Qt::WindowSystemMenuHint;
1218 if (flags & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint
1219 | Qt::WindowSystemMenuHint)) {
1221 flags |= Qt::WindowTitleHint;
1222 flags &= ~Qt::FramelessWindowHint;
1224 } else if (customize && !(flags & Qt::FramelessWindowHint)) {
1225 // if any of the window hints that affect the titlebar are set
1226 // and the window is supposed to have frame, we add a titlebar
1227 // and system menu by default.
1228 flags |= Qt::WindowSystemMenuHint;
1229 flags |= Qt::WindowTitleHint;
1232 ; // don't modify window flags if the user explicitly set them.
1233 else if (type == Qt::Dialog || type == Qt::Sheet)
1235 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowContextHelpButtonHint | Qt::WindowCloseButtonHint;
1237 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
1239 else if (type == Qt::Tool)
1240 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
1242 flags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint;
1247 void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
1250 if (QApplication::type() == QApplication::Tty)
1251 qFatal("QWidget: Cannot create a QWidget when no GUI is being used");
1253 Q_ASSERT(allWidgets);
1255 allWidgets->insert(q);
1257 QWidget *desktopWidget = 0;
1258 if (parentWidget && parentWidget->windowType() == Qt::Desktop) {
1259 desktopWidget = parentWidget;
1265 #ifndef QT_NO_THREAD
1267 Q_ASSERT_X(q->thread() == qApp->thread(), "QWidget",
1268 "Widgets must be created in the GUI thread.");
1272 #if defined(Q_WS_X11)
1273 if (desktopWidget) {
1274 // make sure the widget is created on the same screen as the
1275 // programmer specified desktop widget
1276 xinfo = desktopWidget->d_func()->xinfo;
1278 #elif defined(Q_WS_QPA)
1279 if (desktopWidget) {
1280 int screen = desktopWidget->d_func()->screenNumber;
1281 QPlatformIntegration *platform = QApplicationPrivate::platformIntegration();
1282 platform->moveToScreen(q, screen);
1285 Q_UNUSED(desktopWidget);
1288 data.fstrut_dirty = true;
1291 data.widget_attributes = 0;
1292 data.window_flags = f;
1293 data.window_state = 0;
1294 data.focus_policy = 0;
1295 data.context_menu_policy = Qt::DefaultContextMenu;
1296 data.window_modality = Qt::NonModal;
1298 data.sizehint_forced = 0;
1299 data.is_closing = 0;
1301 data.in_set_window_state = 0;
1302 data.in_destructor = false;
1304 // Widgets with Qt::MSWindowsOwnDC (typically QGLWidget) must have a window handle.
1305 if (f & Qt::MSWindowsOwnDC)
1306 q->setAttribute(Qt::WA_NativeWindow);
1309 q->setAttribute(Qt::WA_NativeWindow);
1312 q->setAttribute(Qt::WA_QuitOnClose); // might be cleared in adjustQuitOnCloseAttribute()
1313 adjustQuitOnCloseAttribute();
1315 q->setAttribute(Qt::WA_WState_Hidden);
1317 //give potential windows a bigger "pre-initial" size; create_sys() will give them a new size later
1320 // Don't waste GPU mem for unnecessary large egl surface
1321 data.crect = QRect(0,0,2,2);
1323 data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,360,640);
1326 data.crect = parentWidget ? QRect(0,0,100,30) : QRect(0,0,640,480);
1329 focus_next = focus_prev = q;
1331 if ((f & Qt::WindowType_Mask) == Qt::Desktop)
1333 else if (parentWidget)
1334 q->setParent(parentWidget, data.window_flags);
1336 adjustFlags(data.window_flags, q);
1337 resolveLayoutDirection();
1338 // opaque system background?
1339 const QBrush &background = q->palette().brush(QPalette::Window);
1340 setOpaque(q->isWindow() && background.style() != Qt::NoBrush && background.isOpaque());
1342 data.fnt = QFont(data.fnt, q);
1343 #if defined(Q_WS_X11)
1344 data.fnt.x11SetScreen(xinfo.screen());
1347 q->setAttribute(Qt::WA_PendingMoveEvent);
1348 q->setAttribute(Qt::WA_PendingResizeEvent);
1350 if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances)
1351 QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter;
1353 if (QApplicationPrivate::app_compile_version < 0x040200
1354 || QApplicationPrivate::testAttribute(Qt::AA_ImmediateWidgetCreation))
1358 QEvent e(QEvent::Create);
1359 QApplication::sendEvent(q, &e);
1360 QApplication::postEvent(q, new QEvent(QEvent::PolishRequest));
1362 extraPaintEngine = 0;
1367 void QWidgetPrivate::createRecursively()
1370 q->create(0, true, true);
1371 for (int i = 0; i < children.size(); ++i) {
1372 QWidget *child = qobject_cast<QWidget *>(children.at(i));
1373 if (child && !child->isHidden() && !child->isWindow() && !child->testAttribute(Qt::WA_WState_Created))
1374 child->d_func()->createRecursively();
1382 Creates a new widget window if \a window is 0, otherwise sets the
1383 widget's window to \a window.
1385 Initializes the window (sets the geometry etc.) if \a
1386 initializeWindow is true. If \a initializeWindow is false, no
1387 initialization is performed. This parameter only makes sense if \a
1388 window is a valid window.
1390 Destroys the old window if \a destroyOldWindow is true. If \a
1391 destroyOldWindow is false, you are responsible for destroying the
1392 window yourself (using platform native code).
1394 The QWidget constructor calls create(0,true,true) to create a
1395 window for this widget.
1398 void QWidget::create(WId window, bool initializeWindow, bool destroyOldWindow)
1401 if (testAttribute(Qt::WA_WState_Created) && window == 0 && internalWinId())
1404 if (d->data.in_destructor)
1407 Qt::WindowType type = windowType();
1408 Qt::WindowFlags &flags = data->window_flags;
1410 if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) {
1412 flags |= Qt::Window;
1416 if (QWidget *parent = parentWidget()) {
1418 if (testAttribute(Qt::WA_NativeWindow) == false)
1419 parent->d_func()->hasAlienChildren = true;
1421 if (type & Qt::Window) {
1422 if (!parent->testAttribute(Qt::WA_WState_Created))
1423 parent->createWinId();
1424 } else if (testAttribute(Qt::WA_NativeWindow) && !parent->internalWinId()
1425 && !testAttribute(Qt::WA_DontCreateNativeAncestors)) {
1426 // We're about to create a native child widget that doesn't have a native parent;
1427 // enforce a native handle for the parent unless the Qt::WA_DontCreateNativeAncestors
1428 // attribute is set.
1429 d->createWinId(window);
1430 // Nothing more to do.
1431 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
1432 Q_ASSERT(internalWinId());
1439 if (flags & Qt::WStaticContents)
1440 setAttribute(Qt::WA_StaticContents);
1441 if (flags & Qt::WDestructiveClose)
1442 setAttribute(Qt::WA_DeleteOnClose);
1443 if (flags & Qt::WShowModal)
1444 setWindowModality(Qt::ApplicationModal);
1445 if (flags & Qt::WMouseNoMask)
1446 setAttribute(Qt::WA_MouseNoMask);
1447 if (flags & Qt::WGroupLeader)
1448 setAttribute(Qt::WA_GroupLeader);
1449 if (flags & Qt::WNoMousePropagation)
1450 setAttribute(Qt::WA_NoMousePropagation);
1453 static int paintOnScreenEnv = -1;
1454 if (paintOnScreenEnv == -1)
1455 paintOnScreenEnv = qgetenv("QT_ONSCREEN_PAINT").toInt() > 0 ? 1 : 0;
1456 if (paintOnScreenEnv == 1)
1457 setAttribute(Qt::WA_PaintOnScreen);
1459 if (QApplicationPrivate::testAttribute(Qt::AA_NativeWindows))
1460 setAttribute(Qt::WA_NativeWindow);
1463 qDebug() << "QWidget::create:" << this << "parent:" << parentWidget()
1464 << "Alien?" << !testAttribute(Qt::WA_NativeWindow);
1467 #if defined (Q_WS_WIN) && !defined(QT_NO_DRAGANDDROP)
1468 // Unregister the dropsite (if already registered) before we
1469 // re-create the widget with a native window.
1470 if (testAttribute(Qt::WA_WState_Created) && !internalWinId() && testAttribute(Qt::WA_NativeWindow)
1471 && d->extra && d->extra->dropTarget) {
1472 d->registerDropSite(false);
1474 #endif // defined (Q_WS_WIN) && !defined(QT_NO_DRAGANDDROP)
1476 d->updateIsOpaque();
1478 setAttribute(Qt::WA_WState_Created); // set created flag
1479 d->create_sys(window, initializeWindow, destroyOldWindow);
1481 // a real toplevel window needs a backing store
1482 if (isWindow() && windowType() != Qt::Desktop) {
1483 d->topData()->backingStore.destroy();
1484 if (hasBackingStoreSupport())
1485 d->topData()->backingStore.create(this);
1490 if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered))
1491 setAttribute(Qt::WA_DropSiteRegistered, true);
1494 extern void qt_eval_init_widget(QWidget *w);
1495 qt_eval_init_widget(this);
1498 // need to force the resting of the icon after changing parents
1499 if (testAttribute(Qt::WA_SetWindowIcon))
1500 d->setWindowIcon_sys(true);
1501 if (isWindow() && !d->topData()->iconText.isEmpty())
1502 d->setWindowIconText_helper(d->topData()->iconText);
1503 if (isWindow() && !d->topData()->caption.isEmpty())
1504 d->setWindowTitle_helper(d->topData()->caption);
1505 if (windowType() != Qt::Desktop) {
1506 d->updateSystemBackground();
1508 if (isWindow() && !testAttribute(Qt::WA_SetWindowIcon))
1509 d->setWindowIcon_sys();
1514 Destroys the widget.
1516 All this widget's children are deleted first. The application
1517 exits if this widget is the main widget.
1523 d->data.in_destructor = true;
1525 #if defined (QT_CHECK_STATE)
1526 if (paintingActive())
1527 qWarning("QWidget: %s (%s) deleted while being painted", className(), name());
1530 #ifndef QT_NO_GESTURES
1531 foreach (Qt::GestureType type, d->gestureContext.keys())
1532 ungrabGesture(type);
1535 // force acceptDrops false before winId is destroyed.
1536 d->registerDropSite(false);
1538 #ifndef QT_NO_ACTION
1539 // remove all actions from this widget
1540 for (int i = 0; i < d->actions.size(); ++i) {
1541 QActionPrivate *apriv = d->actions.at(i)->d_func();
1542 apriv->widgets.removeAll(this);
1547 #ifndef QT_NO_SHORTCUT
1548 // Remove all shortcuts grabbed by this
1549 // widget, unless application is closing
1550 if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut))
1551 qApp->d_func()->shortcutMap.removeShortcut(0, this, QKeySequence());
1554 // delete layout while we still are a valid widget
1556 // Remove myself from focus list
1558 Q_ASSERT(d->focus_next->d_func()->focus_prev == this);
1559 Q_ASSERT(d->focus_prev->d_func()->focus_next == this);
1561 if (d->focus_next != this) {
1562 d->focus_next->d_func()->focus_prev = d->focus_prev;
1563 d->focus_prev->d_func()->focus_next = d->focus_next;
1564 d->focus_next = d->focus_prev = 0;
1568 if (QApplicationPrivate::main_widget == this) { // reset main widget
1569 QApplicationPrivate::main_widget = 0;
1570 QApplication::quit();
1577 // swallow this problem because we are in a destructor
1580 d->setDirtyOpaqueRegion();
1582 if (isWindow() && isVisible() && internalWinId()) {
1584 d->close_helper(QWidgetPrivate::CloseNoEvent);
1586 // if we're out of memory, at least hide the window.
1590 // and if that also doesn't work, then give up
1595 #if defined(Q_WS_WIN) || defined(Q_WS_X11)|| defined(Q_WS_MAC)
1596 else if (!internalWinId() && isVisible()) {
1597 qApp->d_func()->sendSyntheticEnterLeave(this);
1599 #elif defined(Q_WS_QWS) || defined(Q_WS_QPA)
1600 else if (isVisible()) {
1601 qApp->d_func()->sendSyntheticEnterLeave(this);
1606 if (d->extra && d->extra->topextra && d->extra->topextra->backingStore) {
1607 // Okay, we are about to destroy the top-level window that owns
1608 // the backing store. Make sure we delete the backing store right away
1609 // before the window handle is invalid. This is important because
1610 // the backing store will delete its window surface, which may or may
1611 // not have a reference to this widget that will be used later to
1612 // notify the window it no longer has a surface.
1613 d->extra->topextra->backingStore.destroy();
1616 if (QWidgetBackingStore *bs = d->maybeBackingStore()) {
1617 bs->removeDirtyWidget(this);
1618 if (testAttribute(Qt::WA_StaticContents))
1619 bs->removeStaticWidget(this);
1622 delete d->needsFlush;
1625 // set all QPointers for this object to zero
1627 QObjectPrivate::clearGuards(this);
1629 if (d->declarativeData) {
1630 QAbstractDeclarativeData::destroyed(d->declarativeData, this);
1631 d->declarativeData = 0; // don't activate again in ~QObject
1634 #ifdef QT_MAC_USE_COCOA
1635 // QCocoaView holds a pointer back to this widget. Clear it now
1636 // to make sure it's not followed later on. The lifetime of the
1637 // QCocoaView might exceed the lifetime of this widget in cases
1638 // where Cocoa itself holds references to it.
1639 extern void qt_mac_clearCocoaViewQWidgetPointers(QWidget *);
1640 qt_mac_clearCocoaViewQWidgetPointers(this);
1643 if (!d->children.isEmpty())
1644 d->deleteChildren();
1646 QApplication::removePostedEvents(this);
1649 destroy(); // platform-dependent cleanup
1651 // if this fails we can't do anything about it but at least we are not allowed to throw.
1653 --QWidgetPrivate::instanceCounter;
1655 if (QWidgetPrivate::allWidgets) // might have been deleted by ~QApplication
1656 QWidgetPrivate::allWidgets->remove(this);
1659 QEvent e(QEvent::Destroy);
1660 QCoreApplication::sendEvent(this, &e);
1661 } QT_CATCH(const std::exception&) {
1662 // if this fails we can't do anything about it but at least we are not allowed to throw.
1666 int QWidgetPrivate::instanceCounter = 0; // Current number of widget instances
1667 int QWidgetPrivate::maxInstances = 0; // Maximum number of widget instances
1669 void QWidgetPrivate::setWinId(WId id) // set widget identifier
1672 // the user might create a widget with Qt::Desktop window
1673 // attribute (or create another QDesktopWidget instance), which
1674 // will have the same windowid (the root window id) as the
1675 // qt_desktopWidget. We should not add the second desktop widget
1677 bool userDesktopWidget = qt_desktopWidget != 0 && qt_desktopWidget != q && q->windowType() == Qt::Desktop;
1678 if (mapper && data.winid && !userDesktopWidget) {
1679 mapper->remove(data.winid);
1682 const WId oldWinId = data.winid;
1685 #if defined(Q_WS_X11)
1686 hd = id; // X11: hd == ident
1688 if (mapper && id && !userDesktopWidget) {
1689 mapper->insert(data.winid, q);
1692 if(oldWinId != id) {
1693 // Do not emit an event when the old winId is destroyed. This only
1694 // happens (a) during widget destruction, and (b) immediately prior
1695 // to creation of a new winId, for example as a result of re-parenting.
1697 QEvent e(QEvent::WinIdChange);
1698 QCoreApplication::sendEvent(q, &e);
1703 void QWidgetPrivate::createTLExtra()
1707 if (!extra->topextra) {
1708 QTLWExtra* x = extra->topextra = new QTLWExtra;
1711 x->windowSurface = 0;
1712 x->sharedPainter = 0;
1713 x->incw = x->inch = 0;
1714 x->basew = x->baseh = 0;
1715 x->frameStrut.setCoords(0, 0, 0, 0);
1716 x->normalGeometry = QRect(0,0,-1,-1);
1719 x->posFromMove = false;
1720 x->sizeAdjusted = false;
1721 x->inTopLevelResize = false;
1722 x->inRepaint = false;
1725 #ifdef QT_MAC_USE_COCOA
1726 x->wasMaximized = false;
1727 #endif // QT_MAC_USE_COCOA
1730 #ifdef QWIDGET_EXTRA_DEBUG
1731 static int count = 0;
1732 qDebug() << "tlextra" << ++count;
1734 #if defined(Q_WS_QPA)
1735 x->platformWindow = 0;
1736 x->platformWindowFormat = QPlatformWindowFormat::defaultFormat();
1743 Creates the widget extra data.
1746 void QWidgetPrivate::createExtra()
1748 if (!extra) { // if not exists
1749 extra = new QWExtra;
1750 extra->glContext = 0;
1751 extra->topextra = 0;
1752 #ifndef QT_NO_GRAPHICSVIEW
1753 extra->proxyWidget = 0;
1755 #ifndef QT_NO_CURSOR
1760 extra->maxw = QWIDGETSIZE_MAX;
1761 extra->maxh = QWIDGETSIZE_MAX;
1762 extra->customDpiX = 0;
1763 extra->customDpiY = 0;
1764 extra->explicitMinSize = 0;
1765 extra->explicitMaxSize = 0;
1766 extra->autoFillBackground = 0;
1767 extra->nativeChildrenForced = 0;
1768 extra->inRenderWithPainter = 0;
1771 #ifdef QWIDGET_EXTRA_DEBUG
1772 static int count = 0;
1773 qDebug() << "extra" << ++count;
1781 Deletes the widget extra data.
1784 void QWidgetPrivate::deleteExtra()
1786 if (extra) { // if exists
1787 #ifndef QT_NO_CURSOR
1791 #ifndef QT_NO_STYLE_STYLESHEET
1792 // dereference the stylesheet style
1793 if (QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(extra->style))
1796 if (extra->topextra) {
1798 extra->topextra->backingStore.destroy();
1799 delete extra->topextra->icon;
1800 delete extra->topextra->iconPixmap;
1801 #if defined(Q_WS_QWS) && !defined(QT_NO_QWS_MANAGER)
1802 delete extra->topextra->qwsManager;
1804 delete extra->topextra->windowSurface;
1805 delete extra->topextra;
1808 // extra->xic destroyed in QWidget::destroy()
1814 Returns true if there are widgets above this which overlap with
1815 \a rect, which is in parent's coordinate system (same as crect).
1818 bool QWidgetPrivate::isOverlapped(const QRect &rect) const
1822 const QWidget *w = q;
1827 QWidgetPrivate *pd = w->parentWidget()->d_func();
1829 for (int i = 0; i < pd->children.size(); ++i) {
1830 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
1831 if (!sibling || !sibling->isVisible() || sibling->isWindow())
1834 above = (sibling == w);
1838 if (qRectIntersects(sibling->d_func()->effectiveRectFor(sibling->data->crect), r)) {
1839 const QWExtra *siblingExtra = sibling->d_func()->extra;
1840 if (siblingExtra && siblingExtra->hasMask && !sibling->d_func()->graphicsEffect
1841 && !siblingExtra->mask.translated(sibling->data->crect.topLeft()).intersects(r)) {
1847 w = w->parentWidget();
1848 r.translate(pd->data.crect.topLeft());
1853 void QWidgetPrivate::syncBackingStore()
1855 if (paintOnScreen()) {
1858 } else if (QWidgetBackingStore *bs = maybeBackingStore()) {
1863 void QWidgetPrivate::syncBackingStore(const QRegion ®ion)
1865 if (paintOnScreen())
1866 repaint_sys(region);
1867 else if (QWidgetBackingStore *bs = maybeBackingStore()) {
1868 bs->sync(q_func(), region);
1872 void QWidgetPrivate::setUpdatesEnabled_helper(bool enable)
1876 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->updatesEnabled())
1877 return; // nothing we can do
1879 if (enable != q->testAttribute(Qt::WA_UpdatesDisabled))
1880 return; // nothing to do
1882 q->setAttribute(Qt::WA_UpdatesDisabled, !enable);
1886 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceUpdatesDisabled : Qt::WA_UpdatesDisabled;
1887 for (int i = 0; i < children.size(); ++i) {
1888 QWidget *w = qobject_cast<QWidget *>(children.at(i));
1889 if (w && !w->isWindow() && !w->testAttribute(attribute))
1890 w->d_func()->setUpdatesEnabled_helper(enable);
1897 Propagate this widget's palette to all children, except style sheet
1898 widgets, and windows that don't enable window propagation (palettes don't
1899 normally propagate to windows).
1901 void QWidgetPrivate::propagatePaletteChange()
1904 // Propagate a new inherited mask to all children.
1905 #ifndef QT_NO_GRAPHICSVIEW
1906 if (!q->parentWidget() && extra && extra->proxyWidget) {
1907 QGraphicsProxyWidget *p = extra->proxyWidget;
1908 inheritedPaletteResolveMask = p->d_func()->inheritedPaletteResolveMask | p->palette().resolve();
1910 #endif //QT_NO_GRAPHICSVIEW
1911 if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) {
1912 inheritedPaletteResolveMask = 0;
1914 int mask = data.pal.resolve() | inheritedPaletteResolveMask;
1916 QEvent pc(QEvent::PaletteChange);
1917 QApplication::sendEvent(q, &pc);
1918 for (int i = 0; i < children.size(); ++i) {
1919 QWidget *w = qobject_cast<QWidget*>(children.at(i));
1920 if (w && !w->testAttribute(Qt::WA_StyleSheet)
1921 && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) {
1922 QWidgetPrivate *wd = w->d_func();
1923 wd->inheritedPaletteResolveMask = mask;
1924 wd->resolvePalette();
1927 #if defined(QT3_SUPPORT)
1928 q->paletteChange(q->palette()); // compatibility
1933 Returns the widget's clipping rectangle.
1935 QRect QWidgetPrivate::clipRect() const
1938 const QWidget * w = q;
1939 if (!w->isVisible())
1941 QRect r = effectiveRectFor(q->rect());
1947 && w->parentWidget()) {
1950 w = w->parentWidget();
1951 r &= QRect(ox, oy, w->width(), w->height());
1957 Returns the widget's clipping region (without siblings).
1959 QRegion QWidgetPrivate::clipRegion() const
1962 if (!q->isVisible())
1964 QRegion r(q->rect());
1965 const QWidget * w = q;
1966 const QWidget *ignoreUpTo;
1972 && w->parentWidget()) {
1976 w = w->parentWidget();
1977 r &= QRegion(ox, oy, w->width(), w->height());
1980 while(w->d_func()->children.at(i++) != static_cast<const QObject *>(ignoreUpTo))
1982 for ( ; i < w->d_func()->children.size(); ++i) {
1983 if(QWidget *sibling = qobject_cast<QWidget *>(w->d_func()->children.at(i))) {
1984 if(sibling->isVisible() && !sibling->isWindow()) {
1985 QRect siblingRect(ox+sibling->x(), oy+sibling->y(),
1986 sibling->width(), sibling->height());
1987 if (qRectIntersects(siblingRect, q->rect()))
1988 r -= QRegion(siblingRect);
1996 #ifndef QT_NO_GRAPHICSEFFECT
1997 void QWidgetPrivate::invalidateGraphicsEffectsRecursively()
2002 if (w->graphicsEffect()) {
2003 QWidgetEffectSourcePrivate *sourced =
2004 static_cast<QWidgetEffectSourcePrivate *>(w->graphicsEffect()->source()->d_func());
2005 if (!sourced->updateDueToGraphicsEffect)
2006 w->graphicsEffect()->source()->d_func()->invalidateCache();
2008 w = w->parentWidget();
2011 #endif //QT_NO_GRAPHICSEFFECT
2013 void QWidgetPrivate::setDirtyOpaqueRegion()
2017 dirtyOpaqueChildren = true;
2019 #ifndef QT_NO_GRAPHICSEFFECT
2020 invalidateGraphicsEffectsRecursively();
2021 #endif //QT_NO_GRAPHICSEFFECT
2026 QWidget *parent = q->parentWidget();
2030 // TODO: instead of setting dirtyflag, manipulate the dirtyregion directly?
2031 QWidgetPrivate *pd = parent->d_func();
2032 if (!pd->dirtyOpaqueChildren)
2033 pd->setDirtyOpaqueRegion();
2036 const QRegion &QWidgetPrivate::getOpaqueChildren() const
2038 if (!dirtyOpaqueChildren)
2039 return opaqueChildren;
2041 QWidgetPrivate *that = const_cast<QWidgetPrivate*>(this);
2042 that->opaqueChildren = QRegion();
2044 for (int i = 0; i < children.size(); ++i) {
2045 QWidget *child = qobject_cast<QWidget *>(children.at(i));
2046 if (!child || !child->isVisible() || child->isWindow())
2049 const QPoint offset = child->geometry().topLeft();
2050 QWidgetPrivate *childd = child->d_func();
2051 QRegion r = childd->isOpaque ? child->rect() : childd->getOpaqueChildren();
2052 if (childd->extra && childd->extra->hasMask)
2053 r &= childd->extra->mask;
2056 r.translate(offset);
2057 that->opaqueChildren += r;
2060 that->opaqueChildren &= q_func()->rect();
2061 that->dirtyOpaqueChildren = false;
2063 return that->opaqueChildren;
2066 void QWidgetPrivate::subtractOpaqueChildren(QRegion &source, const QRect &clipRect) const
2068 if (children.isEmpty() || clipRect.isEmpty())
2071 const QRegion &r = getOpaqueChildren();
2073 source -= (r & clipRect);
2076 //subtract any relatives that are higher up than me --- this is too expensive !!!
2077 void QWidgetPrivate::subtractOpaqueSiblings(QRegion &sourceRegion, bool *hasDirtySiblingsAbove,
2078 bool alsoNonOpaque) const
2081 static int disableSubtractOpaqueSiblings = qgetenv("QT_NO_SUBTRACTOPAQUESIBLINGS").toInt();
2082 if (disableSubtractOpaqueSiblings || q->isWindow())
2085 QRect clipBoundingRect;
2086 bool dirtyClipBoundingRect = true;
2089 bool dirtyParentClip = true;
2091 QPoint parentOffset = data.crect.topLeft();
2093 const QWidget *w = q;
2098 QWidgetPrivate *pd = w->parentWidget()->d_func();
2099 const int myIndex = pd->children.indexOf(const_cast<QWidget *>(w));
2100 const QRect widgetGeometry = w->d_func()->effectiveRectFor(w->data->crect);
2101 for (int i = myIndex + 1; i < pd->children.size(); ++i) {
2102 QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i));
2103 if (!sibling || !sibling->isVisible() || sibling->isWindow())
2106 const QRect siblingGeometry = sibling->d_func()->effectiveRectFor(sibling->data->crect);
2107 if (!qRectIntersects(siblingGeometry, widgetGeometry))
2110 if (dirtyClipBoundingRect) {
2111 clipBoundingRect = sourceRegion.boundingRect();
2112 dirtyClipBoundingRect = false;
2115 if (!qRectIntersects(siblingGeometry, clipBoundingRect.translated(parentOffset)))
2118 if (dirtyParentClip) {
2119 parentClip = sourceRegion.translated(parentOffset);
2120 dirtyParentClip = false;
2123 const QPoint siblingPos(sibling->data->crect.topLeft());
2124 const QRect siblingClipRect(sibling->d_func()->clipRect());
2125 QRegion siblingDirty(parentClip);
2126 siblingDirty &= (siblingClipRect.translated(siblingPos));
2127 const bool hasMask = sibling->d_func()->extra && sibling->d_func()->extra->hasMask
2128 && !sibling->d_func()->graphicsEffect;
2130 siblingDirty &= sibling->d_func()->extra->mask.translated(siblingPos);
2131 if (siblingDirty.isEmpty())
2134 if (sibling->d_func()->isOpaque || alsoNonOpaque) {
2136 siblingDirty.translate(-parentOffset);
2137 sourceRegion -= siblingDirty;
2139 sourceRegion -= siblingGeometry.translated(-parentOffset);
2142 if (hasDirtySiblingsAbove)
2143 *hasDirtySiblingsAbove = true;
2144 if (sibling->d_func()->children.isEmpty())
2146 QRegion opaqueSiblingChildren(sibling->d_func()->getOpaqueChildren());
2147 opaqueSiblingChildren.translate(-parentOffset + siblingPos);
2148 sourceRegion -= opaqueSiblingChildren;
2150 if (sourceRegion.isEmpty())
2153 dirtyClipBoundingRect = true;
2154 dirtyParentClip = true;
2157 w = w->parentWidget();
2158 parentOffset += pd->data.crect.topLeft();
2159 dirtyParentClip = true;
2163 void QWidgetPrivate::clipToEffectiveMask(QRegion ®ion) const
2167 const QWidget *w = q;
2170 #ifndef QT_NO_GRAPHICSEFFECT
2171 if (graphicsEffect) {
2172 w = q->parentWidget();
2173 offset -= data.crect.topLeft();
2175 #endif //QT_NO_GRAPHICSEFFECT
2178 const QWidgetPrivate *wd = w->d_func();
2179 if (wd->extra && wd->extra->hasMask)
2180 region &= (w != q) ? wd->extra->mask.translated(offset) : wd->extra->mask;
2183 offset -= wd->data.crect.topLeft();
2184 w = w->parentWidget();
2188 bool QWidgetPrivate::paintOnScreen() const
2190 #if defined(Q_WS_QWS)
2192 #elif defined(QT_NO_BACKINGSTORE)
2196 if (q->testAttribute(Qt::WA_PaintOnScreen)
2197 || (!q->isWindow() && q->window()->testAttribute(Qt::WA_PaintOnScreen))) {
2201 return !qt_enable_backingstore;
2205 void QWidgetPrivate::updateIsOpaque()
2207 // hw: todo: only needed if opacity actually changed
2208 setDirtyOpaqueRegion();
2210 #ifndef QT_NO_GRAPHICSEFFECT
2211 if (graphicsEffect) {
2212 // ### We should probably add QGraphicsEffect::isOpaque at some point.
2216 #endif //QT_NO_GRAPHICSEFFECT
2220 if (q->testAttribute(Qt::WA_X11OpenGLOverlay)) {
2227 if (q->windowType() == Qt::Dialog && q->testAttribute(Qt::WA_TranslucentBackground)
2228 && S60->avkonComponentsSupportTransparency) {
2234 if (q->testAttribute(Qt::WA_OpaquePaintEvent) || q->testAttribute(Qt::WA_PaintOnScreen)) {
2239 const QPalette &pal = q->palette();
2241 if (q->autoFillBackground()) {
2242 const QBrush &autoFillBrush = pal.brush(q->backgroundRole());
2243 if (autoFillBrush.style() != Qt::NoBrush && autoFillBrush.isOpaque()) {
2249 if (q->isWindow() && !q->testAttribute(Qt::WA_NoSystemBackground)) {
2250 const QBrush &windowBrush = q->palette().brush(QPalette::Window);
2251 if (windowBrush.style() != Qt::NoBrush && windowBrush.isOpaque()) {
2259 void QWidgetPrivate::setOpaque(bool opaque)
2261 if (isOpaque == opaque)
2265 macUpdateIsOpaque();
2268 x11UpdateIsOpaque();
2271 winUpdateIsOpaque();
2274 s60UpdateIsOpaque();
2278 void QWidgetPrivate::updateIsTranslucent()
2281 macUpdateIsOpaque();
2284 x11UpdateIsOpaque();
2287 winUpdateIsOpaque();
2290 s60UpdateIsOpaque();
2295 \fn void QPixmap::fill(const QWidget *widget, const QPoint &offset)
2297 Fills the pixmap with the \a widget's background color or pixmap
2298 according to the given offset.
2300 The QPoint \a offset defines a point in widget coordinates to
2301 which the pixmap's top-left pixel will be mapped to. This is only
2302 significant if the widget has a background pixmap; otherwise the
2303 pixmap will simply be filled with the background color of the
2307 void QPixmap::fill( const QWidget *widget, const QPoint &off )
2311 widget->d_func()->paintBackground(&p, QRect(off, size()));
2314 static inline void fillRegion(QPainter *painter, const QRegion &rgn, const QBrush &brush)
2318 if (brush.style() == Qt::TexturePattern) {
2320 // Optimize pattern filling on mac by using HITheme directly
2321 // when filling with the standard widget background.
2322 // Defined in qmacstyle_mac.cpp
2323 extern void qt_mac_fill_background(QPainter *painter, const QRegion &rgn, const QBrush &brush);
2324 qt_mac_fill_background(painter, rgn, brush);
2326 #if !defined(QT_NO_STYLE_S60)
2327 // Defined in qs60style.cpp
2328 extern bool qt_s60_fill_background(QPainter *painter, const QRegion &rgn, const QBrush &brush);
2329 if (!qt_s60_fill_background(painter, rgn, brush))
2330 #endif // !defined(QT_NO_STYLE_S60)
2332 const QRect rect(rgn.boundingRect());
2333 painter->setClipRegion(rgn);
2334 painter->drawTiledPixmap(rect, brush.texture(), rect.topLeft());
2338 } else if (brush.gradient()
2339 && brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode) {
2341 painter->setClipRegion(rgn);
2342 painter->fillRect(0, 0, painter->device()->width(), painter->device()->height(), brush);
2345 const QVector<QRect> &rects = rgn.rects();
2346 for (int i = 0; i < rects.size(); ++i)
2347 painter->fillRect(rects.at(i), brush);
2351 void QWidgetPrivate::paintBackground(QPainter *painter, const QRegion &rgn, int flags) const
2355 #ifndef QT_NO_SCROLLAREA
2356 bool resetBrushOrigin = false;
2357 QPointF oldBrushOrigin;
2358 //If we are painting the viewport of a scrollarea, we must apply an offset to the brush in case we are drawing a texture
2359 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea *>(parent);
2360 if (scrollArea && scrollArea->viewport() == q) {
2361 QObjectData *scrollPrivate = static_cast<QWidget *>(scrollArea)->d_ptr.data();
2362 QAbstractScrollAreaPrivate *priv = static_cast<QAbstractScrollAreaPrivate *>(scrollPrivate);
2363 oldBrushOrigin = painter->brushOrigin();
2364 resetBrushOrigin = true;
2365 painter->setBrushOrigin(-priv->contentsOffset());
2368 #endif // QT_NO_SCROLLAREA
2370 const QBrush autoFillBrush = q->palette().brush(q->backgroundRole());
2372 if ((flags & DrawAsRoot) && !(q->autoFillBackground() && autoFillBrush.isOpaque())) {
2373 const QBrush bg = q->palette().brush(QPalette::Window);
2375 if (!(flags & DontSetCompositionMode) && painter->paintEngine()->hasFeature(QPaintEngine::PorterDuff))
2376 painter->setCompositionMode(QPainter::CompositionMode_Source); //copy alpha straight in
2378 fillRegion(painter, rgn, bg);
2381 if (q->autoFillBackground())
2382 fillRegion(painter, rgn, autoFillBrush);
2384 if (q->testAttribute(Qt::WA_StyledBackground)) {
2385 painter->setClipRegion(rgn);
2388 q->style()->drawPrimitive(QStyle::PE_Widget, &opt, painter, q);
2391 #ifndef QT_NO_SCROLLAREA
2392 if (resetBrushOrigin)
2393 painter->setBrushOrigin(oldBrushOrigin);
2394 #endif // QT_NO_SCROLLAREA
2399 This function is called when a widget is hidden or destroyed.
2400 It resets some application global pointers that should only refer active,
2405 extern QPointer<QWidget> qt_button_down;
2407 extern QWidget *qt_button_down;
2410 void QWidgetPrivate::deactivateWidgetCleanup()
2413 // If this was the active application window, reset it
2414 if (QApplication::activeWindow() == q)
2415 QApplication::setActiveWindow(0);
2416 // If the is the active mouse press widget, reset it
2417 if (q == qt_button_down)
2423 Returns a pointer to the widget with window identifer/handle \a
2426 The window identifier type depends on the underlying window
2427 system, see \c qwindowdefs.h for the actual definition. If there
2428 is no widget with this identifier, 0 is returned.
2431 QWidget *QWidget::find(WId id)
2433 return QWidgetPrivate::mapper ? QWidgetPrivate::mapper->value(id, 0) : 0;
2439 \fn WId QWidget::internalWinId() const
2441 Returns the window system identifier of the widget, or 0 if the widget is not created yet.
2446 \fn WId QWidget::winId() const
2448 Returns the window system identifier of the widget.
2450 Portable in principle, but if you use it you are probably about to
2451 do something non-portable. Be careful.
2453 If a widget is non-native (alien) and winId() is invoked on it, that widget
2454 will be provided a native handle.
2456 On Mac OS X, the type returned depends on which framework Qt was linked
2457 against. If Qt is using Carbon, the {WId} is actually an HIViewRef. If Qt
2458 is using Cocoa, {WId} is a pointer to an NSView.
2460 This value may change at run-time. An event with type QEvent::WinIdChange
2461 will be sent to the widget following a change in window system identifier.
2465 WId QWidget::winId() const
2467 if (!testAttribute(Qt::WA_WState_Created) || !internalWinId()) {
2469 qDebug() << "QWidget::winId: creating native window for" << this;
2471 QWidget *that = const_cast<QWidget*>(this);
2473 that->setAttribute(Qt::WA_NativeWindow);
2475 that->d_func()->createWinId();
2476 return that->data->winid;
2482 void QWidgetPrivate::createWinId(WId winid)
2487 qDebug() << "QWidgetPrivate::createWinId for" << q << winid;
2489 const bool forceNativeWindow = q->testAttribute(Qt::WA_NativeWindow);
2490 if (!q->testAttribute(Qt::WA_WState_Created) || (forceNativeWindow && !q->internalWinId())) {
2492 if (!q->isWindow()) {
2493 QWidget *parent = q->parentWidget();
2494 QWidgetPrivate *pd = parent->d_func();
2495 if (forceNativeWindow && !q->testAttribute(Qt::WA_DontCreateNativeAncestors))
2496 parent->setAttribute(Qt::WA_NativeWindow);
2497 if (!parent->internalWinId()) {
2501 for (int i = 0; i < pd->children.size(); ++i) {
2502 QWidget *w = qobject_cast<QWidget *>(pd->children.at(i));
2503 if (w && !w->isWindow() && (!w->testAttribute(Qt::WA_WState_Created)
2504 || (!w->internalWinId() && w->testAttribute(Qt::WA_NativeWindow)))) {
2509 // if the window has already been created, we
2510 // need to raise it to its proper stacking position
2530 Ensures that the widget has a window system identifier, i.e. that it is known to the windowing system.
2534 void QWidget::createWinId()
2538 qDebug() << "QWidget::createWinId" << this;
2540 // qWarning("QWidget::createWinId is obsolete, please fix your code.");
2547 Returns the effective window system identifier of the widget, i.e. the
2548 native parent's window system identifier.
2550 If the widget is native, this function returns the native widget ID.
2551 Otherwise, the window ID of the first native parent widget, i.e., the
2552 top-level widget that contains this widget, is returned.
2554 \note We recommend that you do not store this value as it is likely to
2557 \sa nativeParentWidget()
2559 WId QWidget::effectiveWinId() const
2561 WId id = internalWinId();
2562 if (id || !testAttribute(Qt::WA_WState_Created))
2564 QWidget *realParent = nativeParentWidget();
2565 Q_ASSERT(realParent);
2566 Q_ASSERT(realParent->internalWinId());
2567 return realParent->internalWinId();
2570 #ifndef QT_NO_STYLE_STYLESHEET
2573 \property QWidget::styleSheet
2574 \brief the widget's style sheet
2577 The style sheet contains a textual description of customizations to the
2578 widget's style, as described in the \l{Qt Style Sheets} document.
2580 Since Qt 4.5, Qt style sheets fully supports Mac OS X.
2582 \warning Qt style sheets are currently not supported for custom QStyle
2583 subclasses. We plan to address this in some future release.
2585 \sa setStyle(), QApplication::styleSheet, {Qt Style Sheets}
2587 QString QWidget::styleSheet() const
2592 return d->extra->styleSheet;
2595 void QWidget::setStyleSheet(const QString& styleSheet)
2600 QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(d->extra->style);
2601 d->extra->styleSheet = styleSheet;
2602 if (styleSheet.isEmpty()) { // stylesheet removed
2610 if (proxy) { // style sheet update
2611 proxy->repolish(this);
2615 if (testAttribute(Qt::WA_SetStyle)) {
2616 d->setStyle_helper(new QStyleSheetStyle(d->extra->style), true);
2618 d->setStyle_helper(new QStyleSheetStyle(0), true);
2622 #endif // QT_NO_STYLE_STYLESHEET
2625 \sa QWidget::setStyle(), QApplication::setStyle(), QApplication::style()
2628 QStyle *QWidget::style() const
2632 if (d->extra && d->extra->style)
2633 return d->extra->style;
2634 return QApplication::style();
2638 Sets the widget's GUI style to \a style. The ownership of the style
2639 object is not transferred.
2641 If no style is set, the widget uses the application's style,
2642 QApplication::style() instead.
2644 Setting a widget's style has no effect on existing or future child
2647 \warning This function is particularly useful for demonstration
2648 purposes, where you want to show Qt's styling capabilities. Real
2649 applications should avoid it and use one consistent GUI style
2652 \warning Qt style sheets are currently not supported for custom QStyle
2653 subclasses. We plan to address this in some future release.
2655 \sa style(), QStyle, QApplication::style(), QApplication::setStyle()
2658 void QWidget::setStyle(QStyle *style)
2661 setAttribute(Qt::WA_SetStyle, style != 0);
2663 #ifndef QT_NO_STYLE_STYLESHEET
2664 if (QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(style)) {
2665 //if for some reason someone try to set a QStyleSheetStyle, ref it
2666 //(this may happen for exemple in QButtonDialogBox which propagates its style)
2668 d->setStyle_helper(style, false);
2669 } else if (qobject_cast<QStyleSheetStyle *>(d->extra->style) || !qApp->styleSheet().isEmpty()) {
2670 // if we have an application stylesheet or have a proxy already, propagate
2671 d->setStyle_helper(new QStyleSheetStyle(style), true);
2674 d->setStyle_helper(style, false);
2677 void QWidgetPrivate::setStyle_helper(QStyle *newStyle, bool propagate, bool
2684 QStyle *oldStyle = q->style();
2685 #ifndef QT_NO_STYLE_STYLESHEET
2686 QWeakPointer<QStyle> origStyle;
2690 // the metalhack boolean allows Qt/Mac to do a proper re-polish depending
2691 // on how the Qt::WA_MacBrushedMetal attribute is set. It is only ever
2692 // set when changing that attribute and passes the widget's CURRENT style.
2693 // therefore no need to do a reassignment.
2699 #ifndef QT_NO_STYLE_STYLESHEET
2700 origStyle = extra->style.data();
2702 extra->style = newStyle;
2706 if (q->windowType() != Qt::Desktop) {
2708 oldStyle->unpolish(q);
2711 macUpdateMetalAttribute();
2713 q->style()->polish(q);
2715 } else if (metalHack) {
2716 macUpdateMetalAttribute();
2722 for (int i = 0; i < children.size(); ++i) {
2723 QWidget *c = qobject_cast<QWidget*>(children.at(i));
2725 c->d_func()->inheritStyle();
2729 #ifndef QT_NO_STYLE_STYLESHEET
2730 if (!qobject_cast<QStyleSheetStyle*>(newStyle)) {
2731 if (const QStyleSheetStyle* cssStyle = qobject_cast<QStyleSheetStyle*>(origStyle.data())) {
2732 cssStyle->clearWidgetFont(q);
2737 QEvent e(QEvent::StyleChange);
2738 QApplication::sendEvent(q, &e);
2740 q->styleChange(*oldStyle);
2743 #ifndef QT_NO_STYLE_STYLESHEET
2744 // dereference the old stylesheet style
2745 if (QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(origStyle.data()))
2750 // Inherits style from the current parent and propagates it as necessary
2751 void QWidgetPrivate::inheritStyle()
2753 #ifndef QT_NO_STYLE_STYLESHEET
2756 QStyleSheetStyle *proxy = extra ? qobject_cast<QStyleSheetStyle *>(extra->style) : 0;
2758 if (!q->styleSheet().isEmpty()) {
2764 QStyle *origStyle = proxy ? proxy->base : (extra ? (QStyle*)extra->style : 0);
2765 QWidget *parent = q->parentWidget();
2766 QStyle *parentStyle = (parent && parent->d_func()->extra) ? (QStyle*)parent->d_func()->extra->style : 0;
2767 // If we have stylesheet on app or parent has stylesheet style, we need
2768 // to be running a proxy
2769 if (!qApp->styleSheet().isEmpty() || qobject_cast<QStyleSheetStyle *>(parentStyle)) {
2770 QStyle *newStyle = parentStyle;
2771 if (q->testAttribute(Qt::WA_SetStyle))
2772 newStyle = new QStyleSheetStyle(origStyle);
2773 else if (QStyleSheetStyle *newProxy = qobject_cast<QStyleSheetStyle *>(parentStyle))
2776 setStyle_helper(newStyle, true);
2780 // So, we have no stylesheet on parent/app and we have an empty stylesheet
2781 // we just need our original style back
2782 if (origStyle == (extra ? (QStyle*)extra->style : 0)) // is it any different?
2785 // We could have inherited the proxy from our parent (which has a custom style)
2786 // In such a case we need to start following the application style (i.e revert
2787 // the propagation behavior of QStyleSheetStyle)
2788 if (!q->testAttribute(Qt::WA_SetStyle))
2791 setStyle_helper(origStyle, true);
2792 #endif // QT_NO_STYLE_STYLESHEET
2799 Sets the widget's GUI style to \a style using the QStyleFactory.
2801 QStyle* QWidget::setStyle(const QString &style)
2803 QStyle *s = QStyleFactory::create(style);
2810 \fn bool QWidget::isWindow() const
2812 Returns true if the widget is an independent window, otherwise
2815 A window is a widget that isn't visually the child of any other
2816 widget and that usually has a frame and a
2817 \l{QWidget::setWindowTitle()}{window title}.
2819 A window can have a \l{QWidget::parentWidget()}{parent widget}.
2820 It will then be grouped with its parent and deleted when the
2821 parent is deleted, minimized when the parent is minimized etc. If
2822 supported by the window manager, it will also have a common
2823 taskbar entry with its parent.
2825 QDialog and QMainWindow widgets are by default windows, even if a
2826 parent widget is specified in the constructor. This behavior is
2827 specified by the Qt::Window flag.
2829 \sa window(), isModal(), parentWidget()
2833 \property QWidget::modal
2834 \brief whether the widget is a modal widget
2836 This property only makes sense for windows. A modal widget
2837 prevents widgets in all other windows from getting any input.
2839 By default, this property is false.
2841 \sa isWindow(), windowModality, QDialog
2845 \property QWidget::windowModality
2846 \brief which windows are blocked by the modal widget
2849 This property only makes sense for windows. A modal widget
2850 prevents widgets in other windows from getting input. The value of
2851 this property controls which windows are blocked when the widget
2852 is visible. Changing this property while the window is visible has
2853 no effect; you must hide() the widget first, then show() it again.
2855 By default, this property is Qt::NonModal.
2857 \sa isWindow(), QWidget::modal, QDialog
2860 Qt::WindowModality QWidget::windowModality() const
2862 return static_cast<Qt::WindowModality>(data->window_modality);
2865 void QWidget::setWindowModality(Qt::WindowModality windowModality)
2867 data->window_modality = windowModality;
2868 // setModal_sys() will be called by setAttribute()
2869 setAttribute(Qt::WA_ShowModal, (data->window_modality != Qt::NonModal));
2870 setAttribute(Qt::WA_SetWindowModality, true);
2874 \fn bool QWidget::underMouse() const
2876 Returns true if the widget is under the mouse cursor; otherwise
2879 This value is not updated properly during drag and drop
2882 \sa enterEvent(), leaveEvent()
2886 \property QWidget::minimized
2887 \brief whether this widget is minimized (iconified)
2889 This property is only relevant for windows.
2891 By default, this property is false.
2893 \sa showMinimized(), visible, show(), hide(), showNormal(), maximized
2895 bool QWidget::isMinimized() const
2896 { return data->window_state & Qt::WindowMinimized; }
2899 Shows the widget minimized, as an icon.
2901 Calling this function only affects \l{isWindow()}{windows}.
2903 \sa showNormal(), showMaximized(), show(), hide(), isVisible(),
2906 void QWidget::showMinimized()
2908 bool isMin = isMinimized();
2909 if (isMin && isVisible())
2915 QApplication::sendPostedEvents(parent(), QEvent::ChildInserted);
2919 setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
2924 \property QWidget::maximized
2925 \brief whether this widget is maximized
2927 This property is only relevant for windows.
2929 \note Due to limitations on some window systems, this does not always
2930 report the expected results (e.g., if the user on X11 maximizes the
2931 window via the window manager, Qt has no way of distinguishing this
2932 from any other resize). This is expected to improve as window manager
2935 By default, this property is false.
2937 \sa windowState(), showMaximized(), visible, show(), hide(), showNormal(), minimized
2939 bool QWidget::isMaximized() const
2940 { return data->window_state & Qt::WindowMaximized; }
2945 Returns the current window state. The window state is a OR'ed
2946 combination of Qt::WindowState: Qt::WindowMinimized,
2947 Qt::WindowMaximized, Qt::WindowFullScreen, and Qt::WindowActive.
2949 \sa Qt::WindowState setWindowState()
2951 Qt::WindowStates QWidget::windowState() const
2953 return Qt::WindowStates(data->window_state);
2958 The function sets the window state on child widgets similar to
2959 setWindowState(). The difference is that the window state changed
2960 event has the isOverride() flag set. It exists mainly to keep
2961 Q3Workspace working.
2963 void QWidget::overrideWindowState(Qt::WindowStates newstate)
2965 QWindowStateChangeEvent e(Qt::WindowStates(data->window_state), true);
2966 data->window_state = newstate;
2967 QApplication::sendEvent(this, &e);
2971 \fn void QWidget::setWindowState(Qt::WindowStates windowState)
2973 Sets the window state to \a windowState. The window state is a OR'ed
2974 combination of Qt::WindowState: Qt::WindowMinimized,
2975 Qt::WindowMaximized, Qt::WindowFullScreen, and Qt::WindowActive.
2977 If the window is not visible (i.e. isVisible() returns false), the
2978 window state will take effect when show() is called. For visible
2979 windows, the change is immediate. For example, to toggle between
2980 full-screen and normal mode, use the following code:
2982 \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 0
2984 In order to restore and activate a minimized window (while
2985 preserving its maximized and/or full-screen state), use the following:
2987 \snippet doc/src/snippets/code/src_gui_kernel_qwidget.cpp 1
2989 Calling this function will hide the widget. You must call show() to make
2990 the widget visible again.
2992 \note On some window systems Qt::WindowActive is not immediate, and may be
2993 ignored in certain cases.
2995 When the window state changes, the widget receives a changeEvent()
2996 of type QEvent::WindowStateChange.
2998 \sa Qt::WindowState windowState()
3002 \property QWidget::fullScreen
3003 \brief whether the widget is shown in full screen mode
3005 A widget in full screen mode occupies the whole screen area and does not
3006 display window decorations, such as a title bar.
3008 By default, this property is false.
3010 \sa windowState(), minimized, maximized
3012 bool QWidget::isFullScreen() const
3013 { return data->window_state & Qt::WindowFullScreen; }
3016 Shows the widget in full-screen mode.
3018 Calling this function only affects \l{isWindow()}{windows}.
3020 To return from full-screen mode, call showNormal().
3022 Full-screen mode works fine under Windows, but has certain
3023 problems under X. These problems are due to limitations of the
3024 ICCCM protocol that specifies the communication between X11
3025 clients and the window manager. ICCCM simply does not understand
3026 the concept of non-decorated full-screen windows. Therefore, the
3027 best we can do is to request a borderless window and place and
3028 resize it to fill the entire screen. Depending on the window
3029 manager, this may or may not work. The borderless window is
3030 requested using MOTIF hints, which are at least partially
3031 supported by virtually all modern window managers.
3033 An alternative would be to bypass the window manager entirely and
3034 create a window with the Qt::X11BypassWindowManagerHint flag. This
3035 has other severe problems though, like totally broken keyboard focus
3036 and very strange effects on desktop changes or when the user raises
3039 X11 window managers that follow modern post-ICCCM specifications
3040 support full-screen mode properly.
3042 \sa showNormal(), showMaximized(), show(), hide(), isVisible()
3044 void QWidget::showFullScreen()
3047 // If the unified toolbar is enabled, we have to disable it before going fullscreen.
3048 QMainWindow *mainWindow = qobject_cast<QMainWindow*>(this);
3049 if (mainWindow && mainWindow->unifiedTitleAndToolBarOnMac()) {
3050 mainWindow->setUnifiedTitleAndToolBarOnMac(false);
3051 QMainWindowLayout *mainLayout = qobject_cast<QMainWindowLayout*>(mainWindow->layout());
3052 mainLayout->activateUnifiedToolbarAfterFullScreen = true;
3058 QApplication::sendPostedEvents(parent(), QEvent::ChildInserted);
3061 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
3062 | Qt::WindowFullScreen);
3068 Shows the widget maximized.
3070 Calling this function only affects \l{isWindow()}{windows}.
3072 On X11, this function may not work properly with certain window
3073 managers. See the \l{Window Geometry} documentation for an explanation.
3075 \sa setWindowState(), showNormal(), showMinimized(), show(), hide(), isVisible()
3077 void QWidget::showMaximized()
3082 QApplication::sendPostedEvents(parent(), QEvent::ChildInserted);
3085 setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
3086 | Qt::WindowMaximized);
3088 // If the unified toolbar was enabled before going fullscreen, we have to enable it back.
3089 QMainWindow *mainWindow = qobject_cast<QMainWindow*>(this);
3092 QMainWindowLayout *mainLayout = qobject_cast<QMainWindowLayout*>(mainWindow->layout());
3093 if (mainLayout->activateUnifiedToolbarAfterFullScreen) {
3094 mainWindow->setUnifiedTitleAndToolBarOnMac(true);
3095 mainLayout->activateUnifiedToolbarAfterFullScreen = false;
3103 Restores the widget after it has been maximized or minimized.
3105 Calling this function only affects \l{isWindow()}{windows}.
3107 \sa setWindowState(), showMinimized(), showMaximized(), show(), hide(), isVisible()
3109 void QWidget::showNormal()
3114 QApplication::sendPostedEvents(parent(), QEvent::ChildInserted);
3117 setWindowState(windowState() & ~(Qt::WindowMinimized
3118 | Qt::WindowMaximized
3119 | Qt::WindowFullScreen));
3121 // If the unified toolbar was enabled before going fullscreen, we have to enable it back.
3122 QMainWindow *mainWindow = qobject_cast<QMainWindow*>(this);
3125 QMainWindowLayout *mainLayout = qobject_cast<QMainWindowLayout*>(mainWindow->layout());
3126 if (mainLayout->activateUnifiedToolbarAfterFullScreen) {
3127 mainWindow->setUnifiedTitleAndToolBarOnMac(true);
3128 mainLayout->activateUnifiedToolbarAfterFullScreen = false;
3136 Returns true if this widget would become enabled if \a ancestor is
3137 enabled; otherwise returns false.
3141 This is the case if neither the widget itself nor every parent up
3142 to but excluding \a ancestor has been explicitly disabled.
3144 isEnabledTo(0) is equivalent to isEnabled().
3146 \sa setEnabled() enabled
3149 bool QWidget::isEnabledTo(QWidget* ancestor) const
3151 const QWidget * w = this;
3152 while (!w->testAttribute(Qt::WA_ForceDisabled)
3154 && w->parentWidget()
3155 && w->parentWidget() != ancestor)
3156 w = w->parentWidget();
3157 return !w->testAttribute(Qt::WA_ForceDisabled);
3160 #ifndef QT_NO_ACTION
3162 Appends the action \a action to this widget's list of actions.
3164 All QWidgets have a list of \l{QAction}s, however they can be
3165 represented graphically in many different ways. The default use of
3166 the QAction list (as returned by actions()) is to create a context
3169 A QWidget should only have one of each action and adding an action
3170 it already has will not cause the same action to be in the widget twice.
3172 The ownership of \a action is not transferred to this QWidget.
3174 \sa removeAction(), insertAction(), actions(), QMenu
3176 void QWidget::addAction(QAction *action)
3178 insertAction(0, action);
3182 Appends the actions \a actions to this widget's list of actions.
3184 \sa removeAction(), QMenu, addAction()
3186 void QWidget::addActions(QList<QAction*> actions)
3188 for(int i = 0; i < actions.count(); i++)
3189 insertAction(0, actions.at(i));
3193 Inserts the action \a action to this widget's list of actions,
3194 before the action \a before. It appends the action if \a before is 0 or
3195 \a before is not a valid action for this widget.
3197 A QWidget should only have one of each action.
3199 \sa removeAction(), addAction(), QMenu, contextMenuPolicy, actions()
3201 void QWidget::insertAction(QAction *before, QAction *action)
3204 qWarning("QWidget::insertAction: Attempt to insert null action");
3209 if(d->actions.contains(action))
3210 removeAction(action);
3212 int pos = d->actions.indexOf(before);
3215 pos = d->actions.size();
3217 d->actions.insert(pos, action);
3219 QActionPrivate *apriv = action->d_func();
3220 apriv->widgets.append(this);
3222 QActionEvent e(QEvent::ActionAdded, action, before);
3223 QApplication::sendEvent(this, &e);
3227 Inserts the actions \a actions to this widget's list of actions,
3228 before the action \a before. It appends the action if \a before is 0 or
3229 \a before is not a valid action for this widget.
3231 A QWidget can have at most one of each action.
3233 \sa removeAction(), QMenu, insertAction(), contextMenuPolicy
3235 void QWidget::insertActions(QAction *before, QList<QAction*> actions)
3237 for(int i = 0; i < actions.count(); ++i)
3238 insertAction(before, actions.at(i));
3242 Removes the action \a action from this widget's list of actions.
3243 \sa insertAction(), actions(), insertAction()
3245 void QWidget::removeAction(QAction *action)
3252 QActionPrivate *apriv = action->d_func();
3253 apriv->widgets.removeAll(this);
3255 if (d->actions.removeAll(action)) {
3256 QActionEvent e(QEvent::ActionRemoved, action);
3257 QApplication::sendEvent(this, &e);
3262 Returns the (possibly empty) list of this widget's actions.
3264 \sa contextMenuPolicy, insertAction(), removeAction()
3266 QList<QAction*> QWidget::actions() const
3271 #endif // QT_NO_ACTION
3274 \fn bool QWidget::isEnabledToTLW() const
3277 This function is deprecated. It is equivalent to isEnabled()
3281 \property QWidget::enabled
3282 \brief whether the widget is enabled
3284 An enabled widget handles keyboard and mouse events; a disabled
3287 Some widgets display themselves differently when they are
3288 disabled. For example a button might draw its label grayed out. If
3289 your widget needs to know when it becomes enabled or disabled, you
3290 can use the changeEvent() with type QEvent::EnabledChange.
3292 Disabling a widget implicitly disables all its children. Enabling
3293 respectively enables all child widgets unless they have been
3294 explicitly disabled.
3296 By default, this property is true.
3298 \sa isEnabledTo(), QKeyEvent, QMouseEvent, changeEvent()
3300 void QWidget::setEnabled(bool enable)
3303 setAttribute(Qt::WA_ForceDisabled, !enable);
3304 d->setEnabled_helper(enable);
3307 void QWidgetPrivate::setEnabled_helper(bool enable)
3311 if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->isEnabled())
3312 return; // nothing we can do
3314 if (enable != q->testAttribute(Qt::WA_Disabled))
3315 return; // nothing to do
3317 q->setAttribute(Qt::WA_Disabled, !enable);
3318 updateSystemBackground();
3320 if (!enable && q->window()->focusWidget() == q) {
3321 bool parentIsEnabled = (!q->parentWidget() || q->parentWidget()->isEnabled());
3322 if (!parentIsEnabled || !q->focusNextChild())
3326 Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceDisabled : Qt::WA_Disabled;
3327 for (int i = 0; i < children.size(); ++i) {
3328 QWidget *w = qobject_cast<QWidget *>(children.at(i));
3329 if (w && !w->testAttribute(attribute))
3330 w->d_func()->setEnabled_helper(enable);
3332 #if defined(Q_WS_X11)
3333 if (q->testAttribute(Qt::WA_SetCursor) || q->isWindow()) {
3334 // enforce the windows behavior of clearing the cursor on
3336 qt_x11_enforce_cursor(q);
3339 #if defined(Q_WS_MAC)
3340 setEnabled_helper_sys(enable);
3343 if (q->testAttribute(Qt::WA_InputMethodEnabled) && q->hasFocus()) {
3344 QWidget *focusWidget = effectiveFocusWidget();
3345 QInputContext *qic = focusWidget->d_func()->inputContext();
3347 if (focusWidget->testAttribute(Qt::WA_InputMethodEnabled))
3348 qic->setFocusWidget(focusWidget);
3351 qic->setFocusWidget(0);
3355 QEvent e(QEvent::EnabledChange);
3356 QApplication::sendEvent(q, &e);
3358 q->enabledChange(!enable); // compatibility
3363 \property QWidget::acceptDrops
3364 \brief whether drop events are enabled for this widget
3366 Setting this property to true announces to the system that this
3367 widget \e may be able to accept drop events.
3369 If the widget is the desktop (windowType() == Qt::Desktop), this may
3370 fail if another application is using the desktop; you can call
3371 acceptDrops() to test if this occurs.
3373 \warning Do not modify this property in a drag and drop event handler.
3375 By default, this property is false.
3379 bool QWidget::acceptDrops() const
3381 return testAttribute(Qt::WA_AcceptDrops);
3384 void QWidget::setAcceptDrops(bool on)
3386 setAttribute(Qt::WA_AcceptDrops, on);
3391 \fn void QWidget::enabledChange(bool)
3398 \fn void QWidget::paletteChange(const QPalette &)
3405 \fn void QWidget::fontChange(const QFont &)
3412 \fn void QWidget::windowActivationChange(bool)
3419 \fn void QWidget::languageChange()
3425 \fn void QWidget::styleChange(QStyle& style)
3432 Disables widget input events if \a disable is true; otherwise
3433 enables input events.
3435 See the \l enabled documentation for more information.
3437 \sa isEnabledTo(), QKeyEvent, QMouseEvent, changeEvent()
3439 void QWidget::setDisabled(bool disable)
3441 setEnabled(!disable);
3445 \property QWidget::frameGeometry
3446 \brief geometry of the widget relative to its parent including any
3449 See the \l{Window Geometry} documentation for an overview of geometry
3450 issues with windows.
3452 By default, this property contains a value that depends on the user's
3453 platform and screen geometry.
3455 \sa geometry() x() y() pos()
3457 QRect QWidget::frameGeometry() const
3460 if (isWindow() && ! (windowType() == Qt::Popup)) {
3461 QRect fs = d->frameStrut();
3462 return QRect(data->crect.x() - fs.left(),
3463 data->crect.y() - fs.top(),
3464 data->crect.width() + fs.left() + fs.right(),
3465 data->crect.height() + fs.top() + fs.bottom());
3471 \property QWidget::x
3473 \brief the x coordinate of the widget relative to its parent including
3476 See the \l{Window Geometry} documentation for an overview of geometry
3477 issues with windows.
3479 By default, this property has a value of 0.
3481 \sa frameGeometry, y, pos
3483 int QWidget::x() const
3486 if (isWindow() && ! (windowType() == Qt::Popup))
3487 return data->crect.x() - d->frameStrut().left();
3488 return data->crect.x();
3492 \property QWidget::y
3493 \brief the y coordinate of the widget relative to its parent and
3494 including any window frame
3496 See the \l{Window Geometry} documentation for an overview of geometry
3497 issues with windows.
3499 By default, this property has a value of 0.
3501 \sa frameGeometry, x, pos
3503 int QWidget::y() const
3506 if (isWindow() && ! (windowType() == Qt::Popup))
3507 return data->crect.y() - d->frameStrut().top();
3508 return data->crect.y();
3512 \property QWidget::pos
3513 \brief the position of the widget within its parent widget
3515 If the widget is a window, the position is that of the widget on
3516 the desktop, including its frame.
3518 When changing the position, the widget, if visible, receives a
3519 move event (moveEvent()) immediately. If the widget is not
3520 currently visible, it is guaranteed to receive an event before it
3523 By default, this property contains a position that refers to the
3526 \warning Calling move() or setGeometry() inside moveEvent() can
3527 lead to infinite recursion.
3529 See the \l{Window Geometry} documentation for an overview of geometry
3530 issues with windows.
3532 \sa frameGeometry, size x(), y()
3534 QPoint QWidget::pos() const
3537 if (isWindow() && ! (windowType() == Qt::Popup)) {
3538 QRect fs = d->frameStrut();
3539 return QPoint(data->crect.x() - fs.left(), data->crect.y() - fs.top());
3541 return data->crect.topLeft();
3545 \property QWidget::geometry
3546 \brief the geometry of the widget relative to its parent and
3547 excluding the window frame
3549 When changing the geometry, the widget, if visible, receives a
3550 move event (moveEvent()) and/or a resize event (resizeEvent())
3551 immediately. If the widget is not currently visible, it is
3552 guaranteed to receive appropriate events before it is shown.
3554 The size component is adjusted if it lies outside the range
3555 defined by minimumSize() and maximumSize().
3557 \warning Calling setGeometry() inside resizeEvent() or moveEvent()
3558 can lead to infinite recursion.
3560 See the \l{Window Geometry} documentation for an overview of geometry
3561 issues with windows.
3563 By default, this property contains a value that depends on the user's
3564 platform and screen geometry.
3566 \sa frameGeometry(), rect(), move(), resize(), moveEvent(),
3567 resizeEvent(), minimumSize(), maximumSize()
3571 \property QWidget::normalGeometry
3573 \brief the geometry of the widget as it will appear when shown as
3574 a normal (not maximized or full screen) top-level widget
3576 For child widgets this property always holds an empty rectangle.
3578 By default, this property contains an empty rectangle.
3580 \sa QWidget::windowState(), QWidget::geometry
3584 \property QWidget::size
3585 \brief the size of the widget excluding any window frame
3587 If the widget is visible when it is being resized, it receives a resize event
3588 (resizeEvent()) immediately. If the widget is not currently
3589 visible, it is guaranteed to receive an event before it is shown.
3591 The size is adjusted if it lies outside the range defined by
3592 minimumSize() and maximumSize().
3594 By default, this property contains a value that depends on the user's
3595 platform and screen geometry.
3597 \warning Calling resize() or setGeometry() inside resizeEvent() can
3598 lead to infinite recursion.
3600 \note Setting the size to \c{QSize(0, 0)} will cause the widget to not
3601 appear on screen. This also applies to windows.
3603 \sa pos, geometry, minimumSize, maximumSize, resizeEvent(), adjustSize()
3607 \property QWidget::width
3608 \brief the width of the widget excluding any window frame
3610 See the \l{Window Geometry} documentation for an overview of geometry
3611 issues with windows.
3613 \note Do not use this function to find the width of a screen on
3614 a \l{QDesktopWidget}{multiple screen desktop}. Read
3615 \l{QDesktopWidget#Screen Geometry}{this note} for details.
3617 By default, this property contains a value that depends on the user's
3618 platform and screen geometry.
3620 \sa geometry, height, size
3624 \property QWidget::height
3625 \brief the height of the widget excluding any window frame
3627 See the \l{Window Geometry} documentation for an overview of geometry
3628 issues with windows.
3630 \note Do not use this function to find the height of a screen
3631 on a \l{QDesktopWidget}{multiple screen desktop}. Read
3632 \l{QDesktopWidget#Screen Geometry}{this note} for details.
3634 By default, this property contains a value that depends on the user's
3635 platform and screen geometry.
3637 \sa geometry, width, size
3641 \property QWidget::rect
3642 \brief the internal geometry of the widget excluding any window
3645 The rect property equals QRect(0, 0, width(), height()).
3647 See the \l{Window Geometry} documentation for an overview of geometry
3648 issues with windows.
3650 By default, this property contains a value that depends on the user's
3651 platform and screen geometry.
3657 QRect QWidget::normalGeometry() const
3660 if (!d->extra || !d->extra->topextra)
3663 if (!isMaximized() && !isFullScreen())
3666 return d->topData()->normalGeometry;
3671 \property QWidget::childrenRect
3672 \brief the bounding rectangle of the widget's children
3674 Hidden children are excluded.
3676 By default, for a widget with no children, this property contains a
3677 rectangle with zero width and height located at the origin.
3679 \sa childrenRegion() geometry()
3682 QRect QWidget::childrenRect() const
3685 QRect r(0, 0, 0, 0);
3686 for (int i = 0; i < d->children.size(); ++i) {
3687 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3688 if (w && !w->isWindow() && !w->isHidden())
3695 \property QWidget::childrenRegion
3696 \brief the combined region occupied by the widget's children
3698 Hidden children are excluded.
3700 By default, for a widget with no children, this property contains an
3703 \sa childrenRect() geometry() mask()
3706 QRegion QWidget::childrenRegion() const
3710 for (int i = 0; i < d->children.size(); ++i) {
3711 QWidget *w = qobject_cast<QWidget *>(d->children.at(i));
3712 if (w && !w->isWindow() && !w->isHidden()) {
3713 QRegion mask = w->mask();
3717 r |= mask.translated(w->pos());