📄 qwidget.cpp
字号:
data.widget_attributes = 0; data.window_flags = f; data.window_state = 0; data.focus_policy = 0; data.context_menu_policy = Qt::DefaultContextMenu; data.window_modality = Qt::NonModal; data.sizehint_forced = 0; data.is_closing = 0; data.in_show = 0; data.in_set_window_state = 0; q->setAttribute(Qt::WA_QuitOnClose); // might be cleared in create() q->create(); // platform-dependent init#if defined(Q_WS_X11) data.fnt.x11SetScreen(xinfo.screen());#endif // Q_WS_X11 if (!(q->windowType() == Qt::Desktop)) updateSystemBackground(); if (q->isWindow()) { if (QApplication::isRightToLeft()) q->setAttribute(Qt::WA_RightToLeft);#ifdef Q_WS_MAC extern bool qt_mac_is_macdrawer(const QWidget *); //qwidget_mac.cpp if(!qt_mac_is_macdrawer(q)) //special case q->setAttribute(Qt::WA_WState_Hidden);#else q->setAttribute(Qt::WA_WState_Hidden);#endif createTLExtra(); } else { QWidget *parentWidget = q->parentWidget(); // propagate palette data.pal = parentWidget->data->pal; data.pal.resolve(0); //propagate font data.fnt = parentWidget->data->fnt; data.fnt.resolve(0); // propagate enabled state if (!parentWidget->isEnabled()) q->setAttribute(Qt::WA_Disabled, true); // propgate updates enabled state if (!parentWidget->updatesEnabled()) q->setAttribute(Qt::WA_UpdatesDisabled, true); //propagate layout direction if (parentWidget->testAttribute(Qt::WA_RightToLeft)) q->setAttribute(Qt::WA_RightToLeft); // new widgets do not show up in already visible parents if (parentWidget->isVisible()) q->setAttribute(Qt::WA_WState_Hidden); } if (q->isWindow()) { focus_next = q; } else { // insert at the end of the focus chain QWidget *focus_handler = q->window(); QWidget *w = focus_handler; while (w->d_func()->focus_next != focus_handler) w = w->d_func()->focus_next; w->d_func()->focus_next = q; focus_next = focus_handler; } q->setAttribute(Qt::WA_PendingMoveEvent); q->setAttribute(Qt::WA_PendingResizeEvent); if (++QWidgetPrivate::instanceCounter > QWidgetPrivate::maxInstances) QWidgetPrivate::maxInstances = QWidgetPrivate::instanceCounter; QEvent e(QEvent::Create); QApplication::sendEvent(q, &e); QApplication::postEvent(q, new QEvent(QEvent::PolishRequest)); extraPaintEngine = 0; // send and post remaining QObject events if (q->parent() && sendChildEvents) { QChildEvent e(QEvent::ChildAdded, q); QApplication::sendEvent(q->parent(), &e);#ifdef QT3_SUPPORT QApplication::postEvent(q->parent(), new QChildEvent(QEvent::ChildInserted, q));#endif }}/*! Creates a new widget window if \a window is 0, otherwise sets the widget's window to \a window. Initializes the window (sets the geometry etc.) if \a initializeWindow is true. If \a initializeWindow is false, no initialization is performed. This parameter only makes sense if \a window is a valid window. Destroys the old window if \a destroyOldWindow is true. If \a 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::create(WId window, bool initializeWindow, bool destroyOldWindow){ Q_D(QWidget); if (testAttribute(Qt::WA_WState_Created) && window == 0) return; setAttribute(Qt::WA_WState_Created); // set created flag Qt::WindowType type = windowType(); Qt::WindowFlags &flags = data->window_flags; if ((type == Qt::Widget || type == Qt::SubWindow) && !parentWidget()) { type = Qt::Window; flags |= Qt::Window; }#ifdef QT3_SUPPORT if (flags & Qt::WStaticContents) setAttribute(Qt::WA_StaticContents); if (flags & Qt::WDestructiveClose) setAttribute(Qt::WA_DeleteOnClose); if (flags & Qt::WShowModal) setWindowModality(Qt::ApplicationModal); if (flags & Qt::WMouseNoMask) setAttribute(Qt::WA_MouseNoMask); if (flags & Qt::WGroupLeader) setAttribute(Qt::WA_GroupLeader); if (flags & Qt::WNoMousePropagation) setAttribute(Qt::WA_NoMousePropagation);#endif if ( type != Qt::Widget && type != Qt::Window && type != Qt::Dialog) setAttribute(Qt::WA_QuitOnClose, false); d->create_sys(window, initializeWindow, destroyOldWindow); d->setModal_sys(); if (!isWindow() && parentWidget() && parentWidget()->testAttribute(Qt::WA_DropSiteRegistered)) setAttribute(Qt::WA_DropSiteRegistered, true);#ifdef QT_EVAL extern void qt_eval_init_widget(QWidget *w); qt_eval_init_widget(this);#endif}/*! Destroys the widget. All this widget's children are deleted first. The application exits if this widget is the main widget.*/QWidget::~QWidget(){ Q_D(QWidget);#if defined (QT_CHECK_STATE) if (paintingActive()) qWarning("%s (%s): deleted while being painted", className(), name());#endif // force acceptDrops false before winId is destroyed. d->registerDropSite(false);#ifndef QT_NO_ACTION // remove all actions from this widget for (int i = 0; i < d->actions.size(); ++i) { QActionPrivate *apriv = d->actions.at(i)->d_func(); apriv->widgets.removeAll(this); } d->actions.clear();#endif#ifndef QT_NO_SHORTCUT // Remove all shortcuts grabbed by this // widget, unless application is closing if (!QApplicationPrivate::is_app_closing && testAttribute(Qt::WA_GrabbedShortcut)) qApp->d_func()->shortcutMap.removeShortcut(0, this, QKeySequence());#endif // delete layout while we still are a valid widget delete d->layout; // Remove myself focus list // ### Focus: maybe remove children aswell? QWidget *w = this; while (w->d_func()->focus_next != this) w = w->d_func()->focus_next; w->d_func()->focus_next = d_func()->focus_next; d_func()->focus_next = 0;#ifdef QT3_SUPPORT if (QApplicationPrivate::main_widget == this) { // reset main widget QApplicationPrivate::main_widget = 0; qApp->quit(); }#endif clearFocus(); if (isWindow() && isVisible() && winId()) hide(); // A parent widget must destroy all its children before destroying itself while (!d->children.isEmpty()) delete d->children.takeFirst(); QApplication::removePostedEvents(this); destroy(); // platform-dependent cleanup --QWidgetPrivate::instanceCounter; QEvent e(QEvent::Destroy); QCoreApplication::sendEvent(this, &e);}int QWidgetPrivate::instanceCounter = 0; // Current number of widget instancesint QWidgetPrivate::maxInstances = 0; // Maximum number of widget instancesvoid QWidgetPrivate::setWinId(WId id) // set widget identifier{ Q_Q(QWidget); if (mapper && data.winid) mapper->remove(data.winid); data.winid = id;#if defined(Q_WS_X11) hd = id; // X11: hd == ident#endif if (mapper && id) mapper->insert(data.winid, q);}void QWidgetPrivate::createTLExtra(){ if (!extra) createExtra(); if (!extra->topextra) { QTLWExtra* x = extra->topextra = new QTLWExtra; x->opacity = 255; x->icon = 0; x->iconPixmap = 0; x->fleft = x->fright = x->ftop = x->fbottom = 0; x->incw = x->inch = 0; x->basew = x->baseh = 0; x->normalGeometry = QRect(0,0,-1,-1);#if defined(Q_WS_X11) x->embedded = 0; x->parentWinId = 0; x->spont_unmapped = 0; x->dnd = 0; x->uspos = 0; x->ussize = 0;#endif x->savedFlags = 0;#if defined(Q_WS_QWS) x->inPaintTransaction = false; x->backingStore = 0;#if !defined(QT_NO_QWS_MANAGER) x->qwsManager = 0;#endif#endif createTLSysExtra(); }}/*! \internal Creates the widget extra data.*/void QWidgetPrivate::createExtra(){ if (!extra) { // if not exists extra = new QWExtra; extra->minw = extra->minh = 0; extra->maxw = extra->maxh = QWIDGETSIZE_MAX; extra->explicitMinSize = 0; extra->autoFillBackground = 0;#ifndef QT_NO_CURSOR extra->curs = 0;#endif extra->topextra = 0; extra->style = 0; extra->size_policy = QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); createSysExtra(); }}/*! \internal Deletes the widget extra data.*/void QWidgetPrivate::deleteExtra(){ if (extra) { // if exists#ifndef QT_NO_CURSOR delete extra->curs;#endif deleteSysExtra(); if (extra->topextra) { deleteTLSysExtra(); delete extra->topextra->icon; delete extra->topextra->iconPixmap;#if defined(Q_WS_QWS) && !defined(QT_NO_QWS_MANAGER) delete extra->topextra->qwsManager;#endif delete extra->topextra; } delete extra; // extra->xic destroyed in QWidget::destroy() extra = 0; }}/* Returns true if the background is inherited; otherwise returns false. Mainly used in the paintOnScreen case.*/bool QWidgetPrivate::isBackgroundInherited() const{ Q_Q(const QWidget); // windows do not inherit their background if (q->isWindow() || q->windowType() == Qt::SubWindow) return false; if (q->testAttribute(Qt::WA_NoSystemBackground) || q->testAttribute(Qt::WA_OpaquePaintEvent)) return false; const QPalette &pal = q->palette(); QPalette::ColorRole bg = q->backgroundRole(); QBrush brush = pal.brush(bg); // non opaque brushes leaves us no choice, we must inherit if (!q->autoFillBackground() || !brush.isOpaque()) return true; if (brush.style() == Qt::SolidPattern) { // the background is just a solid color. If there is no // propagated contents, then we claim as performance // optimization that it was not inheritet. This is the normal // case in standard Windows or Motif style. const QWidget *w = q->parentWidget(); if (!w->d_func()->isBackgroundInherited()) return false; } return true;}#ifndef Q_WS_MAC/* Returns true if there are widgets above this which overlap with \a rect, which is in parent's coordinate system (same as crect).*/bool QWidgetPrivate::isOverlapped(const QRect &rect) const{ Q_Q(const QWidget); const QWidget *w = q; QRect r = rect; while (w) { if (w->isWindow()) return false; QWidgetPrivate *pd = w->parentWidget()->d_func(); bool above = false; for (int i = 0; i < pd->children.size(); ++i) { QWidget *sibling = qobject_cast<QWidget *>(pd->children.at(i)); if (!sibling || !sibling->isVisible() || sibling->isWindow()) continue; if (!above) { above = (sibling == w); continue; } if (sibling->data->crect.intersects(r)) return true; } w = w->parentWidget(); r.translate(pd->data.crect.topLeft()); } return false;}#endifvoid QWidgetPrivate::setUpdatesEnabled_helper(bool enable){ Q_Q(QWidget); if (enable && !q->isWindow() && q->parentWidget() && !q->parentWidget()->updatesEnabled()) return; // nothing we can do if (enable != q->testAttribute(Qt::WA_UpdatesDisabled)) return; // nothing to do q->setAttribute(Qt::WA_UpdatesDisabled, !enable); if (enable) q->update(); Qt::WidgetAttribute attribute = enable ? Qt::WA_ForceUpdatesDisabled : Qt::WA_UpdatesDisabled; for (int i = 0; i < children.size(); ++i) { QWidget *w = qobject_cast<QWidget *>(children.at(i)); if (w && !w->isWindow() && !w->testAttribute(attribute)) w->d_func()->setUpdatesEnabled_helper(enable); }}void QWidgetPrivate::propagatePaletteChange(){ Q_Q(QWidget); QEvent pc(QEvent::PaletteChange); QApplication::sendEvent(q, &pc); if(!children.isEmpty()) { for(int i = 0; i < children.size(); ++i) { QWidget *w = qobject_cast<QWidget*>(children.at(i)); if (w && (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))) w->d_func()->resolvePalette(); } }#if defined(QT3_SUPPORT) q->paletteChange(q->palette()); // compatibility
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -