#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include<QTextCodec>
#include <QQuickView>
#include <QtQml>
QQuickView *view;
int main(int argc, char *argv[])
{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
myapplication app(argc, argv);
view = new QQuickView;
view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view->show();
// QQmlApplicationEngine engine;
// engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject::connect(view->engine(), SIGNAL(quit()), view, SLOT(close()));
return app.exec();
}
上面是采用QQuickView的方式(Rectangle加载方式)。这种加载一切正常,启动过程中没有出现白屏现象(此时退出Qt程序需要将QQuickView close掉才行,执行Qt.quit() 会触发QQmlEngine的quit()信号)
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include<QTextCodec>
#include <QQuickView>
#include <QtQml>
QQuickView *view;
int main(int argc, char *argv[])
{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
myapplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
上面的是采用的是QQmlApplicationEngine方式(window加载方式),启动时会先出现白屏,然后才会出现想要的界面。
两种不同的方式实现的效果的细微差异–查看帮助文档个人感觉是由于,QQuickView只是提供一个界面,然后可以在界面添加各种组件,所以显示的时候就是qml加载成功的时候,没成功之前是就是黑的(屏幕原先是什么样还是什么样)。
而QQuickWindow(window)却是为我们提供了一个基于graphical QML scene的窗口,主要是方便scene中的组件(QQuickItems)之间互相interact(交互)。所以在使用的时候,qml还没有加载完毕,scene已经存在,就是我们看到的一片白色。。。