qt - Qml 项目无法编译,错误 'Expected token ` ,'

标签 qt qml qt5

Qt Creator 3.1.2 基于 Qt 5.3.1(MSVC 2010,32 位)。项目已导入 QmlProject 2.0 这是我的程序,出现此错误。这个程序来自教程,它对他有用......所以我不确定那里有什么问题。

import QtQuick 2.0

Rectangle {
    id: rootTangle
    width: 360
    height: 360
    color: "red"
    hoverEnabled: true;


    Rectangle {
        id: blueRec
        color:  "blue"
        //opacity:    .50
        width: rootTangle.width/2
        height: rootTangle.width/6
        anchors.centerIn: rootTangle
        border.color: "black"
        border.width: 7
        rotation: 180
        radius: 20
        gradient: Gradient {
                GradientStop { position: 0.0; color: "#b0c5de" }
                GradientStop { position: 1.0; color: "blue" }
        }
    }

    Text {
        id: nazdarTxt
        anchors.centerIn: blueRec
        text: "Nazdar"
        clip: false
        visible: true
        font.family: "Times New Roman"
        font.bold: true
        //font.pixelSize: Math.round(blueRec.height/3)
        width: blueRec.width
        //wrapMode: Text.WordWrap

    }

    MouseArea {
        id: blueRecMouseArea
        hoverEnabled: true;
        onEntered: {
            blueRec.color: "brown"
        }

        anchors.rightMargin: 0
        anchors.bottomMargin: 0
        anchors.leftMargin: 1
        anchors.topMargin: 0
        anchors.fill: blueRec
        onClicked: {
            Qt.quit();

        }
    }

}

错误出现在第 46 行:onEntered: { blueRec.color: "棕色" }

最佳答案

问题出在颜色后面的冒号:

onEntered: {
    blueRec.color: "brown"
}

您应该更改为等号:

onEntered: {
    blueRec.color = "brown"
}

此外,矩形中没有 hoverEnabled,因此您需要将其删除或注释:

Rectangle {
    id: rootTangle
    width: 360
    height: 360
    color: "red"
    //hoverEnabled: true;

由于您已经为 blueRec 定义了渐变,因此更改其颜色没有任何效果,您应该更改渐变颜色:

onEntered: {
  blueRec.gradient.stops[0].color = "brown"
  blueRec.gradient.stops[1].color = "white"
}

关于qt - Qml 项目无法编译,错误 'Expected token ` ,',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259669/

相关文章:

qt - 如何在 ShapePath 中使用 LinearGradient?

c++ - 您如何注册相互依赖的 Qt QML Singleton?

qt - 如何将 ListView 滚动到当前项目?

qt - 是否有任何 Qt SQLite 插件可以通过 VFS 将数据库存储在 RAM 中(用于从 Qt 资源文件加载数据库)?

c++ - 如何在Qt5中的QToolButton上设置GIF图像

c++ - 当宏用作变量名时,有什么方法可以跳过宏替换(在预处理期间)?

c++ - 在 QT 中使用 LTI-Lib

linux - 应用程序启动器和输入应用程序名称并从终端运行它有什么区别?

c++ - Qt on RaspberryPi - QXmlAttributes 隐式声明

c++ - 当对话框中的任何小部件发出信号时,是否可以调用插槽?