QML出现error: Expected token `numeric literal'

简单地在界面上画了两个Rectangle,运行出来,提示

H:1_helloQtQuickmain0.qml:114: error: Expected token `numeric literal'

最后发现:main.cpp中设置source时路径写错,应当有三个“/”,而不是两个: viewer.setSource(QUrl("qrc:///main.qml"));

//main.cpp

#include <QGuiApplication>
#include <QQuickView>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQuickView viewer;
    viewer.setResizeMode(QQuickView::SizeRootObjectToView);
    viewer.setSource(QUrl("qrc:///main.qml"));//正确的是三个
    viewer.show();

    return app.exec();
}
//main.qml

import QtQuick 2.0
import QtQuick.Window 2.12

Rectangle {
    600;
   height: 400;

   Rectangle {
       id: rect1;
        200;
       height: 100;
       anchors.top: parent.top;
       anchors.topMargin: 20;
       anchors.left: parent.left;
       anchors.leftMargin: 20;
       gradient: Gradient {
           GradientStop {position: 0.0; color: "red"}
           GradientStop {position: 0.5; color: "blue"}
       }
   }

   Rectangle {
        id: rect2;
         200;
        height: 100;
        anchors.top: rect1.top;
        anchors.left: rect1.right;
        anchors.leftMargin: 20;
        rotation: 90;
        gradient: Gradient {
            GradientStop {position: 0.0; color: "black"}
            GradientStop {position: 0.5; color: "blue"}
        }
   }

}


原文地址:https://www.cnblogs.com/BASE64/p/14469091.html