我在使用 QQuickItem 的 QT5 中有一个痛苦的问题。我必须在 QML 中使用纯 openGL 绘制 3D 模型,所以我创建了自己的自定义 QQuickItem。到目前为止,一切都按预期工作:3D 模型在 QML 中精美地显示。
当我想在自定义 QQuickItem 旁边的同一个 QML 中放置一个简单的矩形时,就会出现问题。不再显示 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"
}
}
任何人都可以向我提供至少对此问题的解释或一些建议吗?