C++ 如何修复“创建的图形对象未放置在图形场景中”的错误

C++ 如何修复“创建的图形对象未放置在图形场景中”的错误,c++,qt,qml,qt5,qwidget,C++,Qt,Qml,Qt5,Qwidget,我有一点奇怪的错误是由一个看似简单的问题引起的 在我的源代码中,我试图使用QQuickPaintedItem来渲染QWidget派生类(QPushButton)的整体外观,然后将其绘制到QQuickPaintedItem上 在这样做的过程中,我遇到了以下错误: QQmlComponent:创建的图形对象未放置在图形中 场景 其次是: 程序意外地完成了 以下是我试图创建特殊QQuickPaintedItem的上下文及其源代码: main.qml widgetInterface.h \ifndef

我有一点奇怪的错误是由一个看似简单的问题引起的

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

在这样做的过程中,我遇到了以下错误:

QQmlComponent:创建的图形对象未放置在图形中 场景

其次是:

程序意外地完成了

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

main.qml widgetInterface.h
\ifndef widget接口
#定义WIDGETINTERFACE\u H
#包括
#包括
#包括
#包括
#包括
#包括
WidgetInterface类:公共QQuickPaintedItem
{
公众:
WidgetInterface(QQuickItem*parent=nullptr,QWidget*renderWidget=nullptr);
QWidget*m_widget;
QPushButton*m_按钮;
受保护的:
空漆(QPainter*油漆工);
公众时段:
void morphintobton(QString-txt);
};
#endif//widget接口
widgetinterface.cpp
#包括“widgetinterface.h”
#包括
#包括
#包括
#包括
#包括
WidgetInterface::WidgetInterface(QQuickItem*父对象,QWidget*renderWidget):QQuickPaintedItem(父对象),m_小部件(renderWidget)
{
语素按钮(“测试”);
QQmlEngine::setObjectOwnership(m_按钮,QQmlEngine::JavaScriptOwnership);
}
void WidgetInterface::paint(QPainter*painter)
{
if(m_widget!=nullptr){
画师->结束();
m_按钮->渲染(画师);
}否则{
}
}
void WidgetInterface::MorphintButton(QString txt)
{
m_widget->setGeometry(0,0,this->width(),this->height());
m_按钮=新的QPushButton(m_小部件);
m_按钮->设置几何体(m_小部件->几何体());
m_按钮->设置文本(txt);
此->更新(此->边界Rect().toRect());
}
main.cpp
#包括
#包括
#包括
#包括“src/interfaces/widgetinterface.h”
int main(int argc,char*argv[])
{
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
qmlRegisterType(“com.particletool”,1,0,“WidgetInterface”);
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
if(engine.rootObjects().isEmpty())
返回-1;
返回app.exec();
}
预期结果是在QML场景图上绘制一个按钮 有人知道我如何使用某种方法来实现这一点吗?(我知道Qt不支持它,但我正在尝试实现一种方法来实现它,所以不要同时使用“You can”答案,因为我确信有一种方法

预期结果是在QML场景图上绘制一个按钮


但最终的目标是通过包含一个库(如Qt.Widgets 1.0或其他)来实现QML中所有基于QWidget的元素
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
     }
   }
#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
#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());

}
#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();
}
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;
};