我想在QML应用程序中编写一个定制的OpenGL Widget,以便用MathGL绘制数据。
为了做到这一点,我看了一下http://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html的场景图示例
然后我根据我的需要修改了代码,然后问题就发生了,图像只闪烁一个渲染周期,之后就不再出现了。以下是重要的功能和绑定。
类GLRenderEngine :公共QObject,公共QOpenGLFunctions
void GLRenderEngine::render()
{
if(!m_bInit)
{
initializeOpenGLFunctions();
m_pGraph = new mglGraph( 1 );
m_bInit = true;
}
glViewport(m_Viewport.left(), m_Viewport.top(), m_Viewport.width(), m_Viewport.height());
m_pGraph->Clf();
//Graph stuff ...
m_pGraph->Finish();
if(m_pWindow)
m_pWindow->resetOpenGLState();
}类GLWidget :公共QQuickItem
GLWidget::GLWidget(QQuickItem *parent) : QQuickItem(parent)
{
m_pRender = 0;
connect(this, &QQuickItem::windowChanged, this, &GLWidget::handleWindowChanged);
}
void GLWidget::handleWindowChanged(QQuickWindow *win)
{
if(win)
{
connect(win, &QQuickWindow::beforeSynchronizing, this, &GLWidget::sync, Qt::DirectConnection);
connect(win, &QQuickWindow::sceneGraphInvalidated, this, &GLWidget::cleanup, Qt::DirectConnection);
win->setClearBeforeRendering(false);
}
}
void GLWidget::cleanup()
{
if(m_pRender)
{
delete m_pRender;
m_pRender = 0;
}
}
void GLWidget::sync()
{
if(!m_pRender)
{
m_pRender = new GLRenderEngine();
connect(window(), &QQuickWindow::beforeRendering, m_pRender, &GLRenderEngine::render, Qt::DirectConnection);
}
m_pRender->setViewportSize(boundingRect());
m_pRender->setWindow(window());
}QML-档案
import QtQuick 2.8
import QtQuick.Window 2.2
import GLWidget 1.0
Window {
visible: true
width: 320
height: 480
GLWidget{
anchors.fill: parent
id: glView
}
Rectangle {
color: Qt.rgba(1, 1, 1, 0.7)
radius: 10
border.width: 1
border.color: "white"
anchors.fill: label
anchors.margins: -10
}
Text {
id: label
color: "black"
wrapMode: Text.WordWrap
text: "The background here is a squircle rendered with raw OpenGL using the 'beforeRender()' signal in QQuickWindow. This text label and its border is rendered using QML"
anchors.right: parent.right
anchors.left: parent.left
anchors.bottom: parent.bottom
anchors.margins: 20
}
}我还注意到,当我使用QQuickFramebufferObject时,在调用update()或窗口的一个调整大小事件之后,图像消失了,即使正在调用render函数,所以我的猜测是缓冲区没有被更新,或者其他的qt关闭了。
提前感谢您的帮助。
发布于 2017-07-17 10:48:59
为了解决这个问题,我切换到QQuickFrameBuffer实现,删除了在render函数中使用的所有glClear命令,同时启用了基类QQuickItem的清楚标志。现在它就像一种魅力。
https://stackoverflow.com/questions/45128790
复制相似问题