I have a painful issue in QT5 using QQuickItem. I had to draw a 3D model using pure openGL in QML, so I created my own custom QQuickItem. Untill now everything works as expected: the 3D model is beautifully displayed in QML.
The problem appears when I want to put a simple Rectangle in the same QML beside my custom QQuickItem. The 3D model is no longer displayed. Why is that?
Here is my custom quick item code:
MQuickItem::MQuickItem(){
isInit = false;
connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
}
void MQuickItem::handleWindowChanged(QQuickWindow *win){
w = new SMThickness3DView(/*width(), height()*/);
if(win){
connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(initializeMGL()), Qt::DirectConnection);
connect(win, SIGNAL(beforeRendering()), this, SLOT(paintMGL()), Qt::DirectConnection);
connect(win, SIGNAL(widthChanged(int)), this, SLOT(resizeMGL()), Qt::DirectConnection);
connect(win, SIGNAL(heightChanged(int)), this, SLOT(resizeMGL()), Qt::DirectConnection);
win->setClearBeforeRendering(false);
}
}
void MQuickItem::initializeMGL(){
w->initializeGL();
isInit = true;
}
void MQuickItem::paintMGL(){
w->paintGL();
// connect(window()->openglContext(), SIGNAL(aboutToBeDestroyed()), this, SLOT(cleanupMGL()), Qt::DirectConnection);
}
void MQuickItem::resizeMGL(){
if(isInit){
w->resizeGL(width(), height());
w->paintGL();
}
}
void MQuickItem::cleanupMGL(){
}
void MQuickItem::rotatePerson(bool toLeft){
if(toLeft)
w->setYaw(std::min(w->getYaw() + 2., 90.));
else
w->setYaw(std::max(w->getYaw() - 2.0, -90.0));
}
And here is my QML with a small rectangle over the 3D model:
Item {
anchors.fill: parent
anchors.centerIn: parent
MQuickItem {
id: obj
property bool mReleased: true
anchors.fill: parent
MouseArea{
id: mArea
anchors.fill: parent
drag.target: dummy
property int initialPressedX;
property int initialPressedY;
onPressed: {
initialPressedX = mArea.mouseX;
initialPressedY = mArea.mouseY;
}
onPositionChanged: {
var diff = mArea.mouseX - initialPressedX;
if(Math.abs(diff) > 10){
if(diff < 0){
obj.rotatePerson(false)
}
else{
obj.rotatePerson(true)
}
initialPressedX = mArea.mouseX;
initialPressedY = mArea.mouseY;
}
}
Rectangle{id: dummy}
}
}
Rectangle{
id:thisIsTheRectangleThatMakesMyModelDisapear
width: 50
height: 50
color:"red"
}
}
Can anyone provide me with at least an explanation of this issue, or some suggestions?