【问题标题】:Qml text element syntax errorQml 文本元素语法错误
【发布时间】:2015-03-25 03:22:43
【问题描述】:

我收到以下内容-

file:///C:/Qt/5.4/mingw491_32/Design1.qml:9:1: Syntax error 
     Text {

     ^

这是我非常简单的代码,但我无法让它在 qmlviewer 中运行

import QtQuick 2.0

Rectangle {
    id: page
    width: 500; height: 200
    color: "lightgray"
}

Text {
    id: Text1
    text: "Hello World!"
    y: 30
    anchors.horizontalCenter: page.horizontalCenter
    font.pointSize: 24; font.bold: true
}

【问题讨论】:

    标签: qt syntax-error qml qtquick2


    【解决方案1】:

    ids 的 id 名称开头不能有大写字母。

    Text1 更改为text1,它应该可以工作。

    【讨论】:

    • 感谢 phyatt,但我仍然有同样的语法错误
    【解决方案2】:

    我发现我的问题是我忘了让 txt{} 成为 Rectangle 的子元素,因为只能有一个。

    import QtQuick 1.0
    
    Rectangle {
        id: page
        width: 500; height: 200
        color: "lightgray"
    
        Text {   // Indented
            id: text1
            text: "Hello World!"
            y: 30
            anchors.horizontalCenter: page.horizontalCenter
            font.pointSize: 24; font.bold: true
        }
    }
    

    【讨论】:

    • 与缩进无关。这是因为Text 必须成为Rectanglechild,QML 文件只能有一个根对象。请更正您对答案的描述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多