QML编码约定

QML中的国际化:

QML使用以下函数来将字符串标记为可翻译的

  1. qsTr()
  2. qsTranslate()
  3. qsTrld()
  4. QT_TR_NOOP()
  5. QT_TRANSLATE_NOOP()
  6. QT_TRID_NOOP

最常用的还是qsTr()

string qsTr(string sourceText, string disambiguation, int n) 

  • 返回 sourceText 的翻译版本,可以选择基于消除歧义字符串和包含复数的字符串的值 n ;否则,如果没有合适的翻译字符串可用,则返回 sourceText 本身。

    如果在同一翻译上下文中以不同的角色使用相同的 sourceText,则可能会传入额外的标识字符串以消除歧义

1.正常使用: 

Text {
        id: text1
        text: qsTr("text")
    }

 2.消除歧义:

当两个词语有相近的意思时,使用qsTr()的第二个参数来区分相同的文本 

Text {
    id: txt1;
    text: qsTr("Back", "not front");//将Back和 not front区分
}

3.使用arg()

Text {
        id: text1
        text: qsTr("%1,%2").arg(1).arg(2);
    }

4.使用%lx使数字本地化

可能国家和国家之间的表式方法不一样,可以使用%lx来使数字本地化

Text {
    text: qsTr("%L1").arg(total)
}

如果是数字“4321.56”(四千三百二十一点五十六);使用英语区域设置(区域设置)时,输出为“4,321.56”;使用德语区域设置时,输出为“4.321,56”。

5.国际化日期、时间和货币

Qt.locale返回一个Locale 对象,其中包含有关区域设置的各种信息。特别是,Locale.name 属性包含当前区域设置的语言和国家/地区信息。可以按原样使用该值,也可以分析该值以确定当前区域设置的相应内容。

  • 使用Date()获取当前时间

获取本地化的时间和日期:

Text {
        id: text1
        text: qsTr("Data:%1").arg(Date().toLocaleString(Qt.locale()))
    }

扫描二维码关注公众号,回复: 14776592 查看本文章

 QML编码规定:

 QMl对象属性构建顺序:

  1. id
  2. 属性声明
  3. 信号声明
  4. JavaScript
  5. 对象属性
  6. 子对象
  7. 状态
  8. 状态切换
Rectangle {
    id: photo                                               // id on the first line makes 

    property bool thumbnail: false                          // property declarations
    property alias image: photoImage.source

    signal clicked                                          // signal declarations

    function doSomething(x)                                 // javascript functions
    {
        return x + photoImage.width
    }

    color: "gray"                                           // object properties
    x: 20                                                   // try to group related properties together
    y: 20
    height: 150
    width: {                                                // large bindings
        if (photoImage.width > 200) {
            photoImage.width;
        } else {
            200;
        }
    }

    Rectangle {                                             // child objects
        id: border
        anchors.centerIn: parent; color: "white"

        Image {
            id: photoImage
            anchors.centerIn: parent
        }
    }

    states: State {                                         // states
        name: "selected"
        PropertyChanges { target: border; color: "red" }
    }

    transitions: Transition {                               // transitions
        from: ""
        to: "selected"
        ColorAnimation { target: border; duration: 200 }
    }
}

分组属性:

一些相同元素的属性可以进行合并 

Text{
        font.bold: true
        font.family: "黑体"
        font.pixelSize: 20
        font.underline: true
    }

 可以合并为:

Text{
    font{ bold: true;underline: true;pixelSize: 20;family: "黑体" }
    }

私有属性:

QML和JavaScript中并没有C++中强制私有属性,但需要隐藏那些私有属性的话,使用__开头

Rectangle{
        id:rect1
        property real __peo: width+hieght//使用__开头
        width: 100
        height: 100
    }

JavaScript代码:

  1. 少量代码的话,直接内联
  2. 一个函数的话,把函数放到外部声明
  3. 多个函数的话,使用文件的方式存储再调用

猜你喜欢

转载自blog.csdn.net/qq_45303986/article/details/129343499
QML