Qt Quick 示例 - 窗口和屏幕

本例演示 QML 中的 Window 和 Screen 类型。

Window 和 Screen演示了如何

  • 在 QML 中创建窗口
  • 控制窗口visibility
  • 在应用程序启动时显示闪屏
  • 访问窗口的属性Screen

它还演示了如何将 QML 打包成资源并提供图标,以创建独立的 QML 桌面应用程序。

运行示例

要从 Qt Creator,打开Welcome 模式,并从Examples 选择示例。更多信息,请参阅Qt Creator: Tutorial:构建并运行

窗口实现

可以使用Qt.SplashScreen 标志创建闪屏,闪屏应为ApplicationModal ,以防止与主窗口交互。如果闪屏窗口也是透明的,并显示部分透明的图像,那么它看起来就像一个异形窗口。

Window {
    id: splash
    color: "transparent"
    title: "Splash Window"
    modality: Qt.ApplicationModal
    flags: Qt.SplashScreen
    property int timeoutInterval: 2000
    signal timeout

在本例中,Timer 将自动关闭闪屏,但在实际应用中,可能需要连接到应用程序逻辑的信号,以便在初始化完成后隐藏闪屏。

Timer {
    interval: splash.timeoutInterval; running: splash.visible; repeat: false
    onTriggered: splash.exit()
}

本例中的主窗口是控制窗口,其中的一些按钮和复选框用于控制和反馈辅助窗口的状态。每个复选框都绑定了其显示状态的属性,同时还绑定了一个 onClicked 处理程序来改变状态。这是创建双向绑定同时避免绑定循环的典型模式。

CheckBox {
    text: "Windowed"
    height: showButton.height
    width: col.cellWidth
    Binding on checked { value: root.testWindow.visibility === Window.Windowed }
    onClicked: root.testWindow.visibility = Window.Windowed
}

Screen 对于需要在屏幕方向改变时旋转某些内容、在屏幕上定位窗口或将实际单位转换为逻辑像素单位的应用程序来说,CurrentScreen.qml 具有几个非常有用的属性。CurrentScreen.qml(在 window.qml 中内嵌显示,也可使用qml 工具单独运行)只是显示属性值,而闪屏则使用这些属性值将窗口居中显示在屏幕上。

    x: (Screen.width - splashImage.width) / 2
    y: (Screen.height - splashImage.height) / 2

如果Window 嵌套在Item 或另一个窗口内,则内部窗口将成为外部窗口的瞬态窗口(更多解释请参阅Window )。但是,如果您想创建多个顶层窗口作为不相关的对等窗口,您可以像本例一样,在一个非可视化的QtObject 根项目内创建它们。

示例项目 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.