Results 1 to 7 of 7

Thread: Qt and OpenGl (I'm lost)

  1. #1
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qt and OpenGl (I'm lost)

    Hi,

    I am trying to create a really simple application but I can't seem to understand what is going wrong or if what I am trying to do is possible at all (the way I want it, maybe there is a better way).

    Basically I want to make an application that draws vectors on the screen. I started by reading the hellogl example provided by trolltech and then started coding. I Created a class GLWidget, which inherits from QGLWidget, redefined painGL, resizeGL, initializeGL, etc.

    Everything works fine as long as I use paintGL for the OpenGL API calls, this is a big problem because I wanted to use a class Vector that would contain a method draw() which would draw the vector on the GLWidget. Is this possible? In the Qt4.6 doc it says :

    Your widget's OpenGL rendering context is made current when paintGL(), resizeGL(), or initializeGL() is called. If you need to call the standard OpenGL API functions from other places (e.g. in your widget's constructor or in your own paint functions), you must call makeCurrent() first.
    void QGLWidget::makeCurrent ()
    Makes this widget the current widget for OpenGL operations, i.e. makes the widget's rendering context the current OpenGL rendering context.
    Where I am supposed to call makeCurrent? Inside the GLWidget? Outside right before calling vector.draw()? Nothing seems to works.

    I read somewhere else that you have to call swapBuffers, but that does not work either. So what I am doing wrong?

    I have not been able to find much info concerning Qt + OpenGL on internet. Any help would be greatly appreciated. Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Qt and OpenGl (I'm lost)

    Example:

    Qt Code:
    1. void GLWidget::paintEvent(QPaintEvent *event)
    2. {
    3. makeCurrent();
    4.  
    5. // Your gl painting
    6.  
    7. myGlItem->drawSomeCoolGlGraphics();
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt and OpenGl (I'm lost)

    Hi,

    Thank you for your answer tbscope, unfortunately I have still not been able to make it work. Here is my code so far, maybe you or someone else can check it and tell me what I am doing wrong.

    Thanks a lot.

    GLWidget.h :
    Qt Code:
    1. #ifndef HEADER_GLWIDGET
    2. #define HEADER_GLWIDGET
    3.  
    4. #include <QGLWidget>
    5.  
    6. class GLWidget : public QGLWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public :
    11. GLWidget( QWidget * parent = NULL );
    12. ~GLWidget();
    13.  
    14. protected :
    15. void paintEvent(QMouseEvent *event);
    16. void initializeGL();
    17. void paintGL();
    18. void resizeGL( int width, int height );
    19. QSize minimumSizeHint() const;
    20. QSize sizeHint() const;
    21.  
    22. private :
    23.  
    24. };
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 

    GLWidget.cpp :
    Qt Code:
    1. #include <QtGui>
    2. #include <QtOpenGL>
    3. #include "GLWidget.h"
    4.  
    5. static int WINDOW_WIDTH = 400;
    6. static int WINDOW_HEIGHT = 400;
    7.  
    8. using namespace std;
    9.  
    10. GLWidget::GLWidget( QWidget * parent ) : QGLWidget( parent )
    11. {
    12.  
    13. }
    14.  
    15. GLWidget::~GLWidget()
    16. {
    17.  
    18. }
    19.  
    20. void GLWidget::initializeGL()
    21. {
    22. // Background color
    23. glClearColor(1.0f, 0.0f, 0.0f, 1.0f );
    24.  
    25. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    26.  
    27. glMatrixMode( GL_PROJECTION );
    28. glLoadIdentity();
    29.  
    30. gluPerspective(70,(double)WINDOW_WIDTH/WINDOW_HEIGHT,1,1000);
    31. gluLookAt(0,0,5,0,0,0,0,1,0);
    32.  
    33. glMatrixMode( GL_MODELVIEW );
    34. glLoadIdentity();
    35.  
    36. }
    37.  
    38. void GLWidget::paintGL()
    39. {
    40.  
    41. glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    42. glClear (GL_COLOR_BUFFER_BIT);
    43.  
    44. glColor3fv (m_color);
    45. glBegin(GL_QUADS);
    46. glVertex3f (0.25, 0.25, 0.0);
    47. glVertex3f (0.75, 0.25, 0.0);
    48. glVertex3f (0.75, 0.75, 0.0);
    49. glVertex3f (0.25, 0.75, 0.0);
    50. glEnd();
    51.  
    52. }
    53.  
    54. void GLWidget::paintEvent(QMouseEvent *event)
    55. {
    56. makeCurrent();
    57.  
    58. // Your gl painting
    59.  
    60. glBegin(GL_LINES);
    61. // x (blue)
    62. glColor3ub(0,0,255);
    63. glVertex3i(0,0,0);
    64. glVertex3i(1,0,0);
    65. // y (green)
    66. glColor3ub(0,255,0);
    67. glVertex3i(0,0,0);
    68. glVertex3i(0,1,0);
    69. // z (red)
    70. glColor3ub(255,0,0);
    71. glVertex3i(0,0,0);
    72. glVertex3i(0,0,1);
    73. glEnd();
    74.  
    75. }
    76.  
    77. void GLWidget::resizeGL(int width, int height)
    78. {
    79. glViewport(0, 0, width, height);
    80. }
    81.  
    82. QSize GLWidget::minimumSizeHint() const
    83. {
    84. return QSize(50, 50);
    85. }
    86.  
    87. QSize GLWidget::sizeHint() const
    88. {
    89. return QSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    90. }
    To copy to clipboard, switch view to plain text mode 

    I changed the
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    for
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    to make it work for my test, otherwise nothing was showing up. Now the code inside painGL() works, but not the one inside paintEvent().

  4. #4
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt and OpenGl (I'm lost)

    Sorry to bump this, I really need help figuring this out otherwise I can't even get started.

    Maybe if you don't feel like answering my question, please reply with a link to some tutorial or example that would help me understand why my opengl calls are not working outside paintGL.

    Sorry again for the bump and thanks in advance.

  5. #5
    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: Qt and OpenGl (I'm lost)

    Hi!

    To my knowledge that's exactly how it's supposed to work. Why do you want to mix philosophies here? paintGL is fine for pure raw openGL.

    Use a widgets paintevent if you want to use QPainter. BTW: Qt's OpenGL QPainter implementation is quite good. So it might be a lot easier and more portable to use QPainter and use QGraphicsView with an OpenGL Viewport.

    Have a look at this: http://www.qtcentre.org/threads/2999...ht=#post140754

    Maybe you should describe what you want to do, and then we can find a solution.

    Johannes

  6. #6
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt and OpenGl (I'm lost)

    Hi JohannesMunk,

    Basically I want to create an application to illustrate certain aspects of linear algebra : draw vectors, planes, vector/dot product, point closer to a line, etc. I am mostly doing this to learn, I love math et want to learn Qt and OpenGL to a higher level (this is the reason for mixing the philosophies).

    I will have a look at the link you posted as soon as I get home and post back. Thanks a lot.

  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: Qt and OpenGl (I'm lost)

    Ok! Then I would highly recommend to use the comfortable opengl wrapping that QPainter allows:

    In another thread just yesterday I set up a simple OpenGL GraphicsView:

    http://www.qtcentre.org/threads/3424...476#post158476

    Instead of a button-widget you will add lots of custom QGraphicsItems for the different mathematical objects.

    Use scene->addItem(new Vector(QVector3D(..)));

    Qt Code:
    1. class Vector : public QGraphicsItem
    2. {
    3. public:
    4. Vector(QVector3D a);
    5. QRectF boundingRect() const{ return QRectF( ... ); }
    6.  
    7. void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
    8. {
    9. painter->drawLine(.. );
    10. }
    11. };
    To copy to clipboard, switch view to plain text mode 

    Let me know if you run into more problems!

    Joh

Similar Threads

  1. Hightlight not lost
    By wirasto in forum Qt Programming
    Replies: 0
    Last Post: 5th August 2010, 13:16
  2. Focus lost
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 25th January 2010, 22:07
  3. Completely lost!!!
    By Zuks in forum Newbie
    Replies: 4
    Last Post: 2nd December 2009, 09:58
  4. lost signals ?
    By deepinlife in forum Qt Programming
    Replies: 3
    Last Post: 17th June 2008, 10:11
  5. Lost widget
    By anli in forum Qt Programming
    Replies: 6
    Last Post: 29th July 2006, 19:02

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.