QQuickItem上的Qt矩形未正确呈现

时间:2014-12-04 09:09:09

标签: opengl qml qt5 qtopengl qquickitem

我使用QQuickItem在QT5中遇到了一个痛苦的问题。我不得不在QML中使用纯OpenGL绘制3D模型,因此我创建了自己的自定义QQuickItem。直到现在一切都按预期工作:3D模型在QML中精美地显示。

当我想在我的自定义QQuickItem旁边的同一个QML中放置一个简单的Rectangle时,会出现问题。不再显示3D模型。那是为什么?

这是我的自定义快速商品代码:

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

这是我的QML,在3D模型上有一个小矩形:

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"
    }
}

任何人都可以向我提供至少对此问题的解释或一些建议吗?

0 个答案:

没有答案