如何修复错误,说明创建的图形对象未放置在图形场景中

时间:2018-04-25 00:24:14

标签: c++ qt qml qt5 qwidget

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

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

这样做我遇到了这个错误:

  

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

其次是:

  

该程序意外结束。

以下是我试图创建我的特殊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的支持,但是我试图实现一种方法来实现它,所以不要同时使用“你不能”的答案,因为有一种方式我确定。

1 个答案:

答案 0 :(得分:1)

  

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

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