Results 1 to 9 of 9

Thread: how to write QT openGL code in two classes

  1. #1
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to write QT openGL code in two classes

    I want to create large openGL models, so I want to divide it in multiple classes. For that, I wrote two classes,

    Qt Code:
    1. /////////////////// glhandle.cpp ////////////////////////////////////////
    2.  
    3. #include "glhandle.h" //Contains defn of GLHandle class
    4. #include "buildingpainter.h"
    5.  
    6. void GLHandle::paintGL() {
    7. //SOME DRAWING CODE HERE
    8. BuildingPainter p=BuildingPainter p=BuildingPainter(this->context());
    9. p.draw();
    10. }
    11.  
    12. //ResizeGL(), initializeGL() and other functions
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. ////////////////// BuildingPainter.cpp ///////////////////////////////
    2.  
    3. #include "buildingpainter.h" //Defn of BuildingPainter class
    4.  
    5. BuildingPainter::BuildingPainter(QWidget *parent) :
    6. QGLWidget(parent) {
    7. }
    8.  
    9. // This constructor is called: So that openGL states should be copied
    10. BuildingPainter::BuildingPainter(const QGLContext *refs): QGLWidget(refs) {
    11. }
    12.  
    13. void BuildingPainter::draw() {
    14. //More drawing Code here
    15. }
    To copy to clipboard, switch view to plain text mode 

    However, I am getting compile error:
    error: ‘QGLWidget::QGLWidget(const QGLWidget&)’ is private
    in definition of BuildingPainter.

    What is right way to do it so that I can render different parts of my model from different classes?
    Last edited by qtUse; 30th August 2010 at 19:24.

  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: how to write QT openGL code in two classes

    The constructor you use is private, you need to use a public constructor.

    Qt Code:
    1. QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
    To copy to clipboard, switch view to plain text mode 

    May I ask what type of program you're making? I see a class BuildingPainter which interests me.

  3. #3
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to write QT openGL code in two classes

    Thanks for reply, tbscope

    I tried function you mentioned above which threw an error as:
    error: no matching function for call to ‘BuildingPainter::BuildingPainter(QGLFormat, GLHandle* const)’
    Can you point me to sample use of function you mentioned??
    Also will it be able to take OpenGL context (openGL states) from calling function?

    BTW, I am trying to model real world scenes(Park) in openGL and "BuildingPainter" creates "Building" model (Basic modelling only, by cubes etc.)

  4. #4
    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: how to write QT openGL code in two classes


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

    Default Re: how to write QT openGL code in two classes

    But HelloGL uses different GL interfacing class-'GLWidget' and I am using 'QGLWidget', so constructor is not found in my case.
    Also helloGL handles all openGL calls from only one class.
    Also in QGLWidget has constructors
    Qt Code:
    1. QGLWidget ( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 )
    2. QGLWidget ( QGLContext * context, QWidget * parent, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 )
    To copy to clipboard, switch view to plain text mode 
    as public member (see this: http://doc.trolltech.com/3.3/qglwidget.html), so it should not cause problem.

    Is there any alternate method to access and modify QGLWidget from different class?

  6. #6
    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: how to write QT openGL code in two classes

    I suggest a good C++ book.

    Are you using Qt3 or Qt4?
    The hellogl example uses QGLWidget like you are. But it shows you how to use the QGLWidget constructor. It doesn't matter if you split your program in different parts.

    C++ contains several ways to exchange information between different objects or different classes.

  7. #7
    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: how to write QT openGL code in two classes

    By the way, I think you handle this a bit in the wrong way.

    You should only have one GL Widget.
    Then you can create different classes that do different things. Like a Building class or a Car class which each can serialise data and create the GL code. Then you can paint that GL code on the widget. There are several techniques and programming patterns you can use for this.

  8. The following user says thank you to tbscope for this useful post:

    qtUse (30th August 2010)

  9. #8
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to write QT openGL code in two classes

    Ohhhh .....I verified, HelloGL's using QGLWidget only as you said
    I tried according to it and it's finely taking GL contexts from calling function

    Thanks for your prompt help, tbscope

    @second post:
    But in order to draw, say car, I need to paint different sides in car drawing class only (to avoid passing all its vertices to single QGLWidget), so GL context must be available to my BuildingPainter class. So I am using multiple OGLWidgets
    Last edited by qtUse; 30th August 2010 at 21:12.

  10. #9
    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: how to write QT openGL code in two classes

    I would probably go with the Model and View pattern. The view being the QGLWidget and the model a tree of objects.

    Example:
    Qt Code:
    1. +------------------+
    2. | MyGlModel |
    3. | |
    4. | Rootitem | +-------------+
    5. | | | | MyGlView |
    6. | +- Item | | |
    7. | | |----->| setModel() |
    8. | +- Item | | |
    9. | | | | |
    10. | + Item | +-------------+
    11. | |
    12. +------------------+
    13.  
    14.  
    15.  
    16.  
    17.  
    18. +------------------+ +-------------------+
    19. | MyGlView | | MyGlItem |
    20. | | | |
    21. | paintGl() | | drawItem(context) |
    22. | | |----->| |
    23. | +- iterate | | |
    24. | over model | +-------------------+
    25. | |
    26. +------------------+
    27.  
    28.  
    29.  
    30. void MyGlView::drawGl()
    31. {
    32. // recursive call each item in the model tree
    33.  
    34. // For each item:
    35. item->drawItem(context(), /*maybe other stuff*/);
    36. }
    37.  
    38. void MyGlItem::drawItem(QGLContext *context, /*maybe other things*/)
    39. {
    40. // Do the GL Painting for this item based on the context.
    41. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. linking the files with openGL code
    By Abeer in forum Newbie
    Replies: 1
    Last Post: 25th May 2010, 01:10
  2. Replies: 0
    Last Post: 6th December 2009, 00:41
  3. Qt with OpenGL ES for ARM9 - All OpenGL ES tests have failed!
    By vinpa in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 3rd December 2009, 10:10
  4. Code Completion in UI classes
    By ChrisW67 in forum Qt Tools
    Replies: 5
    Last Post: 19th June 2009, 00:26

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.