Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: Render QWidget within QGLWidget

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

    Default Render QWidget within QGLWidget

    Hello All This is my first post on this forum. but i have a question (i dont know if it is a newbie question or not)

    I have been using/learning Qt over the past week or so, I'm comfortable with Qt and OpenGL to the point where i know how to use standard OpenGL API commands within Qt.

    Firstly I have done some searching and found http://doc.trolltech.com/qq/qq26-openglcanvas.html which is exactly what im trying to achieve, but when i try to compile and run the example code it doesn't work. these widgets also need to have their own event listeners etc...

    does anybody know if this is the only way to do this? any help will be appreciated.

    Thanks

    Crazymonkey
    Last edited by crazymonkey; 16th September 2010 at 21:10. Reason: updated contents

  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

    Hi!

    What doesn't work? Describe the problem! What event listeners?

    Joh

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

    Default Re: Render QWidget within QGLWidget

    This qWarning pops up several times when i run the program.

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

    JohannesMunk what I meant to say is that these Widgets which are embedded within my QGLWidget Scene must be able to handle their own event listeners as well


    PS im running Qt 4.7.0 with Qt Creator 2.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

    Well the warning says that your viewport setup didn't work.
    Did you copy/leave this line in place?
    Qt Code:
    1. view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    To copy to clipboard, switch view to plain text mode 
    Yes for sure if you want the widgets to work in a certain way, you will have to code their eventhandlers.

    What do you want to do? Where is the problem?

    Joh

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

    Default Re: Render QWidget within QGLWidget

    Joh i never altered the above code in any way. it's just giving me those errors (those qWarnings)

    What could the problem be? is it my PC?

    I ultimately want to embed a normal QWidget within a QGLWidget. at the moment all i have is a QGLWidget which renders my scene. now i want to add those widgets ontop so i can get user input/output.

  6. #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

    Ok. Step by step. You need to setup a QGraphicsView/Scene properly.

    Qt Code:
    1. main.cpp:
    2.  
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. #include "main.h"
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication a(argc, argv);
    11.  
    12. MainWindow w;
    13. w.show();
    14. return a.exec();
    15. }
    16.  
    17. ---------------------------
    18. main.h:
    19.  
    20. #ifndef MAIN_H
    21. #define MAIN_H
    22.  
    23. #include <QtCore>
    24. #include <QtGui>
    25. #include <QtOpenGL>
    26.  
    27. using namespace std;
    28.  
    29. class MainWindow : public QMainWindow
    30. {
    31. public:
    32. MainWindow(QWidget *parent = 0);
    33. private:
    34. QGraphicsView *graphicsView;
    35. };
    36.  
    37. MainWindow::MainWindow(QWidget *parent) :
    38. QMainWindow(parent)
    39. {
    40. scene = new QGraphicsScene();
    41.  
    42. QPushButton* btn = new QPushButton("Test");
    43. scene->addWidget(btn);
    44.  
    45. graphicsView = new QGraphicsView(this);
    46. graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    47. graphicsView->setScene(scene);
    48. graphicsView->setGeometry(QRect(50, 50, 500, 300));
    49. graphicsView->show();
    50.  
    51. this->setCentralWidget(graphicsView);
    52.  
    53. }
    54.  
    55. #endif // MAIN_H
    To copy to clipboard, switch view to plain text mode 
    Don't forget to add

    QT += opengl

    to your pro file.

    Does that work?

    Joh
    Last edited by JohannesMunk; 16th September 2010 at 22:08. Reason: QPushButton :->

  7. #7
    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

  8. #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

    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

  9. #9
    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

  10. #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

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

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

    crazymonkey (18th September 2010)

  12. #11
    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 14:02.

  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

    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

  14. #13
    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?

  15. #14
    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

  16. #15
    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 12:13.

  17. #16
    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 13:47.

  18. #17
    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")

  19. #18
    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

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

    Default Re: Render QWidget within QGLWidget

    sorry i meant how do make it draggable across the scene?

  21. #20
    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 can take it and drag it around this way. what else do you want?

Similar Threads

  1. Replies: 1
    Last Post: 7th May 2010, 18:20
  2. QGLWidget Render Text in the foreground
    By arpspatel in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2010, 12:35
  3. How to render the contents of QPrinter to QWidget
    By nifei in forum Qt Programming
    Replies: 0
    Last Post: 6th March 2009, 05:25
  4. QWidget::render with DrawChildren flag
    By Apocalypse in forum Qt Programming
    Replies: 4
    Last Post: 4th January 2009, 23:15
  5. QWidget::render() and sharedPainter
    By faldzip in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2008, 21: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.