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

时间:2015-12-15 16:33:14

标签: qt qml qtquick2 qqmlcomponent

我实施了Clear data的自定义QQuickItem。现在它在所有平台上变得更有用,但由于Windows中的一些原因,我们没有使用QML纯QT应用程序。

但是我无法在QML中实例化自定义QQuickItem,是否有人可以帮助我解决这个问题?

这是代码:
customItem.h

Qt

customItem.cpp

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

}

问题: 无法实例化CustomItem

0 个答案:

没有答案