QT for Android 使用Item作为QML根对象

如果将main.qml中的Window替换为Rectangle,再将main.cpp修改成下面的样子;


 #include <QGuiApplication>

//#include <QQmlApplicationEngine>
#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();
    //QGuiApplication app(argc, argv);
    //QQmlApplicationEngine engine;
    //engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    //return app.exec();
}



 
 

这样,在main.xml文件中就可以这样写;

import QtQuick 2.2

//import QtQuick.Window 2.1
Rectangle{
    width:320;
    height: 480;
    color:"red";
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
}
 
 


你可能感兴趣的:(QT for Android 使用Item作为QML根对象)