Qt 警告:QQmlComponent:组件未准备好 [英] Qt Warning: QQmlComponent: Component is not ready
问题描述
使用动态创建的项目时是否有可能获得 Qt 错误消息?
Is it possible to get Qt error messages when using dynamically created items?
我已经安装了一个消息处理程序来在运行时捕获 Qt 输出:
I've installed a message handler to capture Qt output at run time:
qInstallMessageHandler( myMessageOutput );
我将一个基本的 qml 文件加载到 QQuickView 中,并且工作正常.如果 qml 源中有错误,它们会显示在我的日志中.如果我动态创建项目并且它们包含错误,它会失败并且没有任何消息.
I load a basic qml file into a QQuickView and it works fine. If there are errors in the qml source they're displayed in my log. If I dynamically create items and they contain errors it fails without any message.
我像这样创建动态对象:
I create the dynamic objects like this:
var component = Qt.createComponent( "config.qml" );
var dlg = component.createObject( parentId, {} );
我收到的唯一错误如下:
The only error I receive is the following:
'qml\qqmlcomponent.cpp':845 function: 'QObject* QQmlComponentPrivate::beginCreate(QQmlContextData*)'|Qt Warning: QQmlComponent: Component is not ready
此错误是针对它尝试加载的 qml 中的任何类型的问题编写的.
This error is written for any kind of problem in the qml it's trying to load.
推荐答案
您应该阅读并遵循 文档.
您没有检查的是在调用
必须等于 component.createObject
之前,component.statusComponent.Ready
.
What you do not check is that component.status
must be equal to Component.Ready
before calling to component.createObject
.
如果文件以某种方式加载失败,因为它没有正确解析,component.status
将等于 Component.Error
,你应该调用 errorString()
得到更多信息.
If the file somehow failed to load, as it does not parse correctly, component.status
will be equal to Component.Error
, and you should call errorString()
to get more information.
var component = Qt.createComponent( "config.qml" );
if( component.status != Component.Ready )
{
if( component.status == Component.Error )
console.debug("Error:"+ component.errorString() );
return; // or maybe throw
}
var dlg = component.createObject( parentId, {} );
无论如何,您应该始终在调用 createObject()
之前声明 component.status == Component.Ready
.
Anyway you should always assert component.status == Component.Ready
before calling createObject()
.
这篇关于Qt 警告:QQmlComponent:组件未准备好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!