Qt源码分析--QWidget(1)--Window functions

本文详细介绍了Qt库中关于窗口管理的几个关键函数:show()用于显示窗口及其子窗口,依据平台默认行为选择全屏或最大化;hide()用于隐藏窗口;raise()使窗口置于顶层;lower()则将其置于底层。这些函数在窗口显示和层级控制中起着重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.void show();

/*!
    Shows the widget and its child widgets.
    This is equivalent to calling showFullScreen(), showMaximized(), or setVisible(true),
    depending on the platform's default behavior for the window flags.
     \sa raise(), showEvent(), hide(), setVisible(), showMinimized(), showMaximized(),
    showNormal(), isVisible(), windowFlags()
*/
void QWidget::show()
{
    Qt::WindowState defaultState = QGuiApplicationPrivate::platformIntegration()->defaultWindowState(data->window_flags);
    if (defaultState == Qt::WindowFullScreen)
        showFullScreen();
    else if (defaultState == Qt::WindowMaximized)
        showMaximized();
    else
        setVisible(true); // Don't call showNormal() as not to clobber Qt::Window(Max/Min)imized
}

/*!
    Shows the widget in full-screen mode.
    Calling this function only affects \l{isWindow()}{windows}.
    To return from full-screen mode, call showNormal().
    Full-screen mode works fine under Windows, but has certain
    problems under X. These problems are due to limitations of the
    ICCCM protocol that specifies the communication between X11
    clients and the window manager. ICCCM simply does not understand
    the concept of non-decorated full-screen windows. Therefore, the
    best we can do is to request a borderless window and place and
    resize it to fill the entire screen. Depending on the window
    manager, this may or may not work. The borderless window is
    requested using MOTIF hints, which are at least partially
    supported by virtually all modern window managers.
    An alternative would be to bypass the window manager entirely and
    create a window with the Qt::X11BypassWindowManagerHint flag. This
    has other severe problems though, like totally broken keyboard focus
    and very strange effects on desktop changes or when the user raises
    other windows.
    X11 window managers that follow modern post-ICCCM specifications
    support full-screen mode properly.
    \sa showNormal(), showMaximized(), show(), hide(), isVisible()
*/
void QWidget::showFullScreen()
{
    ensurePolished();
    setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
                   | Qt::WindowFullScreen);
    setVisible(true);
#if !defined Q_OS_QNX // On QNX this window will be activated anyway from libscreen
                      // activating it here before libscreen activates it causes problems
    activateWindow();
#endif
}

根据窗口状态不同, 做不同的展示。

2.void hide();

/*!
    Hides the widget. This function is equivalent to
    setVisible(false).
    \note If you are working with QDialog or its subclasses and you invoke
    the show() function after this function, the dialog will be displayed in
    its original position.
    \sa hideEvent(), isHidden(), show(), setVisible(), isVisible(), close()
*/
void QWidget::hide()
{
    setVisible(false);
}

void QWidget::setVisible(bool visible)
{
    if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden) == !visible)
        return;
    // Remember that setVisible was called explicitly
    setAttribute(Qt::WA_WState_ExplicitShowHide);
    Q_D(QWidget);
    d->setVisible(visible);
}

void QWidgetPrivate::setVisible(bool visible)
{
    Q_Q(QWidget);
    if (visible) { // show
        // Designer uses a trick to make grabWidget work without showing
        if (!q->isWindow() && q->parentWidget() && q->parentWidget()->isVisible()
            && !q->parentWidget()->testAttribute(Qt::WA_WState_Created))
            q->parentWidget()->window()->d_func()->createRecursively();
        //create toplevels but not children of non-visible parents
        QWidget *pw = q->parentWidget();
        if (!q->testAttribute(Qt::WA_WState_Created)
            && (q->isWindow() || pw->testAttribute(Qt::WA_WState_Created))) {
            q->create();
        }
        bool wasResized = q->testAttribute(Qt::WA_Resized);
        Qt::WindowStates initialWindowState = q->windowState();
        // polish if necessary
        q->ensurePolished();
        // whether we need to inform the parent widget immediately
        bool needUpdateGeometry = !q->isWindow() && q->testAttribute(Qt::WA_WState_Hidden);
        // we are no longer hidden
        q->setAttribute(Qt::WA_WState_Hidden, false);
        if (needUpdateGeometry)
            updateGeometry_helper(true);
        // activate our layout before we and our children become visible
        if (layout)
            layout->activate();
        if (!q->isWindow()) {
            QWidget *parent = q->parentWidget();
            while (parent && parent->isVisible() && parent->d_func()->layout  && !parent->data->in_show) {
                parent->d_func()->layout->activate();
                if (parent->isWindow())
                    break;
                parent = parent->parentWidget();
            }
            if (parent)
                parent->d_func()->setDirtyOpaqueRegion();
        }
        // adjust size if necessary
        if (!wasResized
            && (q->isWindow() || !q->parentWidget()->d_func()->layout))  {
            if (q->isWindow()) {
                q->adjustSize();
                if (q->windowState() != initialWindowState)
                    q->setWindowState(initialWindowState);
            } else {
                q->adjustSize();
            }
            q->setAttribute(Qt::WA_Resized, false);
        }
        q->setAttribute(Qt::WA_KeyboardFocusChange, false);
        if (q->isWindow() || q->parentWidget()->isVisible()) {
            show_helper();
            qApp->d_func()->sendSyntheticEnterLeave(q);
        }
        QEvent showToParentEvent(QEvent::ShowToParent);
        QCoreApplication::sendEvent(q, &showToParentEvent);
    } else { // hide
        if (QApplicationPrivate::hidden_focus_widget == q)
            QApplicationPrivate::hidden_focus_widget = nullptr;
        // hw: The test on getOpaqueRegion() needs to be more intelligent
        // currently it doesn't work if the widget is hidden (the region will
        // be clipped). The real check should be testing the cached region
        // (and dirty flag) directly.
        if (!q->isWindow() && q->parentWidget()) // && !d->getOpaqueRegion().isEmpty())
            q->parentWidget()->d_func()->setDirtyOpaqueRegion();
        if (!q->testAttribute(Qt::WA_WState_Hidden)) {
            q->setAttribute(Qt::WA_WState_Hidden);
            if (q->testAttribute(Qt::WA_WState_Created))
                hide_helper();
        }
        // invalidate layout similar to updateGeometry()
        if (!q->isWindow() && q->parentWidget()) {
            if (q->parentWidget()->d_func()->layout)
                q->parentWidget()->d_func()->layout->invalidate();
            else if (q->parentWidget()->isVisible())
                QCoreApplication::postEvent(q->parentWidget(), new QEvent(QEvent::LayoutRequest));
        }
        QEvent hideToParentEvent(QEvent::HideToParent);
        QCoreApplication::sendEvent(q, &hideToParentEvent);
    }
}

void QWidgetPrivate::hide_helper()
{
    Q_Q(QWidget);
    bool isEmbedded = false;
#if QT_CONFIG(graphicsview)
    isEmbedded = q->isWindow() && !bypassGraphicsProxyWidget(q) && nearestGraphicsProxyWidget(q->parentWidget()) != nullptr;
#else
    Q_UNUSED(isEmbedded);
#endif
    if (!isEmbedded && (q->windowType() == Qt::Popup))
        qApp->d_func()->closePopup(q);
    q->setAttribute(Qt::WA_Mapped, false);
    hide_sys();
    bool wasVisible = q->testAttribute(Qt::WA_WState_Visible);
    if (wasVisible) {
        q->setAttribute(Qt::WA_WState_Visible, false);
    }
    QHideEvent hideEvent;
    QCoreApplication::sendEvent(q, &hideEvent);
    hideChildren(false);
    // next bit tries to move the focus if the focus widget is now
    // hidden.
    if (wasVisible) {
        qApp->d_func()->sendSyntheticEnterLeave(q);
        QWidget *fw = QApplication::focusWidget();
        while (fw &&  !fw->isWindow()) {
            if (fw == q) {
                q->focusNextPrevChild(true);
                break;
            }
            fw = fw->parentWidget();
        }
    }
    if (QWidgetRepaintManager *repaintManager = maybeRepaintManager())
        repaintManager->removeDirtyWidget(q);
#ifndef QT_NO_ACCESSIBILITY
    if (wasVisible) {
        QAccessibleEvent event(q, QAccessible::ObjectHide);
        QAccessible::updateAccessibility(&event);
    }
#endif
}

将窗口从刷新列表中清除,隐藏窗口。

3.void raise();

void QWidget::raise()
{
    Q_D(QWidget);
    if (!isWindow()) {
        QWidget *p = parentWidget();
        const int parentChildCount = p->d_func()->children.size();
        if (parentChildCount < 2)
            return;
        const int from = p->d_func()->children.indexOf(this);
        Q_ASSERT(from >= 0);
        // Do nothing if the widget is already in correct stacking order _and_ created.
        if (from != parentChildCount -1)
            p->d_func()->children.move(from, parentChildCount - 1);
        if (!testAttribute(Qt::WA_WState_Created) && p->testAttribute(Qt::WA_WState_Created))
            create();
        else if (from == parentChildCount - 1)
            return;
        QRegion region(rect());
        d->subtractOpaqueSiblings(region);
        d->invalidateBackingStore(region);
    }
    if (testAttribute(Qt::WA_WState_Created))
        d->raise_sys();
    if (d->extra && d->extra->hasWindowContainer)
        QWindowContainer::parentWasRaised(this);
    QEvent e(QEvent::ZOrderChange);
    QCoreApplication::sendEvent(this, &e);
}

void QWidgetPrivate::raise_sys()
{
    Q_Q(QWidget);
    if (q->isWindow() || q->testAttribute(Qt::WA_NativeWindow)) {
        q->windowHandle()->raise();
    } else if (renderToTexture) {
        if (QWidget *p = q->parentWidget()) {
            setDirtyOpaqueRegion();
            p->d_func()->invalidateBackingStore(effectiveRectFor(q->geometry()));
        }
    }
}

void QWindow::raise()
{
    Q_D(QWindow);
    d->updateSiblingPosition(QWindowPrivate::PositionTop);
    if (d->platformWindow)
        d->platformWindow->raise();
}

4.void lower();

/*!
    Lower the window in the windowing system.
    Requests that the window be lowered to appear below other windows.
*/
void QWindow::lower()
{
    Q_D(QWindow);
    d->updateSiblingPosition(QWindowPrivate::PositionBottom);
    if (d->platformWindow)
        d->platformWindow->lower();
}

5.bool close();

/*!
    Close the window.
    This closes the window, effectively calling destroy(), and potentially
    quitting the application. Returns \c true on success, false if it has a parent
    window (in which case the top level window should be closed instead).
    \sa destroy(), QGuiApplication::quitOnLastWindowClosed(), closeEvent()
*/
bool QWindow::close()
{
    Q_D(QWindow);
    // Do not close non top level windows
    if (parent())
        return false;
    if (!d->platformWindow)
        return true;
    return d->platformWindow->close();
}

关于 `WA_MFORENSICS_070700` 的具体定义和技术文档,在现有的引用材料中并未提及相关内容。然而,基于 Qt 属性命名的一般规律以及常见的错误处理方式,可以推测该属性可能属于某种特定场景下的调试标志或内部机制。 以下是对此问题的深入分析: ### 可能的技术背景 1. **QtWindow Attributes 的通用特性** 在 Qt 中,窗口属性(Window Attributes)用于控制窗口的行为和外观。例如: - `Qt::WA_DeleteOnClose` 表示当窗口被关闭时自动删除对象[^1]。 - `Qt::WA_TransparentForMouseEvents` 使控件忽略鼠标事件并将其传递给父级控件[^3]。 - `Qt::WA_OpaquePaintEvent` 控制是否在绘制时不擦除背景[^4]。 如果存在名为 `WA_MFORENSICS_070700` 的属性,则它可能是某个特殊用途的标记,通常仅限于内部使用或者高级调试功能。 2. **潜在含义** 基于其名称结构,“MForensics” 或许暗示与性能监控、日志记录或诊断工具有关。“070700” 则可能是版本号或其他编码形式的一部分。这种类型的属性一般不会公开暴露给开发者,而是供框架维护者排查复杂问题所用。 3. **常见解决路径** 针对未明确说明的属性或错误码,建议采取以下措施: - 查阅官方发行版变更日志 (Changelog),寻找新增加或修改过的相关条目; - 访问社区论坛如 Stack Overflow 或 Qt 官方邮件列表提问; - 使用源代码浏览器定位实际实现位置及其上下文依赖关系。 ### 示例代码片段 假设我们需要验证某未知 flag 是否影响应用程序行为,可以通过简单测试来确认效果: ```cpp #include <QApplication> #include <QWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; // 尝试设置假定存在的自定义属性 #ifdef WA_MFORENSICS_070700 window.setAttribute(WA_MFORENSICS_070700); #endif window.show(); return app.exec(); } ``` 注意:上述例子中的宏定义需替换为你真正感兴趣的常量名,并确保编译环境支持相应选项。 ### 结论 由于缺乏直接关联的信息来源,目前无法确切给出 `WA_MFORENSICS_070700` 的正式解释及对应修复策略。不过通过合理猜测加上实验手段往往可以帮助我们接近真相。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天进步2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值