c++ 安全删除StackView转换中使用的QML组件

p5cysglq  于 2023-08-09  发布在  其他
关注(0)|答案(3)|浏览(115)

概述

我的问题涉及到由QQmlComponent::create()创建的QObject的生存期。create()返回的对象是QQmlComponent的示例化,我将其添加到QML StackView中。我在C++中创建对象并将其传递给QML以在StackView中显示。问题是,当我从堆栈中弹出一个项目时,我会收到错误。我写了一个演示应用程序来说明正在发生的事情。

  • 免责声明:是的,我知道从C进入QML不是“最佳实践”。然而,在生产环境中,有大量的C代码需要与UI共享,因此C和CML之间需要一些互操作。我使用的主要机制是通过在C端设置上下文的Q_PROPERTY绑定。*

此屏幕是演示开始时的样子:
x1c 0d1x的数据
StackView位于中间,背景为灰色,其中有一个项目(带有文本“默认视图”);该项目由QML示例化和管理。现在,如果你按下Push按钮,那么C++后端会从ViewA.qml创建一个对象,并将其放置在堆栈上......下面是一个屏幕截图:



此时,我按PopStackView中删除“View A”(上图中的红色)。C调用QML从堆栈中弹出项,然后删除它创建的对象。问题是QML需要这个对象来制作过渡动画(我使用的是StackView的默认动画),当我从C中删除它时,它会抱怨。所以我想我明白为什么会发生这种情况,但我不确定如何找出QML何时完成对象,以便我可以删除它。我如何确保QML是用我在C++中创建的对象完成的,以便我可以安全地删除它?
总而言之,以下是重现我所描述的问题的步骤:
1.启动程序
1.点击推送
1.点击弹出
下面的输出显示了在上面的步骤3中弹出项目时发生的TypeError

输出

在下面的输出中,我按了一次“Push”,然后按了“Pop”。请注意调用~ViewA()时的两个TypeError

root object name =  "appWindow"
[c++] pushView() called
qml: [qml] pushView called with QQuickRectangle(0xdf4c00, "my view")
[c++] popView() called
qml: [qml] popView called
[c++] deleting view
~ViewA() called
file:///opt/Qt5.8.0/5.8/gcc_64/qml/QtQuick/Controls/Private/StackViewSlideDelegate.qml:97: TypeError: Cannot read property 'width' of null
file:///opt/Qt5.8.0/5.8/gcc_64/qml/QtQuick/Controls/StackView.qml:899: TypeError: Type error

字符串

Context必须从C++设置

显然,发生的事情是StackView正在使用的对象(项)被C删除,但QML仍然需要这个项来进行过渡动画。我想我可以在QML中创建对象,并让QML引擎管理生存期,但我需要设置对象的QQmlContext以将QML视图绑定到C端的Q_PROPERTY s。
请参阅Who owns object returned by QQmlIncubator上的相关问题。

代码示例

我已经生成了一个最小的完整示例来说明这个问题。下面列出了所有文件。特别是,请查看~ViewA()中的代码注解。

// main.qml
import QtQuick 2.3
import QtQuick.Controls 1.4

Item {
    id: myItem
    objectName: "appWindow"

    signal signalPushView;
    signal signalPopView;

    visible: true
    width: 400
    height: 400

    Button {
        id: buttonPushView
        text: "Push"
        anchors.left: parent.left
        anchors.top: parent.top
        onClicked: signalPushView()
    }

    Button {
        id: buttonPopView
        text: "Pop"
        anchors.left: buttonPushView.left
        anchors.top: buttonPushView.bottom
        onClicked: signalPopView()
    }

    Rectangle {
        x: 100
        y: 50
        width: 250
        height: width
        border.width: 1

        StackView {
            id: stackView
            initialItem: view
            anchors.fill: parent

            Component {
                id: view

                Rectangle {
                    color: "#DDDDDD"

                    Text {
                        anchors.centerIn: parent
                        text: "Default View"
                    }
                }
            }
        }
    }

    function pushView(item) {
        console.log("[qml] pushView called with " + item)
        stackView.push(item)
    }

    function popView() {
        console.log("[qml] popView called")
        stackView.pop()
    }
}
// ViewA.qml
import QtQuick 2.0

Rectangle {
    id: myView
    objectName: "my view"

    color: "#FF4a4a"

    Text {
        text: "View A"
        anchors.centerIn: parent
    }
}
// viewa.h
    #include <QObject>

class QQmlContext;
class QQmlEngine;
class QObject;

class ViewA : public QObject
{
    Q_OBJECT
public:
    explicit ViewA(QQmlEngine* engine, QQmlContext* context, QObject *parent = 0);
    virtual ~ViewA();

    // imagine that this view has property bindings used by 'context'
    // Q_PROPERTY(type name READ name WRITE setName NOTIFY nameChanged)

    QQmlContext* context = nullptr;
    QObject* object = nullptr;
};
// viewa.cpp
#include "viewa.h"
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>
#include <QDebug>

ViewA::ViewA(QQmlEngine* engine, QQmlContext *context, QObject *parent) :
    QObject(parent),
    context(context)
{
    // make property bindings visible to created component
    this->context->setContextProperty("ViewAContext", this);

    QQmlComponent component(engine, QUrl(QLatin1String("qrc:/ViewA.qml")));
    object = component.create(context);
}

ViewA::~ViewA()
{
    qDebug() << "~ViewA() called";
    // Deleting 'object' in this destructor causes errors
    // because it is an instance of a QML component that is
    // being used in a transition. Deleting it here causes a
    // TypeError in both StackViewSlideDelegate.qml and
    // StackView.qml. If 'object' is not deleted here, then
    // no TypeError happens, but then 'object' is leaked.
    // How should 'object' be safely deleted?

    delete object;  // <--- this line causes errors

    delete context;
}
// viewmanager.h
#include <QObject>

class ViewA;
class QQuickItem;
class QQmlEngine;

class ViewManager : public QObject
{
    Q_OBJECT
public:
    explicit ViewManager(QQmlEngine* engine, QObject* topLevelView, QObject *parent = 0);

    QList<ViewA*> listOfViews;
    QQmlEngine* engine;
    QObject* topLevelView;

public slots:
    void pushView();
    void popView();
};
// viewmanager.cpp
#include "viewmanager.h"
#include "viewa.h"
#include <QQmlEngine>
#include <QQmlContext>
#include <QDebug>
#include <QMetaMethod>

ViewManager::ViewManager(QQmlEngine* engine, QObject* topLevelView, QObject *parent) :
    QObject(parent),
    engine(engine),
    topLevelView(topLevelView)
{
    QObject::connect(topLevelView, SIGNAL(signalPushView()), this, SLOT(pushView()));
    QObject::connect(topLevelView, SIGNAL(signalPopView()), this, SLOT(popView()));
}

void ViewManager::pushView()
{
    qDebug() << "[c++] pushView() called";

    // create child context
    QQmlContext* context = new QQmlContext(engine->rootContext());

    auto view = new ViewA(engine, context);
    listOfViews.append(view);

    QMetaObject::invokeMethod(topLevelView, "pushView",
        Q_ARG(QVariant, QVariant::fromValue(view->object)));
}

void ViewManager::popView()
{
    qDebug() << "[c++] popView() called";

    if (listOfViews.count() <= 0) {
        qDebug() << "[c++] popView(): no views are on the stack.";
        return;
    }

    QMetaObject::invokeMethod(topLevelView, "popView");

    qDebug() << "[c++] deleting view";
    auto view = listOfViews.takeLast();
    delete view;
}
// main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickView>
#include <QQuickItem>
#include "viewmanager.h"
#include <QDebug>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQuickView view;
    view.setSource(QUrl(QLatin1String("qrc:/main.qml")));

    QObject* item = view.rootObject();
    qDebug() << "root object name = " << item->objectName();
    ViewManager viewManager(view.engine(), item);

    view.show();
    return app.exec();
}
eqoofvh9

eqoofvh91#

我在回答自己的问题。如果你发布一个答案,我会考虑接受你的答案,而不是这个。但是,这是一种可能的变通方法。
问题是,在C++中创建的QML对象需要足够长的时间,以便QML引擎完成所有转换。我使用的技巧是将QML对象示例标记为删除,等待几秒钟让QML完成动画,然后删除该对象。这里的“hacky”部分是我必须猜测我应该等待多少秒,直到我认为QML完全完成了对象。
首先,我列出了计划销毁的对象的列表。我还创建了一个slot,它将在延迟后被调用以实际删除对象:

class ViewManager : public QObject {
public:
    ...
    QList<ViewA*> garbageBin;
public slots:
    void deleteAfterDelay();
}

字符串
然后,当堆栈项弹出时,我将该项添加到garbageBin,并在2秒内执行一个单次触发信号:

void ViewManager::popView()
{
    if (listOfViews.count() <= 0) {
        qDebug() << "[c++] popView(): no views are on the stack.";
        return;
    }

    QMetaObject::invokeMethod(topLevelView, "popView");

    // schedule the object for deletion in a few seconds
    garbageBin.append(listOfViews.takeLast());
    QTimer::singleShot(2000, this, SLOT(deleteAfterDelay()));
}


几秒钟后,deleteAfterDelay()插槽被调用,并对该项进行“垃圾收集”:

void ViewManager::deleteAfterDelay()
{
    if (garbageBin.count() > 0) {
        auto view = garbageBin.takeFirst();
        qDebug() << "[c++] delayed delete activated for " << view->objectName();
        delete view;
    }
}


除了不能100%确信等待2秒总是足够长之外,它在实践中似乎工作得非常好-不再有TypeError s,并且C++创建的所有对象都被正确清理。

9njqaruj

9njqaruj2#

我相信我已经找到了一种方法来丢弃@Matthew Kraus推荐的垃圾清单。我让QML在弹出StackView时处理视图的销毁。

  • 警告:片段是不完整的,只是为了说明扩展到OP的职位 *
function pushView(item, id) {
    // Attach option to automate the destruction on pop (called by C++)
    rootStackView.push(item, {}, {"destroyOnPop": true})
}

function popView(id) {
    // Pop immediately (removes transition effects) and verify that the view
    // was deleted (null). Else, delete immediately.
    var old = rootStackView.pop({"item": null, "immediate": true})
    if (old !== null) {
        old.destroy() // Requires C++ assigns QML ownership
    }

    // Tracking views in m_activeList by id. Notify C++ ViewManager that QML has
    // done his job
    viewManager.onViewClosed(id)
}

字符串
你很快就会发现,如果对象是由C++创建并拥有的,那么解释器会在删除时向你大喊。

m_pEngine->setObjectOwnership(view, QQmlEngine::JavaScriptOwnership);
QVariant arg = QVariant::fromValue(view);

bool ret = QMetaObject::invokeMethod(
            m_pRootPageObj,
            "pushView",
            Q_ARG(QVariant, arg),
            Q_ARG(QVariant, m_idCnt));

voj3qocg

voj3qocg3#

虽然迟到了,但仍在与同样的问题作斗争,似乎解决方案是让绑定变量保持活动状态,直到组件接收到此事件:

StackView.onRemoved: { call your destruction sequence here; }

字符串
这似乎延迟了销毁,以便QML可以完成所有需要的操作,并且绑定不再活动。

相关问题