我已经实现QQuickItem
了暴露于QML
. 现在它在所有平台上都变得更有用了,但由于 Windows 中的某些原因,我们没有使用QML
它的纯 QT 应用程序。
但是我无法实例化自定义QQuickItem
,Qt
有人可以帮我解决这个问题吗?
这是代码:
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);
};
customItem.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);
}
}
主.cpp:
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