(Qt 5.12.2(mingw 7.3 32 位)Windows 8 64 位)
下一个代码适用于 DEBUG,但在 RELEASE 模式下失败:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDir>
#include <QFile>
#include <QResource>
#include <QByteArray>
#include <iostream>
void exploreQrcDir( const QString& path )
{
QDir qrcDir( ":/layouts" );
std::cout << ":/layouts : " << std::endl;
for ( const QString& item : qrcDir.entryList())
{
std::cout << item.toStdString() << std::endl;
}
for ( const QFileInfo& item : qrcDir.entryInfoList())
{
std::cout << item.absoluteFilePath().toStdString() << ";" << item.isReadable() << std::endl;
}
}
QByteArray resourceData( const QString& path )
{
QResource qmlFile( path );
std::cout << "Resource is valid = " << qmlFile.isValid()
<< " , is compressed = " << qmlFile.isCompressed()
<< " , size = " << qmlFile.size() << std::endl;
QByteArray data = (const char*)( qmlFile.data() );
std::cout << "Resource data = " << qmlFile.data() << std::endl;
std::cout << "ByteArray = " << data.toStdString() << std::endl;
return data;
}
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
exploreQrcDir( ":/layouts" );
const QString qmlDataSource( "qrc:/layouts/layouts.qml" );
QQmlApplicationEngine engine;
engine.loadData( resourceData( ":/layouts/layouts.qml" ) ); // debug version - ok
// release version - qml window not open
// engine.load( qmlDataSource ); // ok
// engine.load( QUrl( qmlDataSource ) ); // ok
return app.exec();
}
在调试模式下一切正常。在 RLEASE 模式下 - 任务管理器任务列表中没有错误、没有应用程序窗口和应用程序进程。
我可以在发布模式下从应用程序资源加载 QML 文件吗?