是否可以知道QML控件的默认大小?

问题描述 投票:0回答:1

是否可以知道 QML 控件的默认大小(高度)?像

QWidget::sizeHint()
...

之类的东西

我想将

implicitHeight
TextField
设置为 8mm,这在桌面上没问题,但在 Android 上 8mm 还不够,所以我想要这样的东西:

implicitHeight: Math.max( minimumCtrlHeight (8mm), defaultHeight )

也许可以用另一种方法来完成?谢谢。

也许在 QML 中可以使用类似

#ifdef
的东西在桌面上设置
implicitHeight
,但不能在移动设备上设置?

qml qt6
1个回答
1
投票

你可以这样做:

import QtQuick 2.15
import QtQuick.Controls 2.15

Rectangle {
    id: root
    anchors.centerIn: parent;
    function preffredButtonHeight(parent_: Item) {
        if (Qt.platform.os == "android" || 
               Qt.platform.os == "wasm" || 
               Qt.platform.os == "ios") {
            return Math.max(parent_.height / 25, 88, implicitHeight);
        } else {
            return Math.max(parent_.height / 25, 50, implicitHeight);
        }
    }
    Button {
        anchors.centerIn: parent;
        text: "platform is: " + Qt.platform.os
        height: preffredButtonHeight(parent)
    }
}

这可以更声明性地完成,尽管我认为这会更混乱。 你也可以用 C++ 实现那个 JS 函数,这就是我要做的。

请注意,如果您不想使用

Screen.desktopAvailableHeight
或同时使用它们,可以使用
parent
...

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.2

Rectangle {
    id: root
    anchors.centerIn: parent;
    function preffredButtonHeight(parent_: Item) {
        if (Qt.platform.os == "andriod" || "wasm" || "ios") {
            return Math.max(Screen.desktopAvailableHeight / 25, 88, implicitHeight);
        } else {
            return Math.max(Screen.desktopAvailableHeight / 25, 50, implicitHeight);
        }
    }
    Button {
        anchors.centerIn: parent;
        text: "platform is: " + Qt.platform.os
        height: preffredButtonHeight(parent)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.