如何在Qt中初始化自定义QQuickItem

如何在Qt中初始化自定义QQuickItem,qt,qml,qtquick2,qqmlcomponent,Qt,Qml,Qtquick2,Qqmlcomponent,我已经实现了定制的QQuickItem,它暴露于QML。现在它在所有平台上都变得更加有用,但由于Windows中的某些原因,我们没有使用QML它是纯QT应用程序 但是我无法在Qt中实例化自定义QQuickItem,有人能帮我解决这个问题吗 下面是代码: customItem.h #include <QQuickItem> #include <QQuickWindow> class CustomItem : public QQuickItem { Q_OBJECT pub

我已经实现了定制的
QQuickItem
,它暴露于
QML
。现在它在所有平台上都变得更加有用,但由于Windows中的某些原因,我们没有使用
QML
它是纯QT应用程序

但是我无法在
Qt
中实例化自定义
QQuickItem
,有人能帮我解决这个问题吗

下面是代码:
customItem.h

#include <QQuickItem>
#include <QQuickWindow>

class CustomItem : public QQuickItem
{
Q_OBJECT
public:
explicit CustomItem(QQuickItem *parent = 0);

signals:

public slots:
void paint();

private slots:
void handleWindowChanged(QQuickWindow * win);

};
main.cpp

#include "customItem.h"

CustomItem::CustomItem(QQuickItem *parent) :
QQuickItem(parent)
{
connect(this, &CustomItem::windowChanged, this, &CustomItem::handleWindowChanged);
}

void CustomItem::paint()
{
QQuickWindow * win = window();
qreal ratio = win->devicePixelRatio();
int w = int(ratio * win->width());
int h = int(ratio * win->height());
glViewport(0, 0, w, h);

glDisable(GL_DEPTH_TEST);
glClearColor(0, 1, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);

}

void CustomItem::handleWindowChanged(QQuickWindow * win)
{
if (win) {
connect(win, &QQuickWindow::beforeRendering, this, &CustomItem::paint, Qt::DirectConnection);
    win->setClearBeforeRendering(false);
}

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

    QQuickView  *view = new QQuickView ();
    QWidget *container = QWidget::createWindowContainer(view);

    CustomItem *customitem = new CustomItem();///how can i set it view 
    container->show();

    return app.exec();
}
问题:
无法实例化CustomItem

您需要qmlRegisterType()(google it)等待。不要介意。你做什么?不能将小部件用作quickitem的父级。您需要创建一个快速视图或QuickWindow@Teimpz我已经更新了我的代码,你能帮我设置customItem来查看吗。我想最常见的方法是创建一个.qml文件,并将其设置为quickview的源文件。然后从qml实例化CustomItem。(类似于“CustomItem{anchors.fill:parent}”。您需要将qmlRegisterType设置为CustomItem才能执行此操作。您需要使用qmlRegisterType()等等。没关系。你做什么?你不能将小部件用作quickitem的父项。你需要创建QuickView或QuickWindow@Teimpz我已经更新了我的代码,你能帮我将customItem设置为view吗?嗯,有多种方法。我想最常见的方法是创建一个.qml文件,并将其设置为quickview的源文件。然后实例化从qml中删除CustomItem。(类似于“CustomItem{anchors.fill:parent}”。您需要将qmlRegisterType设置为CustomItem才能执行此操作。