簡單地在界面上畫了兩個Rectangle,運行出來,提示
H:\01_helloQtQuick\main0.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 { width: 600; height: 400; Rectangle { id: rect1; width: 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; width: 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"} } } }
