Search code examples
c++qtqmlqt5qwidget

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


I have a bit of a strange error being caused by a seemingly simple problem.

In my source code I am attempting to use QQuickPaintedItem to render just the overall look of a QWidget derived class (QPushButton), and then painting it on to the QQuickPaintedItem

In doing so I ran into this error:

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

Followed by:

The program has unexpectedly finished.

Here's the context I am trying to create my special QQuickPaintedItem in along with the source code for it:

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

The expected result is a button being drawn on to the QML scene graph Does anyone know how I can achieve this using some method? ( I know that its not supported by Qt, but I am trying to implement a way to make it happen so dont both with the "You can't" answers because there is a way I'm sure.


Solution

  • 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;
    };