QtQuickItem with custom opengl draw - after first frame, item dissapear
-
Basically i have this simple window with one GridLayout (for simplicity i added just one column and two rows).
In first row, i have GLViewer, which is QtQuickItem, that is drawing custom gl in beforeRendering slot ("inspired by this":http://qt-project.org/doc/qt-5/qtquick-scenegraph-openglunderqml-example.html )
When i run my app, all is ok, but when window is repainted (dragging or resizing, whatever causing repaint), my GLViewer dissapeared and i see ApplicationWindow background color instead (but paint method on GLViewer still called each repaint).@import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
import ge.components 1.0ApplicationWindow {
visible: true
width: 1200
height: 700
title: qsTr("Hello World")
color: "#292929"menuBar: MenuBar { Menu { title: qsTr("Filxe") MenuItem { text: qsTr("Exit") onTriggered: Qt.quit(); } } } GridLayout { columns: 1 rows: 2 anchors.fill: parent Rectangle { color: "white" Layout.row: 2 Layout.column: 1 Layout.fillWidth: true Layout.fillHeight: true } GLViewer { Layout.row: 1 Layout.column: 1 Layout.fillWidth: true Layout.fillHeight: true } }
}@
@#include "glviewer.h"
#include <QQuickWindow>
#include <iostream>
#include <QColor>using namespace std;
GLViewer::GLViewer(QQuickItem parent) :
QQuickItem(parent)
{
connect(this, SIGNAL(windowChanged(QQuickWindow)), this, SLOT(handleWindowChanged(QQuickWindow*)));
}void GLViewer::handleWindowChanged(QQuickWindow *win)
{
if (win) {
connect(win, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
win->setClearBeforeRendering(false);}
}
void GLViewer::paint() {
QColor color = window()->color(); glClearColor(color.redF(), color.greenF(), color.blueF(), color.alphaF()); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); cout << "X: " << x() << ", Y: " << window()->height() - y() - height() << ", W: " << width() << ", H: " << height() << endl; glViewport(x(), window()->height() - y() - height(), width(), height()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); QColor top(127, 145, 166), bottom(24, 26, 28); glBegin(GL_QUADS); glColor3f(bottom.redF(), bottom.greenF(), bottom.blueF()); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glColor3f(top.redF(), top.greenF(), top.blueF()); glVertex3f(1.0f, 1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd();
}
@ -
first frame:
!http://oi61.tinypic.com/2emzx9i.jpg(first frame)!second frame:
!http://oi59.tinypic.com/2qv9x90.jpg(second frame)! -
pleas Qt guys, does anyone know where is problem?