Home · All Classes · Main Classes · Grouped Classes · Modules · Functions

QWidget Class Reference
[QtGui module]

The QWidget class is the base class of all user interface objects. More...

#include <QWidget>

Inherits QObject and QPaintDevice.

Inherited by Q3ComboBox, Q3DataBrowser, Q3DataView, Q3DateTimeEdit, Q3DateTimeEditBase, Q3DockArea, Q3Header, Q3MainWindow, QAbstractButton, QAbstractSlider, QAbstractSpinBox, QComboBox, QDesignerActionEditorInterface, QDesignerFormWindowInterface, QDesignerObjectInspectorInterface, QDesignerPropertyEditorInterface, QDesignerWidgetBoxInterface, QDialog, QDockWidget, QFocusFrame, QFrame, QGLWidget, QGroupBox, QLineEdit, QMainWindow, QMenu, QMenuBar, QProgressBar, QRubberBand, QSizeGrip, QSplashScreen, QSplitterHandle, QStatusBar, QSvgWidget, QTabBar, QTabWidget, QToolBar, QWorkspace, QX11EmbedContainer, and QX11EmbedWidget.

Properties

Public Functions

Public Slots

Signals

Static Public Members

Protected Functions

Macros


Detailed Description

The QWidget class is the base class of all user interface objects.

The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. Every widget is rectangular, and they are sorted in a Z-order. A widget is clipped by its parent and by the widgets in front of it.

A widget that isn't embedded in a parent widget is called a window. Usually, windows have a frame and a title bar, although it is also possible to create windows without such decoration using suitable window flags). In Qt, QMainWindow and the various subclasses of QDialog are the most common window types.

A widget without a parent widget is always an independent window.

Non-window widgets are child widgets. These are child windows in their parent widgets. You cannot usually distinguish a child widget from its parent visually. Most other widgets in Qt are useful only as child widgets. (It is possible to make, say, a button into a window, but most people prefer to put their buttons inside other widgets, e.g. QDialog.)

If you want to use a QWidget to hold child widgets you will probably want to add a layout to the parent QWidget. (See Layouts.)

QWidget has many member functions, but some of them have little direct functionality: for example, QWidget has a font property, but never uses this itself. There are many subclasses which provide real functionality, such as QPushButton, QListWidget and QTabWidget, etc.

Groups of functions:

ContextFunctions
Window functionsshow(), hide(), raise(), lower(), close().
Top-level windowsisWindowModified(), setWindowModified(), windowTitle(), setWindowTitle(), windowIcon(), setWindowIcon(), windowIconText(), setWindowIconText(), isActiveWindow(), activateWindow(), showMinimized(). showMaximized(), showFullScreen(), showNormal().
Window contentsupdate(), repaint(), scroll().
Geometrypos(), size(), rect(), x(), y(), width(), height(), sizePolicy(), setSizePolicy(), sizeHint(), updateGeometry(), layout(), move(), resize(), setGeometry(), frameGeometry(), geometry(), childrenRect(), adjustSize(), mapFromGlobal(), mapFromParent() mapToGlobal(), mapToParent(), maximumSize(), minimumSize(), sizeIncrement(), setMaximumSize(), setMinimumSize(), setSizeIncrement(), setBaseSize(), setFixedSize()
ModeisVisible(), isVisibleTo(), isMinimized(), isEnabled(), isEnabledTo(), isModal(), isWindow(), setEnabled(), hasMouseTracking(), setMouseTracking(), updatesEnabled(), setUpdatesEnabled(), visibleRegion().
Look and feelstyle(), setStyle(), cursor(), setCursor() font(), setFont(), palette(), setPalette(), backgroundRole(), setBackgroundRole(), fontMetrics(), fontInfo().
Keyboard focus functionssetFocusPolicy(), focusPolicy(), hasFocus(), setFocus(), clearFocus(), setTabOrder(), setFocusProxy().
Mouse and keyboard grabbinggrabMouse(), releaseMouse(), grabKeyboard(), releaseKeyboard(), mouseGrabber(), keyboardGrabber().
Event handlersevent(), mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), keyPressEvent(), keyReleaseEvent(), focusInEvent(), focusOutEvent(), wheelEvent(), enterEvent(), leaveEvent(), paintEvent(), moveEvent(), resizeEvent(), closeEvent(), dragEnterEvent(), dragMoveEvent(), dragLeaveEvent(), dropEvent(), childEvent(), showEvent(), hideEvent(), customEvent(). changeEvent(),
System functionsparentWidget(), window(), setParent(), winId(), find(), metric().
What's this helpsetWhatsThis()
Focus functionsfocusNextChild(), focusPreviousChild()

Every widget's constructor accepts one or two standard arguments:

  1. QWidget *parent = 0 is the parent of the new widget. If it is 0 (the default), the new widget will be a window. If not, it will be a child of parent, and be constrained by parent's geometry (unless you specify Qt::Window as window flag).
  2. Qt::WFlags f = 0 (where available) sets the window flags; the default is suitable for almost all widgets, but to get, for example, a window without a window system frame, you must use special flags.

The widgets/calculator example program is good example of a simple widget. It contains event handlers (as all widgets must), routines that are specific to it (as all useful widgets do), and has children and connections. Everything it does is done in response to an event: this is by far the most common way to design GUI applications.

You will need to supply the content for your widgets yourself, but here is a brief run-down of the events, starting with the most common ones:

If your widget only contains child widgets, you probably do not need to implement any event handlers. If you want to detect a mouse click in a child widget call the child's underMouse() function inside the parent widget's mousePressEvent().

Widgets that accept keyboard input need to reimplement a few more event handlers:

Some widgets will also need to reimplement some of the less common event handlers:

There are also some rather obscure events. They are listed in qevent.h and you need to reimplement event() to handle them. The default implementation of event() handles Tab and Shift+Tab (to move the keyboard focus), and passes on most other events to one of the more specialized handlers above.

When implementing a widget, there are a few more things to consider.

Transparency and Double Buffering

From Qt 4.0, QWidget automatically double-buffers its painting, so there's no need to write double-buffering code in paintEvent() to avoid flicker. Additionally, it became possible for widgets to propagate their contents to children, in order to enable transparency effects, by setting the Qt::WA_ContentsPropagated widget attribute - this is now deprecated in Qt 4.1.

In Qt 4.1, the contents of parent widgets are propagated by default to each of their children. Custom widgets can be written to take advantage of this feature by only updating irregular regions (to create non-rectangular child widgets), or by using painting with colors that have less than the full alpha component. The following diagram shows how attributes and properties of a custom widget can be fine-tuned to achieve different effects.

In the above diagram, a semi-transparent rectangular child widget with an area removed is constructed and added to a parent widget (a QLabel showing a pixmap) then different properties and widget attributes are set to achieve different effects:

For rapidly updating custom widgets with simple background colors, such as real-time plotting or graphing widgets, it is better to define a suitable background color (using setBackgroundRole() with the QPalette::Window role), set the autoFillBackground property, and only implement the necessary drawing functionality in the widget's paintEvent().

For rapidly updating custom widgets that constantly paint over their entire areas with opaque content, such as video streaming widgets, it is better to set the widget's Qt::WA_OpaquePaintEvent, avoiding any unnecessary overhead associated with repainting the widget's background.

If a widget has both the Qt::WA_OpaquePaintEvent widget attribute and the autoFillBackground property set, the Qt::WA_OpaquePaintEvent attribute takes precedence. You should choose just one of these depending on your requirements.

In Qt 4.1, the contents of parent widgets are also propagated to standard Qt widgets. This can lead to some unexpected results if the parent widget is decorated in a non-standard way, as shown in the diagram below.

The scope for customizing the painting behavior of standard Qt widgets, without resorting to subclassing, is slightly less than that possible for custom widgets. Usually, the desired appearance of a standard widget can be achieved by setting its autoFillBackground property.

See also QEvent, QPainter, QGridLayout, and QBoxLayout.


Property Documentation

acceptDrops : bool

This property holds whether drop events are enabled for this widget.

Setting this property to true announces to the system that this widget may be able to accept drop events.

If the widget is the desktop (QWidget::(windowType() == Qt::Desktop)), this may fail if another application is using the desktop; you can call acceptDrops() to test if this occurs.

Warning: Do not modify this property in a Drag&Drop event handler.

Access functions:

accessibleDescription : QString

This property holds the widget's description as seen by assistive technologies.

Access functions:

See also QAccessibleInterface::text().

accessibleName : QString

This property holds the widget's name as seen by assistive technologies.

Access functions:

See also QAccessibleInterface::text().

autoFillBackground : bool

This property holds whether the widget background is filled automatically.

If enabled, this will cause Qt to fill the background using the widget's background role before invoking the paint event. The background role is defined by the widget's palette.

In addition, Windows are always filled with QPalette::Window, unless the WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set.

This property was introduced in Qt 4.1.

Access functions:

See also Qt::WA_OpaquePaintEvent and Qt::WA_NoSystemBackground.

baseSize : QSize

This property holds the base size of the widget.

The base size is used to calculate a proper widget size if the widget defines sizeIncrement().

Access functions:

See also setSizeIncrement().

childrenRect : const QRect

This property holds the bounding rectangle of the widget's children.

Hidden children are excluded.

Access functions:

See also childrenRegion() and geometry().

childrenRegion : const QRegion

This property holds the combined region occupied by the widget's children.

Hidden children are excluded.

Access functions:

See also childrenRect(), geometry(), and mask().

contextMenuPolicy : Qt::ContextMenuPolicy

This property holds how the widget shows a context menu.

The default value of this property is Qt::DefaultContextMenu, which means the contextMenuEvent() handler is called. Other values are Qt::NoContextMenu, Qt::ActionsContextMenu, and Qt::CustomContextMenu. With Qt::CustomContextMenu, the signal customContextMenuRequested() is emitted.

Access functions:

See also contextMenuEvent() and customContextMenuRequested().

cursor : QCursor

This property holds the cursor shape for this widget.

The mouse cursor will assume this shape when it's over this widget. See the list of predefined cursor objects for a range of useful shapes.

An editor widget might use an I-beam cursor:

    setCursor(Qt::IBeamCursor);

If no cursor has been set, or after a call to unsetCursor(), the parent's cursor is used. The function unsetCursor() has no effect on windows.

Access functions:

See also QApplication::setOverrideCursor().

enabled : bool

This property holds whether the widget is enabled.

An enabled widget handles keyboard and mouse events; a disabled widget does not.

Some widgets display themselves differently when they are disabled. For example a button might draw its label grayed out. If your widget needs to know when it becomes enabled or disabled, you can use the changeEvent() with type QEvent::EnabledChange.

Disabling a widget implicitly disables all its children. Enabling respectively enables all child widgets unless they have been explicitly disabled.

Access functions:

See also isEnabledTo(), QKeyEvent, QMouseEvent, and changeEvent().

focus : const bool

This property holds whether this widget (or its focus proxy) has the keyboard input focus.

Effectively equivalent to QApplication::focusWidget() == this.

Access functions:

See also setFocus(), clearFocus(), setFocusPolicy(), and QApplication::focusWidget().

focusPolicy : Qt::FocusPolicy

This property holds the way the widget accepts keyboard focus.

The policy is Qt::TabFocus if the widget accepts keyboard focus by tabbing, Qt::ClickFocus if the widget accepts focus by clicking, Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if it does not accept focus at all.

You must enable keyboard focus for a widget if it processes keyboard events. This is normally done from the widget's constructor. For instance, the QLineEdit constructor calls setFocusPolicy(Qt::StrongFocus).

Access functions:

See also focusInEvent(), focusOutEvent(), keyPressEvent(), keyReleaseEvent(), and enabled.

font : QFont

This property holds the font currently set for the widget.

The fontInfo() function reports the actual font that is being used by the widget.

As long as no special font has been set, or after setFont(QFont()) is called, this is either a special font for the widget class, the parent's font or (if this widget is a top level widget), the default application font.

This code fragment sets a 12 point helvetica bold font:

    QFont f("Helvetica", 12, QFont::Bold);
    setFont(f);

In addition to setting the font, setFont() informs all children about the change.

Access functions:

See also fontInfo() and fontMetrics().

frameGeometry : const QRect

This property holds geometry of the widget relative to its parent including any window frame.

See the Window Geometry documentation for an overview of geometry issues with windows.

Access functions:

See also geometry(), x(), y(), and pos().

frameSize : const QSize

This property holds the size of the widget including any window frame.

Access functions:

fullScreen : const bool

This property holds whether the widget is full screen.

Access functions:

See also windowState(), minimized, and maximized.

geometry : QRect

This property holds the geometry of the widget relative to its parent and excluding the window frame.

When changing the geometry, the widget, if visible, receives a move event (moveEvent()) and/or a resize event (resizeEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive appropriate events before it is shown.

The size component is adjusted if it lies outside the range defined by minimumSize() and maximumSize().

Warning: Calling setGeometry() inside resizeEvent() or moveEvent() can lead to infinite recursion.

See the Window Geometry documentation for an overview of window geometry.

Access functions:

See also frameGeometry(), rect(), move(), resize(), moveEvent(), resizeEvent(), minimumSize(), and maximumSize().

height : const int

This property holds the height of the widget excluding any window frame.

See the Window Geometry documentation for an overview of window geometry.

Access functions:

See also geometry, width, and size.

isActiveWindow : const bool

This property holds whether this widget's window is the active window.

The active window is the window that contains the widget that has keyboard focus.

When popup windows are visible, this property is true for both the active window and for the popup.

Access functions:

See also activateWindow() and QApplication::activeWindow().

layoutDirection : Qt::LayoutDirection

This property holds the layout direction for this widget.

Access functions:

See also QApplication::layoutDirection.

maximized : const bool

This property holds whether this widget is maximized.

This property is only relevant for windows.

Note that due to limitations in some window-systems, this does not always report the expected results (e.g. if the user on X11 maximizes the window via the window manager, Qt has no way of distinguishing this from any other resize). This is expected to improve as window manager protocols evolve.

Access functions:

See also windowState(), showMaximized(), visible, show(), hide(), showNormal(), and minimized.

maximumHeight : int

This property holds the widget's maximum height.

This property corresponds to maximumSize().height(). It is limited by the QWIDGETSIZE_MAX macro, i.e. the largest allowed height is 16777215.

Access functions:

See also maximumSize and maximumWidth.

maximumSize : QSize

This property holds the widget's maximum size.

The widget cannot be resized to a larger size than the maximum widget size.

The property is limited by the QWIDGETSIZE_MAX macro, i.e. the largest allowed size is QSize(16777215, 16777215).

Access functions:

See also maximumWidth, maximumHeight, minimumSize, and sizeIncrement.

maximumWidth : int

This property holds the widget's maximum width.

This property corresponds to maximumSize().width(). It is limited by the QWIDGETSIZE_MAX macro, i.e. the largest allowed width is 16777215.

Access functions:

See also maximumSize and maximumHeight.

minimized : const bool

This property holds whether this widget is minimized (iconified).

This property is only relevant for windows.

Access functions:

See also showMinimized(), visible, show(), hide(), showNormal(), and maximized.

minimumHeight : int

This property holds the widget's minimum height.

This property corresponds to minimumSize().height().

Access functions:

See also minimumSize and minimumWidth.

minimumSize : QSize

This property holds the widget's minimum size.

The widget cannot be resized to a smaller size than the minimum widget size. The widget's size is forced to the minimum size if the current size is smaller.

The minimum size set by this function will override the minimum size defined by QLayout.

Access functions:

See also minimumWidth, minimumHeight, maximumSize, and sizeIncrement.

minimumSizeHint : const QSize

This property holds the recommended minimum size for the widget.

If the value of this property is an invalid size, no minimum size is recommended.

The default implementation of minimumSizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's minimum size otherwise. Most built-in widgets reimplement minimumSizeHint().

QLayout will never resize a widget to a size smaller than the minimum size hint unless minimumSize() is set or the size policy is set to QSizePolicy::Ignore. If minimumSize() is set, the minimum size hint will be ignored.

Access functions:

See also QSize::isValid(), resize(), setMinimumSize(), and sizePolicy().

minimumWidth : int

This property holds the widget's minimum width.

This property corresponds to minimumSize().width().

Access functions:

See also minimumSize and minimumHeight.

modal : const bool

This property holds whether the widget is a modal widget.

This property only makes sense for windows. A modal widget prevents widgets in all other windows from getting any input.

Access functions:

See also isWindow(), windowModality, and QDialog.

mouseTracking : bool

This property holds whether mouse tracking is enabled for the widget.

If mouse tracking is disabled (the default), the widget only receives mouse move events when at least one mouse button is pressed while the mouse is being moved.

If mouse tracking is enabled, the widget receives mouse move events even if no buttons are pressed.

Access functions:

See also mouseMoveEvent().

normalGeometry : const QRect

This property holds the geometry of the widget as it will appear when shown as a normal (not maximized or fullscreen) toplevel widget.

For child widgets this property always holds an empty rect.

Access functions:

See also QWidget::windowState() and QWidget::geometry.

palette : QPalette

This property holds the widget's palette.

As long as no special palette has been set, this is either a special palette for the widget class, the parent's palette or (if this widget is a top level widget), the default application palette.

Note: The palette's background color will only have an effect on the appearance of the widget if the autoFillBackground property is set.

Access functions:

See also QApplication::palette().

pos : QPoint

This property holds the position of the widget within its parent widget.

If the widget is a window, the position is that of the widget on the desktop, including its frame.

When changing the position, the widget, if visible, receives a move event (moveEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive an event before it is shown.

move() is virtual, and all other overloaded move() implementations in Qt call it.

Warning: Calling move() or setGeometry() inside moveEvent() can lead to infinite recursion.

See the Window Geometry documentation for an overview of window geometry.

Access functions:

See also frameGeometry, size, x(), and y().

rect : const QRect

This property holds the internal geometry of the widget excluding any window frame.

The rect property equals QRect(0, 0, width(), height()).

See the Window Geometry documentation for an overview of window geometry.

Access functions:

See also size.

size : QSize

This property holds the size of the widget excluding any window frame.

If the widget is visible when it is being resized, it receives a resize event (resizeEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive an event before it is shown.

The size is adjusted if it lies outside the range defined by minimumSize() and maximumSize(). For windows, the minimum size is always at least QSize(1, 1), and it might be larger, depending on the window manager.

Warning: Calling resize() or setGeometry() inside resizeEvent() can lead to infinite recursion.

Access functions:

See also pos, geometry, minimumSize, maximumSize, and resizeEvent().

sizeHint : const QSize

This property holds the recommended size for the widget.

If the value of this property is an invalid size, no size is recommended.

The default implementation of sizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise.

Access functions:

See also QSize::isValid(), minimumSizeHint(), sizePolicy(), setMinimumSize(), and updateGeometry().

sizeIncrement : QSize

This property holds the size increment of the widget.

When the user resizes the window, the size will move in steps of sizeIncrement().width() pixels horizontally and sizeIncrement.height() pixels vertically, with baseSize() as the basis. Preferred widget sizes are for non-negative integers i and j:

    width = baseSize().width() + i * sizeIncrement().width();
    height = baseSize().height() + j * sizeIncrement().height();

Note that while you can set the size increment for all widgets, it only affects windows.

Warning: The size increment has no effect under Windows, and may be disregarded by the window manager on X.

Access functions:

See also size, minimumSize, and maximumSize.

sizePolicy : QSizePolicy

This property holds the default layout behavior of the widget.

If there is a QLayout that manages this widget's children, the size policy specified by that layout is used. If there is no such QLayout, the result of this function is used.

The default policy is Preferred/Preferred, which means that the widget can be freely resized, but prefers to be the size sizeHint() returns. Button-like widgets set the size policy to specify that they may stretch horizontally, but are fixed vertically. The same applies to lineedit controls (such as QLineEdit, QSpinBox or an editable QComboBox) and other horizontally orientated widgets (such as QProgressBar). QToolButton's are normally square, so they allow growth in both directions. Widgets that support different directions (such as QSlider, QScrollBar or QHeader) specify stretching in the respective direction only. Widgets that can provide scrollbars (usually subclasses of QScrollView) tend to specify that they can use additional space, and that they can make do with less than sizeHint().

Access functions:

See also sizeHint(), QLayout, QSizePolicy, and updateGeometry().

statusTip : QString

This property holds the widget's status tip.

Access functions:

See also toolTip and whatsThis.

toolTip : QString

This property holds the widget's tooltip.

Access functions:

See also QToolTip, statusTip, and whatsThis.

updatesEnabled : bool

This property holds whether updates are enabled.

An updates enabled widget receives paint events and has a system background; a disabled widget does not. This also implies that calling update() and repaint() has no effect if updates are disabled.

setUpdatesEnabled() is normally used to disable updates for a short period of time, for instance to avoid screen flicker during large changes. In Qt, widgets normally do not generate screen flicker, but on X11 the server might erase regions on the screen when widgets get hidden before they can be replaced by other widgets. Disabling updates solves this.

Example:

    setUpdatesEnabled(false);
    bigVisualChanges();
    setUpdatesEnabled(true);

Disabling a widget implicitly disables all its children. Enabling respectively enables all child widgets unless they have been explicitly disabled. Re-enabling updates implicitly calls update() on the widget.

Access functions:

See also paintEvent().

visible : bool

This property holds whether the widget is visible.

Calling setVisible(true) or show() sets the widget to visible status if all its parent widgets up to the window are visible. If an ancestor is not visible, the widget won't become visible until all its ancestors are shown. If its size or position has changed, Qt guarantees that a widget gets move and resize events just before it is shown. If the widget has not been resized yet, Qt will adjust the widget's size to a useful default using adjustSize().

Calling setVisible(false) or hide() hides a widget explicitly. An explicitly hidden widget will never become visible, even if all its ancestors become visible, unless you show it.

A widget receives show and hide events when its visibility status changes. Between a hide and a show event, there is no need to waste CPU cycles preparing or displaying information to the user. A video application, for example, might simply stop generating new frames.

A widget that happens to be obscured by other windows on the screen is considered to be visible. The same applies to iconified windows and windows that exist on another virtual desktop (on platforms that support this concept). A widget receives spontaneous show and hide events when its mapping status is changed by the window system, e.g. a spontaneous hide event when the user minimizes the window, and a spontaneous show event when the window is restored again.

You almost never have to reimplement the setVisible() function. If you need to change some settings before a widget is shown, use showEvent() instead. If you need to do some delayed initialization use the Polish event delivered to the event() method

Access functions:

See also show(), hide(), isHidden(), isVisibleTo(), isMinimized(), showEvent(), and hideEvent().

whatsThis : QString

This property holds the widget's What's This help text.

Access functions:

See also QWhatsThis, QWidget::toolTip, and QWidget::statusTip.

width : const int

This property holds the width of the widget excluding any window frame.

See the Window Geometry documentation for an overview of window geometry.

Access functions:

See also geometry, height, and size.

windowFlags : Qt::WindowFlags

Window flags are a combination of a type (e.g. Qt::Dialog) and zero or more hints to the window system (e.g. Qt::FramelessWindowHint).

If the widget had type Qt::Widget or Qt::SubWindow and becomes a window (Qt::Window, Qt::Dialog, etc.), it is put at position (0, 0) on the desktop. If the widget is a window and becomes a Qt::Widget or Qt::SubWindow, it is put at position (0, 0) relative to its parent widget.

Access functions:

See also windowType().

windowIcon : QIcon

This property holds the widget's icon.

This property only makes sense for windows. If no icon has been set, windowIcon() returns the application icon (QApplication::windowIcon()).

Access functions:

See also windowIconText and windowTitle.

windowIconText : QString

This property holds the widget's icon text.

This property only makes sense for windows. If no icon text has been set, this functions returns an empty string.

Access functions:

See also windowIcon and windowTitle.

windowModality : Qt::WindowModality

This property holds which windows are blocked by the modal widget.

This property only makes sense for windows. A modal widget prevents widgets in other windows from getting input. The value of this property controls which windows are blocked when the widget is visible. Changing this property while the window is visible has no effect; you must hide() the wiget first, then show() it again.

By default, this property is Qt::NonModal.

This property was introduced in Qt 4.1.

Access functions:

See also isWindow(), QWidget::modal, and QDialog.

windowModified : bool

This property holds whether the document shown in the window has unsaved changes.

A modified window is a window whose content has changed but has not been saved to disk. This flag will have different effects varied by the platform. On Mac OS X the close button will have a modified look; on other platforms, the window title will have an '*' (asterisk).

The window title must contain a "[*]" placeholder, which indicates where the '*' should appear. Normally, it should appear right after the file name (e.g., "document1.txt[*] - Text Editor"). If the window isn't modified, the placeholder is simply removed.

Access functions:

See also windowTitle.

windowOpacity : double

This property holds the level of opacity for the window.

The valid range of opacity is from 1.0 (completely opaque) to 0.0 (completely transparent).

By default the value of this property is 1.0.

This feature is available on Mac OS X, X11 platforms that support the Composite extension, and Windows 2000 and later.

Note that under X11 you need to have a composite manager running, and the X11 specific _NET_WM_WINDOW_OPACITY atom needs to be supported by the window manager you are using.

Warning: Changing this property from opaque to transparent might issue a paint event that needs to be processed before the window is displayed correctly. This affects mainly the use of QPixmap::grabWindow(). Also note that semi-transparent windows update and resize significantly slower than opaque windows.

Access functions:

See also setMask().

windowTitle : QString

This property holds the window title (caption).

This property only makes sense for windows. If no caption has been set, the title is an empty string.

If you use the windowModified mechanism, the window title must contain a "[*]" placeholder, which indicates where the '*' should appear. Normally, it should appear right after the file name (e.g., "document1.txt[*] - Text Editor"). If the windowModified property is false (the default), the placeholder is simply removed.

Access functions:

See also windowIcon, windowIconText, and windowModified.

x : const int

This property holds the x coordinate of the widget relative to its parent including any window frame.

See the Window Geometry documentation for an overview of window geometry.

Access functions:

See also frameGeometry, y, and pos.

y : const int

This property holds the y coordinate of the widget relative to its parent and including any window frame.

See the Window Geometry documentation for an overview of window geometry.

Access functions:

See also frameGeometry, x, and pos.


Member Function Documentation

QWidget::QWidget ( QWidget * parent = 0, Qt::WFlags f = 0 )

Constructs a widget which is a child of parent, with widget flags set to f.

If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is deleted.

The widget flags argument, f, is normally 0, but it can be set to customize the frame of a window (i.e. parent must be 0). To customize the frame, use a value composed from the bitwise OR of any of the window flags.

If you add a child widget to an already visible widget you must explicitly show the child to make it visible.

Note that the X11 version of Qt may not be able to deliver all combinations of style flags on all systems. This is because on X11, Qt can only ask the window manager, and the window manager can override the application's settings. On Windows, Qt can set whatever flags you want.

QWidget::~QWidget ()

Destroys the widget.

All this widget's children are deleted first. The application exits if this widget is the main widget.

void QWidget::actionEvent ( QActionEvent * event )   [virtual protected]

This event handler is called with the given event whenever the widget's actions are changed.

See also addAction(), insertAction(), removeAction(), actions(), and QActionEvent.

QList<QAction *> QWidget::actions () const

Returns the (possibly empty) list of this widget's actions.

void QWidget::activateWindow ()

Sets the top-level widget containing this widget to be the active window.

An active window is a visible top-level window that has the keyboard input focus.

This function performs the same operation as clicking the mouse on the title bar of a top-level window. On X11, the result depends on the Window Manager. If you want to ensure that the window is stacked on top as well you should also call raise(). Note that the window must be visible, otherwise activateWindow() has no effect.

On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the task bar entry to indicate that the window has changed in some way. This is because Microsoft do not allow an application to interrupt what the user is currently doing in another application.

See also isActiveWindow(), window(), and show().

void QWidget::addAction ( QAction * action )

Appends the action action to this widget's list of actions.

All QWidgets have a list of QActions, however they can be represented graphically in many different ways. The default use of the QAction list (as returned by actions()) is to create a context QMenu.

A QWidget should only have one of each action.

See also removeAction() and QMenu.

void QWidget::addActions ( QList<QAction *> actions )

Appends the actions actions to this widget's list of actions.

See also removeAction(), QMenu, and addAction().

void QWidget::adjustSize ()

Adjusts the size of the widget to fit the contents.

Uses sizeHint() if valid (i.e if the size hint's width and height are >= 0), otherwise sets the size to the children rectangle (the union of all child widget geometries). For toplevel widgets, the function also takes the screen size into account.

See also sizeHint() and childrenRect().

QPalette::ColorRole QWidget::backgroundRole () const

Returns the background role of the widget.

The background role defines the brush from the widget's palette that is used to render the background.

If no explicit background role is set, the widget inherts its parent widget's background role.

See also setBackgroundRole() and foregroundRole().

void QWidget::changeEvent ( QEvent * event )   [virtual protected]

This event handler can be reimplemented to handle state changes.

The state being changed in this event can be retrieved through event event.

Change events include: QEvent::ToolBarChange, QEvent::ActivationChange, QEvent::EnabledChange, QEvent::FontChange, QEvent::StyleChange, QEvent::PaletteChange, QEvent::WindowTitleChange, QEvent::IconTextChange, QEcvent::ModifiedChange, QEvent::MouseTrackingChange, QEvent::ParentChange, QEvent::WindowStateChange.

QWidget * QWidget::childAt ( int x, int y ) const

Returns the visible child widget at the position (x, y) in the widget's coordinate system. If there is no visible child widget at the specified position, the function returns 0.

QWidget * QWidget::childAt ( const QPoint & p ) const

This is an overloaded member function, provided for convenience.

Returns the visible child widget at point p in the widget's own coordinate system.

void QWidget::clearFocus ()

Takes keyboard input focus from the widget.

If the widget has active focus, a focus out event is sent to this widget to tell it that it is about to lose the focus.

This widget must enable focus setting in order to get the keyboard input focus, i.e. it must call setFocusPolicy().

See also hasFocus(), setFocus(), focusInEvent(), focusOutEvent(), setFocusPolicy(), and QApplication::focusWidget().

void QWidget::clearMask ()

Removes any mask set by setMask().

See also setMask().

bool QWidget::close ()   [slot]

Closes this widget. Returns true if the widget was closed; otherwise returns false.

First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it ignores the event, nothing happens. The default implementation of QWidget::closeEvent() accepts the close event.

If the widget has the Qt::WA_DeleteOnClose flag, the widget is also deleted. A close events is delivered to the widget no matter if the widget is visible or not.

The QApplication::lastWindowClosed() signal is emitted when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus.

void QWidget::closeEvent ( QCloseEvent * event )   [virtual protected]

This event handler, for event event, can be reimplemented in a subclass to receive widget close events.

See also event(), hide(), close(), and QCloseEvent.

QRect QWidget::contentsRect () const

Returns the area inside the widget's margins.

See also setContentsMargins() and getContentsMargins().

void QWidget::contextMenuEvent ( QContextMenuEvent * event )   [virtual protected]

This event handler, for event event, can be reimplemented in a subclass to receive widget context menu events.

The handler is called when the widget's contextMenuPolicy is Qt::DefaultContextMenu.

The default implementation calls event->ignore(), which rejects the context event. See the QContextMenuEvent documentation for more details.

See also event(), QContextMenuEvent, and customContextMenuRequested().

void QWidget::create ( WId window = 0, bool initializeWindow = true, bool destroyOldWindow = true )   [protected]

Creates a new widget window if window is 0, otherwise sets the widget's window to window.

Initializes the window (sets the geometry etc.) if initializeWindow is true. If initializeWindow is false, no initialization is performed. This parameter only makes sense if window is a valid window.

Destroys the old window if destroyOldWindow is true. If destroyOldWindow is false, you are responsible for destroying the window yourself (using platform native code).

The QWidget constructor calls create(0,true,true) to create a window for this widget.

void QWidget::customContextMenuRequested ( const QPoint & pos )   [signal]

This signal is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu, and the user has requested a context menu on the widget. The position pos is the position of the context menu event that the widget receives. Normally this is in widget coordinates. The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport() .

See also mapToGlobal(), QMenu, and contextMenuPolicy.

void QWidget::destroy ( bool destroyWindow = true, bool destroySubWindows = true )   [protected]

Frees up window system resources. Destroys the widget window if destroyWindow is true.

destroy() calls itself recursively for all the child widgets, passing destroySubWindows for the destroyWindow parameter. To have more control over destruction of subwidgets, destroy subwidgets selectively first.

This function is usually called from the QWidget destructor.

void QWidget::dragEnterEvent ( QDragEnterEvent * event )   [virtual protected]

This event handler is called when a drag is in progress and the mouse enters this widget. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also QTextDrag, QImageDrag, and QDragEnterEvent.

void QWidget::dragLeaveEvent ( QDragLeaveEvent * event )   [virtual protected]

This event handler is called when a drag is in progress and the mouse leaves this widget. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also QTextDrag, QImageDrag, and QDragLeaveEvent.

void QWidget::dragMoveEvent ( QDragMoveEvent * event )   [virtual protected]

This event handler is called if a drag is in progress, and when any of the following conditions occurs: the cursor enters this widget, the cursor moves within this widget, or a modifier key is pressed on the keyboard while this widget has the focus. The event is passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also QTextDrag, QImageDrag, and QDragMoveEvent.

void QWidget::dropEvent ( QDropEvent * event )   [virtual protected]

This event handler is called when the drag is dropped on this widget which are passed in the event parameter.

See the Drag-and-drop documentation for an overview of how to provide drag-and-drop in your application.

See also QTextDrag, QImageDrag, and QDropEvent.

void QWidget::ensurePolished () const

Ensures that the widget has been polished by QStyle (i.e., has a proper font and palette).

QWidget calls this function after it has been fully constructed but before it is shown the very first time. You can call this function if you want to ensure that the widget is polished before doing an operation (e.g., the correct font size might be needed in the widget's sizeHint() reimplementation).

Polishing is useful for final initialization that must happen after all constructors (from base classes as well as from subclasses) have been called.

If you need to change some settings when a widget is polished, reimplement event() and handle the QEvent::Polish event type.

Note: The function is declared const so that it can be called from other const functions (e.g., sizeHint()).

See also event().

void QWidget::enterEvent ( QEvent * event )   [virtual protected]

This event handler can be reimplemented in a subclass to receive widget enter events which are passed in the event parameter.

An event is sent to the widget when the mouse cursor enters the widget.

See also leaveEvent(), mouseMoveEvent(), and event().

bool QWidget::event ( QEvent * event )   [virtual protected]

This is the main event handler; it handles event event. You can reimplement this function in a subclass, but we recommend using one of the specialized event handlers instead.

Key press and release events are treated differently from other events. event() checks for Tab and Shift+Tab and tries to move the focus appropriately. If there is no widget to move the focus to (or the key press is not Tab or Shift+Tab), event() calls keyPressEvent().

Mouse and tablet event handling is also slightly special: only when the widget is enabled, event() will call the specialized handlers such as mousePressEvent(); otherwise it will discard the event.

This function returns true if the event was recognized, otherwise it returns false. If the recognized event was accepted (see QEvent::accepted), any further processing such as event propagation to the parent widget stops.

Reimplemented from QObject.

See also closeEvent(), focusInEvent(), focusOutEvent(), enterEvent(), keyPressEvent(), keyReleaseEvent(), leaveEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), mousePressEvent(), mouseReleaseEvent(), moveEvent(), paintEvent(), resizeEvent(), QObject::event(), and QObject::timerEvent().

QWidget * QWidget::find ( WId id )   [static]

Returns a pointer to the widget with window identifer/handle id.

The window identifier type depends on the underlying window system, see qwindowdefs.h for the actual definition. If there is no widget with this identifier, 0 is returned.

void QWidget::focusInEvent ( QFocusEvent * event )   [virtual protected]

This event handler can be reimplemented in a subclass to receive keyboard focus events (focus received) for the widget. The event is passed in the event parameter

A widget normally must setFocusPolicy() to something other than Qt::NoFocus in order to receive focus events. (Note that the application programmer can call setFocus() on any widget, even those that do not normally accept focus.)

The default implementation updates the widget (except for windows that do not specify a focusPolicy()).

See also focusOutEvent(), setFocusPolicy(), keyPressEvent(), keyReleaseEvent(), event(), and QFocusEvent.

bool QWidget::focusNextChild ()   [protected]

Finds a new widget to give the keyboard focus to, as appropriate for Tab, and returns true if it can find a new widget, or false if it can't.

This is the same as focusNextPrevChild(true).

See also focusPreviousChild().

bool QWidget::focusNextPrevChild ( bool next )   [virtual protected]

Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns true if it can find a new widget, or false if it can't.

If next is true, this function searches forward