Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QML跨平台设计的问题:文本大小_Qt_Fonts_Cross Platform_Qml - Fatal编程技术网

Qt QML跨平台设计的问题:文本大小

Qt QML跨平台设计的问题:文本大小,qt,fonts,cross-platform,qml,Qt,Fonts,Cross Platform,Qml,不同的操作系统(试过Windows7和Ubuntu13.10)有不同的字体宽度和高度,我遇到了麻烦。我试着设置pixelSize和pointSize,结果是Arial和Courier字体文本的大小都不同(Windows字体通常更大) 以下是一个例子: Rectangle { width: 312 height: 44 Text { id: text_1 text: qsTr("1234567890123456789012345678901

不同的操作系统(试过Windows7和Ubuntu13.10)有不同的字体宽度和高度,我遇到了麻烦。我试着设置pixelSize和pointSize,结果是Arial和Courier字体文本的大小都不同(Windows字体通常更大)

以下是一个例子:

Rectangle {
    width: 312
    height: 44

    Text {
        id: text_1
        text: qsTr("1234567890123456789012345678901234567890")
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right

        font.family: "Courier"
        font.pointSize: 10
    }
    Text {
        id: text_2
        text: qsTr("123456789012345678901234567890")
        anchors.top: text_1.bottom
        anchors.left: parent.left
        anchors.right: parent.right

        font.family: "Courier"
        font.pointSize: 10
    }
    Text {
        id: text_3
        text: qsTr("12345678901234567890123456789012345")
        anchors.top: text_2.bottom
        anchors.left: parent.left
        anchors.right: parent.right

        font.family: "Courier"
        font.pointSize: 10
    }
}
这个矩形的宽度和高度是通过反复试验来定义的,以适应Ubuntu中的3个文本。请参见下面的屏幕截图:

在我的应用程序中,我想要一个文本对象(带有Courier字体)来填充其父对象的宽度(文本是固定的)。在另一个矩形中,我希望文本对象(不止一个,就像上面的例子一样,用Arial字体锚定)填充父对象的高度

目前,我正在寻找一种方法来动态设置父对象的宽度和高度,根据固定的文本内容进行计算,但我不能动摇这样的感觉:必须有一种更简单的方法来实现这一点


任何提示都非常感谢。

在这种情况下选择父级高度/宽度是个坏主意-正如您所看到的,它不便于携带;如果您决定以后更改字体,是否要重新计算所有内容?这是您希望委托给QML的内容

我建议使用如下方法:

import QtQuick.Layouts 1.1 // Necessary to use ColumnLayout

ColumnLayout {   // A Single Column
    Text {
        id: text_1
        text: qsTr("1234567890123456789012345678901234567890")

        // No need for anchoring ! You may want to use Layout.fillWidth: true in some cases

        font.family: "Courier"
        font.pointSize: 10
    }
    Text {
        id: text_2
        text: qsTr("123456789012345678901234567890")

        font.family: "Courier"
        font.pointSize: 10
    }
    Text {
        id: text_3
        text: qsTr("12345678901234567890123456789012345")

        font.family: "Courier"
        font.pointSize: 10
    }
}