1

I have implemented custom QQuickItem which is exposed to QML. Now it becomes more useful item in all platforms but due some reasons in Windows we are not using QML it's pure QT Application.

But I am not able to instantiate custom QQuickItem in Qt, could some one help me out of this?

Here is the code:
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);
}

}

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

Issue: Not able to instantiate CustomItem

Jeggu
  • 569
  • 2
  • 10
  • 26
  • You need to qmlRegisterType() (google it) – Teimpz Dec 15 '15 at 17:06
  • 3
    Wait. Never mind. You do what? You can't use a widget as a parent for a quickitem. You need to create a QuickView or a QuickWindow – Teimpz Dec 15 '15 at 17:08
  • @Teimpz I have updated my code, Could you help me set customItem to view. – Jeggu Dec 16 '15 at 04:30
  • Wel, there are more than one way. I guess the most common would be to creat a .qml file and set it as source to you quickview. Then instanciate the CustomItem from qml. (Somrthing like "CustomItem{anchors.fill:parent}". You will need to have qmlRegisterType'd the CustomItem to do this. – Teimpz Dec 17 '15 at 08:36

0 Answers0