Results 1 to 3 of 3

Thread: [Qt4.8] QDeclarativeView in front of QGLWidget

  1. #1
    Join Date
    Jun 2009
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt4.8] QDeclarativeView in front of QGLWidget

    I am trying to display a QDeclarativeView in front of QGLWidget, to create contextual menus,...

    Here is my code:
    Qt Code:
    1. QApplication::setGraphicsSystem("opengl");
    2.  
    3. ....
    4.  
    5.  
    6. RenderWidget* renderWidget = new RenderWidget; // RenderWidget is the QGLWidget
    7.  
    8. QDeclarativeView *qmlView = new QDeclarativeView(renderWidget);
    9. qmlView->setSource(QUrl::fromLocalFile("qml/main.qml"));
    10. qmlView->setStyleSheet(QString("background: transparent"));
    11. qmlView->setAutoFillBackground(false);
    12. qmlView->setPalette(Qt::transparent);
    13.  
    14. QVBoxLayout *layout = new QVBoxLayout(renderWidget);
    15. layout->setMargin(200); // reducing size to view to 3D scene behind
    16. layout->addWidget(qmlView);
    17.  
    18. setCentralWidget(renderWidget);
    19. resize(640,480);
    To copy to clipboard, switch view to plain text mode 

    With this code my qml is display in front of the 3D scene, but the layout is rendered with full opacity. When I use gDEBugger I see that the QDeclarativeView use an other openGL context than my 3D engine, and a texture containing the result of the qml view.
    The texture of the qml view seems correct (with appropriate alpha zones)
    I suppose the blit is completly software, when I am expecting an hardware one because I call QApplication::setGraphicsSystem("opengl"); before the QApplication initialization.

    Is there a way to force the QDeclarativeView to share his opengl context with those of QGLWidget?


    Added after 29 minutes:


    When I remove the layout the result is exactly the same.
    Last edited by flamaros; 21st February 2013 at 17:08.
    Koalabs Studio

  2. #2
    Join Date
    Jun 2009
    Posts
    27
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt4.8] QDeclarativeView in front of QGLWidget

    Solved :

    I found a simpler way to achieve my purpose. I force the application to use opengl render and register a class as QDeclarativeItem in which I put my openGL.

    main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication::setGraphicsSystem("opengl");
    4. QScopedPointer<QApplication> app(createApplication(argc, argv));
    5.  
    6. // 3D Scene Binding
    7. qmlRegisterType<RenderItem>("RenderItem", 1, 0, "Scene3D");
    8.  
    9. // classic Qt init
    10. QmlApplicationViewer viewer;
    11.  
    12. // update timer
    13. QTimer repaintTimer;
    14. QObject::connect(&repaintTimer, SIGNAL(timeout()), &viewer, SLOT(update()));
    15. repaintTimer.setSingleShot(false);
    16. repaintTimer.start(0);
    17. // --
    18.  
    19. viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    20. viewer.setRenderHints(QPainter::HighQualityAntialiasing);
    21. viewer.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    22.  
    23. viewer.setMainQmlFile(QLatin1String("qml/main.qml"));
    24. viewer.showExpanded();
    25.  
    26. return app->exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    RenderItem.h:
    Qt Code:
    1. class RenderItem : public QDeclarativeItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. RenderItem(QDeclarativeItem* parent = 0)
    7. {
    8. // Tell to Qt Declarative that this item have to be draw
    9. setFlag(QGraphicsItem::ItemHasNoContents, false);
    10. }
    11. ~RenderItem();
    12.  
    13. virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    14. {
    15. painter->beginNativePainting();
    16. if (!m_bGLInitialized)
    17. // No relation of QGLWidget method (I just didn't change names)
    18. initializeGL();
    19.  
    20. if (mWidth != width() || mHeight != height())
    21. {
    22. mWidth = width(); mHeight = height();
    23. // No relation of QGLWidget method (I just didn't change names)
    24. resizeGL(mWidth, mHeight);
    25. }
    26.  
    27. // No relation of QGLWidget method (I just didn't change names)
    28. paintGL();
    29.  
    30. painter->endNativePainting();
    31. }
    32.  
    33. ...
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 

    main.qml:
    Qt Code:
    1. import QtQuick 1.1
    2. import RenderItem 1.0
    3.  
    4. Rectangle {
    5. id: main
    6. width: 1024
    7. height: 600
    8.  
    9. Scene3D {
    10. anchors.centerIn: parent
    11. anchors.fill: parent
    12. }
    13.  
    14. ...
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 


    Notes: If you are using FBO just take care of backup and restore the current FBO ID each frames, else you will have conflicts between your scene and qml GUI (in some cases your window can just stay white).
    Koalabs Studio

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [Qt4.8] QDeclarativeView in front of QGLWidget

    And all that was required was to call:

    Qt Code:
    1. qmlView->setViewport(renderWidget);
    To copy to clipboard, switch view to plain text mode 

    and make sure the qmlscene doesn't clear to white.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 16th February 2013, 16:06
  2. How QDeclarativeView destruction
    By xman_ss in forum Qt Programming
    Replies: 3
    Last Post: 27th April 2012, 07:55
  3. Drag into a QDeclarativeView
    By frankiefrank in forum Qt Quick
    Replies: 1
    Last Post: 3rd October 2011, 12:12
  4. Resize QDeclarativeView
    By Globulus in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2011, 17:31
  5. Using QDeclarativeView::Show()
    By proj_symbian in forum Qt Quick
    Replies: 17
    Last Post: 30th May 2011, 19:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.