Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/core/qmlengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,29 @@
const QVariantMap &properties)
{
auto context = qmlContext(parent);
auto obj = component.beginCreate(context);
if (!properties.isEmpty()) {
component.setInitialProperties(obj, properties);
if (!context) {
qCCritical(qLcQmlEngine) << "Can't get QML context from parent";
return nullptr;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Downgrading from qCFatal to qCCritical changes failure mode from aborting to continuing after a serious construction error.

Previously, a failed cast here would abort the process; now we just log, delete obj, and return nullptr. If callers still assume a non-null QQuickItem*, this can turn into a later null dereference or subtle misbehavior. Either keep this fatal, or audit/update all call sites to handle a nullptr return explicitly.

}

QObject *obj = component.createWithInitialProperties(properties, context);

if (!obj) {
qCCritical(qLcQmlEngine) << "Component creation failed:" << component.errorString();
return nullptr;
}

auto item = qobject_cast<QQuickItem *>(obj);
if (!item) {
qCFatal(qLcQmlEngine) << "Can't create component:" << component.errorString();
qCCritical(qLcQmlEngine) << "Created object is not a QQuickItem. Actual type:"
<< obj->metaObject()->className();
delete obj;
return nullptr;
}

QQmlEngine::setObjectOwnership(item, QQmlEngine::objectOwnership(parent));
item->setParent(parent);
item->setParentItem(parent);
component.completeCreate();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前之所以用 being/complete 的分体式方式实现,就是为了在初始化好它的parent对象后再标记为创建完成。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要么改为使用 QQmlComponentPrivate::createWithProperties


return item;
}
Expand All @@ -82,15 +93,20 @@
{ { "surface", QVariant::fromValue(surface) } });
}

QObject *QmlEngine::createWindowMenu(QObject *parent)

Check warning on line 96 in src/core/qmlengine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'createWindowMenu' is never used.
{
auto context = qmlContext(parent);
auto obj = windowMenuComponent.beginCreate(context);
if (!context) {
qCCritical(qLcQmlEngine) << "Can't get QML context from parent for WindowMenu";
return nullptr;
}

auto obj = windowMenuComponent.create(context);
if (!obj) {
qCFatal(qLcQmlEngine) << "Can't create WindowMenu:" << windowMenuComponent.errorString();
qCCritical(qLcQmlEngine) << "Can't create WindowMenu:" << windowMenuComponent.errorString();
return nullptr;
}
obj->setParent(parent);
windowMenuComponent.completeCreate();

return obj;
}
Expand Down