Results 1 to 20 of 30

Thread: Render QWidget within QGLWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    Now we render the background of the scene with our own OpenGL calls..

    Qt Code:
    1. #ifndef MAIN_H
    2. #define MAIN_H
    3.  
    4. #include <QtCore>
    5. #include <QtGui>
    6. #include <QtOpenGL>
    7.  
    8. using namespace std;
    9.  
    10. class OpenGLScene : public QGraphicsScene
    11. {
    12. public:
    13. OpenGLScene() {
    14.  
    15. }
    16. protected:
    17. void drawBackground(QPainter *painter, const QRectF &);
    18. };
    19.  
    20. void OpenGLScene::drawBackground(QPainter *painter, const QRectF &)
    21. {
    22. /*if (painter->paintEngine()->type() != QPaintEngine::OpenGL) {
    23.   qWarning("OpenGLScene: drawBackground needs a "
    24.   "QGLWidget to be set as viewport on the "
    25.   "graphics view");
    26.   return;
    27.   }*/
    28. //
    29. QColor m_backgroundColor = QColor("yellow");
    30. glClearColor(m_backgroundColor.redF(),
    31. m_backgroundColor.greenF(),
    32. m_backgroundColor.blueF(), 1.0f);
    33. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    34. }
    35.  
    36.  
    37. class MainWindow : public QMainWindow
    38. {
    39. public:
    40. MainWindow(QWidget *parent = 0);
    41. private:
    42. OpenGLScene *scene;
    43. QGraphicsView *graphicsView;
    44. };
    45.  
    46.  
    47.  
    48. MainWindow::MainWindow(QWidget *parent) :
    49. QMainWindow(parent)
    50. {
    51. scene = new OpenGLScene();
    52.  
    53. QPushButton* btn = new QPushButton("Test");
    54. scene->addWidget(btn);
    55.  
    56. graphicsView = new QGraphicsView(this);
    57. graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    58. graphicsView->setScene(scene);
    59. graphicsView->setGeometry(QRect(50, 50, 500, 300));
    60. graphicsView->show();
    61.  
    62. this->setCentralWidget(graphicsView);
    63.  
    64. }
    65.  
    66. #endif // MAIN_H
    To copy to clipboard, switch view to plain text mode 
    Aah! Something is wrong with this check! Did produce the same warning here. But the OpenGL stuff obviously works!

    Joh

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    That check should be nowadays:

    Qt Code:
    1. if ((painter->paintEngine()->type() != QPaintEngine::OpenGL2) &&
    2. (painter->paintEngine()->type() != QPaintEngine::OpenGL))
    3. {
    4. qWarning("OpenGLScene: drawBackground needs a "
    5. "QGLWidget to be set as viewport on the "
    6. "graphics view");
    7. return;
    8. }
    To copy to clipboard, switch view to plain text mode 
    HIH

    Joh

  3. #3
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    Thanks Joh ill try the above mentioned when i get back from work

    speaking of which, i tried to run the qq26-openglcanvas program on an Ubuntu 10.04 PC with Qt 4.6.2 and this code runs fine. howcome?

    BTW my home PC is Windows 7 using Qt 4.7.0

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    I guess your Ubuntu only supports/has drivers for OpenGL 1.0.
    Happy coding

  5. The following user says thank you to JohannesMunk for this useful post:

    crazymonkey (18th September 2010)

  6. #5
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    Thanks Joh, youve helped alot!

    quick question: where do i make my usual updateGL() , resizeGL() and paintGL() calls then?
    Last edited by crazymonkey; 18th September 2010 at 13:02.

  7. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    You don't!

    paintGL => drawBackground
    or if you want individual items, subclass QGraphicsItem implement boundingRect and paintEvent (using either QPainter stuff or native OpenGL calls with QPainter.beginNativePainting() .. )

    http://www.qtcentre.org/threads/34148-Qt-and-OpenGl-(I-m-lost)?p=158675#post158675

    resizeGL => taken care of the view, otherwise subclass QGraphicsView, implement resizeEvent.
    updateGL => not necessary.

    Joh

  8. #7
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    and if i want to use listeners to zoom in/out of my openGL scene is it done in the same way as i would normally do it?

  9. #8
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    Yes. You can then update the view either manually (view->update()), when something changes - or periodically with a Timer (fixed framerate), depends.

    Or you split your scene and implement different QGraphicsItems. Call their update() function to trigger a redraw of them.

    Joh

  10. #9
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    Sorry and what about initializeGL() ??

    im very noob sorry for all these questions!
    Last edited by crazymonkey; 19th September 2010 at 11:13.

  11. #10
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    InitializeGL. Bit tricky that one.

    Your example http://doc.trolltech.com/qq/qq26-openglcanvas.html doesn't use any opengl initialization. Does it on every repaint.

    drawBackground is called every 20ms due to (view.setViewportUpdateMode(QGraphicsView::FullVie wportUpdate); in main.cpp and QTimer::singleShot(20, this, SLOT(update())); at the end of drawBackground)

    If that's too expensive for you, because you need to setup big displaylists or something like that, you could:

    1) Do one of these: http://stackoverflow.com/questions/1...stom-qglwidget

    2) Have a look at the boxes demo: http://doc.trolltech.com/latest/demos-boxes.html

    3) "Dirty", but efficient Hack:

    Qt Code:
    1. class OpenGLScene : ..
    2. {
    3. private:
    4. bool initialized;
    5. }
    6.  
    7. OpenGLScene::OpenGLScene(..)
    8. {
    9. initialized = false;
    10. }
    11.  
    12. void OpenGLScene::drawBackground(QPainter *painter, const QRectF &)
    13. {
    14. if (initialized == false)
    15. {
    16. // your initialization goes here..
    17. initialized = true;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    Joh
    Last edited by JohannesMunk; 19th September 2010 at 12:47.

  12. #11
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    hello Joh im having difficulty in adding new widgets

    i think this is more of a Qt specific question but what command can i use to set the position of my widgets on my scene?

    i have added widgets made via Qt designer perfectly fine. but now i want to make them movable with mouse dragging.

    also my widget doesn't have a title like in the example shown http://doc.trolltech.com/qq/qq26-openglcanvas.html

    how do i make them movable via mouse dragging and how do i show a title (eg "Instructions")

  13. #12
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Render QWidget within QGLWidget

    Hi!

    You could have looked it up in the example you referred to!

    Qt Code:
    1. QWidget* w = new QWidget();
    2. w->setGeometry(0,0,150,150);
    3. w->setWindowTitle("Title");
    4. QGraphicsProxyWidget* proxy = scene->addWidget(w);
    5. proxy->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    6. proxy->setPos(10,10);
    To copy to clipboard, switch view to plain text mode 
    Cheers!

    Joh

Similar Threads

  1. Replies: 1
    Last Post: 7th May 2010, 17:20
  2. QGLWidget Render Text in the foreground
    By arpspatel in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2010, 11:35
  3. How to render the contents of QPrinter to QWidget
    By nifei in forum Qt Programming
    Replies: 0
    Last Post: 6th March 2009, 04:25
  4. QWidget::render with DrawChildren flag
    By Apocalypse in forum Qt Programming
    Replies: 4
    Last Post: 4th January 2009, 22:15
  5. QWidget::render() and sharedPainter
    By faldzip in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2008, 20:04

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
  •  
Qt is a trademark of The Qt Company.