如何修复“Created graphical object was not placed in the graphics scene”的错误

How can I fix the error saying Created graphical object was not placed in the graphics scene

我有一个 st运行ge 错误是由一个看似简单的问题引起的。

在我的源代码中,我试图使用 QQuickPaintedItem 来渲染派生的 QWidget class (QPushButton) 的整体外观,然后将其绘制到 QQuickPaintedItem

在这样做的过程中,我 运行 遇到了这个错误:

QQmlComponent: Created graphical object was not placed in the graphics scene.

其次是:

The program has unexpectedly finished.

这是我试图创建我的特殊 QQuickPaintedItem 的上下文及其源代码:

main.qml

import QtQuick 2.9
import com.particletool 1.0
import QtQuick.Particles 2.0
import QtQuick.Window 2.3
import QtQuick.Controls 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Controls 1.4 as QQ1
  Item {
     id: root
     width: Screen.width
     height: Screen.height
     WidgetInterface {
        id: w
     }
   }

widgetInterface.h

#ifndef WIDGETINTERFACE_H
#define WIDGETINTERFACE_H

#include <QObject>
#include <QQuickItem>
#include <QQuickPaintedItem>
#include <QWidget>
#include <QPushButton>
#include <QtQuick>
class WidgetInterface : public QQuickPaintedItem
{
public:
    WidgetInterface(QQuickItem *parent = nullptr, QWidget* renderWidget = nullptr);
    QWidget* m_widget;
    QPushButton* m_button;
protected:
    void paint(QPainter* painter);
public slots:
   void morphIntoButton(QString txt);
};

#endif // WIDGETINTERFACE_H

widgetinterface.cpp

#include "widgetinterface.h"
#include <QQuickPaintedItem>
#include <QObject>
#include <QPainter>
#include <QWidget>
#include <QPushButton>
WidgetInterface::WidgetInterface(QQuickItem *parent, QWidget* renderWidget) : QQuickPaintedItem(parent), m_widget(renderWidget)
{
    morphIntoButton("test");
    QQmlEngine::setObjectOwnership(m_button, QQmlEngine::JavaScriptOwnership);
}

void WidgetInterface::paint(QPainter *painter)
{
    if (m_widget != nullptr) {
        painter->end();
        m_button->render(painter);

    } else {

    }
}


void WidgetInterface::morphIntoButton(QString txt)
{
    m_widget->setGeometry(0,0, this->width(), this->height());

    m_button = new QPushButton(m_widget);
    m_button->setGeometry(m_widget->geometry());
    m_button->setText(txt);
    this->update(this->boundingRect().toRect());

}

main.cpp

#include <QtGui/QGuiApplication>
#include <QApplication>
#include <QtQml/QQmlApplicationEngine>
#include "src/interfaces/widgetinterface.h"
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);


    QQmlApplicationEngine engine;

      qmlRegisterType<WidgetInterface>("com.particletool", 1, 0, "WidgetInterface");

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

预期的结果是在 QML 场景图上绘制了一个按钮 有谁知道我如何使用某种方法实现这一目标? (我知道 Qt 不支持它,但我正在尝试实现一种实现它的方法,所以不要同时使用 "You can't" 答案,因为我确定有一种方法。

The expected result is a button being drawn on to the QML scene graph

class WidgetInterface : public QQuickPaintedItem
{
public:
    WidgetInterface(QQuickItem *parent = Q_NULLPTR) :
        QQuickPaintedItem(parent)
    {
        mButton = new QPushButton("TEST", Q_NULLPTR);
        auto resize = [=](){
            mButton->setGeometry(QRect(QPoint(), boundingRect().size().toSize()));
            QPixmap p(mButton->size());
            mButton->render(&p);
            if(!p.isNull())
                mButtonPix = p;
            update();
        };
        connect(this, &QQuickPaintedItem::widthChanged,  this, resize, Qt::QueuedConnection);
        connect(this, &QQuickPaintedItem::heightChanged, this, resize, Qt::QueuedConnection);
    }
    ~WidgetInterface() {
        mButton->deleteLater();
    }
protected:
    void paint(QPainter* painter){
        painter->drawPixmap(0, 0, mButtonPix);
    }
private:
    QPushButton* mButton;
    QPixmap      mButtonPix;
};