QML Screen(屏幕)组件

Screen 是 Qt Quick 提供的一个类型,用于访问屏幕相关信息(如分辨率、DPI、方向等)。它通常与 Window 或 ApplicationWindow 一起使用来管理多屏幕应用。

属性

属性类型描述示例
namestring屏幕名称"HDMI-1"
manufacturerstring屏幕制造商"Samsung"
modelstring屏幕型号"U28E590"
serialNumberstring屏幕序列号"XYZ123"
widthint屏幕宽度(像素)1920
heightint屏幕高度(像素)1080
desktopAvailableWidthint可用宽度(减去任务栏等)1920
desktopAvailableHeightint可用高度(减去任务栏等)1040
pixelDensityreal每毫米像素数(物理密度)3.4
logicalPixelDensityreal逻辑像素密度(考虑缩放)2.0
primaryOrientationenum主方向(Portrait/Landscape等)Qt.LandscapeOrientation
orientationenum当前方向Qt.PortraitOrientation
orientationUpdateMaskenum监听哪些方向变化Qt.PrimaryOrientation
virtualXint虚拟桌面中的X位置0
virtualYint虚拟桌面中的Y位置1080 (第二屏幕)
primarybool是否是主屏幕true

方法

方法参数返回值描述
setOrientation()orientation (enum)void强制设置屏幕方向
angleBetween()ab (两个enum方向)int计算两个方向间的角度差

方向枚举值

qml

Qt.PrimaryOrientation
Qt.PortraitOrientation
Qt.LandscapeOrientation
Qt.InvertedPortraitOrientation
Qt.InvertedLandscapeOrientation

信号

信号参数描述
orientationChanged()orientation (enum)屏幕方向改变时触发
geometryChanged()-屏幕尺寸或位置变化时触发
physicalDotsPerInchChanged()dpi (real)物理DPI变化时触发
logicalDotsPerInchChanged()dpi (real)逻辑DPI变化时触发
primaryChanged()isPrimary (bool)主屏幕状态变化时触发

基础使用示例

qml

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    visible: true
    title: "Screen Info Demo"

    // 获取主屏幕引用
    property var primaryScreen: Screen.primaryScreen

    Text {
        anchors.centerIn: parent
        text: `
            屏幕名称: ${primaryScreen.name}
            分辨率: ${primaryScreen.width}x${primaryScreen.height}
            物理DPI: ${primaryScreen.pixelDensity * 25.4} (${primaryScreen.pixelDensity} px/mm)
            当前方向: ${getOrientationName(primaryScreen.orientation)}
            ${primaryScreen.primary ? "主屏幕" : "副屏幕"}
        `
        wrapMode: Text.Wrap
    }

    function getOrientationName(ori) {
        const map = {
            0: "Primary",
            1: "Portrait",
            2: "Landscape",
            3: "InvertedPortrait",
            4: "InvertedLandscape"
        }
        return map[ori] || "Unknown"
    }
}

多屏幕管理示例

qml

// 获取所有屏幕列表
property var screens: Screen.screens

// 在第二屏幕创建窗口
Component.onCompleted: {
    if (screens.length > 1) {
        secondWindow.screen = screens[1]
        secondWindow.show()
    }
}

Window {
    id: secondWindow
    width: 800
    height: 600
    title: "Second Screen Window"
}

常见应用场景

  1. 响应式布局

    qml

    GridView {
        cellWidth: Screen.desktopAvailableWidth / 4
        cellHeight: Screen.desktopAvailableHeight / 4
    }
  2. 多屏幕应用

    qml

    Window {
        screen: Qt.application.screens[1] // 显示在第二个屏幕
    }
  3. 方向检测

    qml

    Screen.onOrientationChanged: {
        if (orientation === Qt.LandscapeOrientation) {
            console.log("横向模式")
        }
    }
  4. DPI适配

    qml

    Text {
        font.pixelSize: 12 * Screen.logicalPixelDensity
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

byxdaz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值