Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

OpenGL rendering to a specifc QML item



  • Hi,
    I have a QML file which contains a layout of QML items and now I want one of those items to be a QGLWidget. i.e. I want to render to a specific QML item.

    Is anyone aware of how to do this? Thank you.

    CV



  • I don't think it's possible, but you could use FBO instead.



  • Hi,

    I'd suggest setting the viewport of your QDeclarativeView to a QGLWidget. You can then use GL within your item's paint() method. (typically using QPainter::beginNativePainting() and QPainter::endNativePainting()). Is this type of approach viable for what you need to do?

    Regards,
    Michael



  • Hi Michael,

    If I set the viewport to QGLWidget then the entire DeclarativeView becomes the paint area right. i.e. when I paint inside QGLWidget::paintGL() then I'd be painting to the whole view and not a specific region (i.e. QDeclarativeItem).
    I have a list of QDeclarativeItems from MyQDeclarativeView.rootObject()->children();
    And trying to figure out how I can make one of these items as a paint area for my GLWidget.
    Thanks for your reply.

    CV



  • Hi Michael,

    I am kind of successful but not completely, I created a new DeclarativeItem and set its graphicsproxywidget's widget to my glwidget as below

    @class GLWidgetDeclarativeItem : public QDeclarativeItem
    {
    Q_OBJECT
    public:
    GLWidgetDeclarativeItem(QDeclarativeItem *parent =0)
    : QDeclarativeItem(parent)
    {
    setFlag(QGraphicsItem::ItemHasNoContents, false);
    QDeclarativeItem::setFlag(QGraphicsItem::ItemHasNoContents, false);
    m_glWidget = new GLWidget();
    m_proxy = new QGraphicsProxyWidget(this);
    m_proxy->setWidget(m_glWidget);
    m_glWidget->show();
    m_proxy->show();
    }

    private:
    GLWidget *m_glWidget;
    QGraphicsProxyWidget *m_proxy;
    };
    @

    And in my main when I create a declarativeview, I find the QML item and pass it as parent to GLWidgetDeclarativeItem as
    @
    int main(int argc, char argv[])
    {
    QApplication app(argc, argv);
    QDeclarativeView qmlview (QUrl::fromLocalFile("qml/GLInsideQML/main.qml"));
    QDeclarativeItem item = qobject_cast<QDeclarativeItem>
    (qmlview.rootObject()->findChild <QObject
    > ("GLArea"));
    GLWidgetDeclarativeItem glItem(item);
    qmlview.show();
    return app.exec();
    }
    @

    Now, the only problem is I don't see the painting happening, I get the mouseEvents invoked in my GlWidget when I click. But no painting :(
    "Here's the complete test code":https://sites.google.com/site/blueskinneo/home/GLInsideQMLTest.zip?attredirects=0&d=1
    Let me know if you find anything wrong.

    Thanks
    CV



  • Hi,

    [quote author="blueskin.neo" date="1318962508"]
    If I set the viewport to QGLWidget then the entire DeclarativeView becomes the paint area right. i.e. when I paint inside QGLWidget::paintGL() then I'd be painting to the whole view and not a specific region (i.e. QDeclarativeItem).[/quote]

    You shouldn't need to worry about QGLWidget::paintGL() if you are creating a single custom item. You can use raw GL inside of your QDeclarativeItem subclass' paint() function instead (wrapped in the calls I mention above), as long as the viewport for the view is a QGLWidget.

    Here are a couple other threads that might explain this a bit better:

    Hope that helps.

    Regards,
    Michael



  • [quote author="blueskin.neo" date="1318962508"]Hi Michael,

    If I set the viewport to QGLWidget then the entire DeclarativeView becomes the paint area right. i.e. when I paint inside QGLWidget::paintGL() then I'd be painting to the whole view and not a specific region (i.e. QDeclarativeItem).
    I have a list of QDeclarativeItems from MyQDeclarativeView.rootObject()->children();
    And trying to figure out how I can make one of these items as a paint area for my GLWidget.
    Thanks for your reply.

    CV[/quote]
    I agree with mbrasser!
    In my real project, the painting could be restricted in the specific region. I created a C++ extension inherited from QDeclarativeItem (so, it is QGraphicsItem rathan than QGLWidget), and the extension works well with opengl native commands. I just overwrite the paint() function in QDeclarativeItem, not the paintGL() in QGLWidget.


Log in to reply