Results 1 to 8 of 8

Thread: QGLWidget renderText() problem in openGL.

  1. #1
    Join Date
    Jul 2010
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGLWidget renderText() problem in openGL.

    Hi Friends,

    I am getting problem using the render text function in openGL.

    Actually I want to use openGL with QGraphicsView in my application. So I am using class QGraphicsItem to achieve this. I have used renderPixmap() function. But when I execute my function I got the text but soon there is a crash.

    I have search across the forum and found that

    "renderText is 8-bit and can only use "OpenGL compatible fonts".

    So I have used the following code to do this:

    QFont myFont("Verdana", 24);
    myFont.setStyleStrategy(QFont.OpenGLCompatible);
    renderText(20,20, 0,"test", myFont);
    What can be cause of the problem...

    I have also attached the code [Older one already posted in previous thread].

    My QT version is "Qt by Nokia v4.7.0 (VS2008)"
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QGLWidget renderText() problem in openGL.

    What happens if you use

    Qt Code:
    1. renderText(20,20, 0,"test");
    To copy to clipboard, switch view to plain text mode 
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  3. #3
    Join Date
    Jul 2010
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget renderText() problem in openGL.

    Quote Originally Posted by john_god View Post
    What happens if you use

    Qt Code:
    1. renderText(20,20, 0,"test");
    To copy to clipboard, switch view to plain text mode 
    I have tried this["renderText(20,20, 0,"test")"] also and even renderText(20,20,"test"), but no result...again application crashed....

    Is anything I am missing????

  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: QGLWidget renderText() problem in openGL.

    Can you post the crash report?

  5. #5
    Join Date
    Jul 2010
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget renderText() problem in openGL.

    Quote Originally Posted by tbscope View Post
    Can you post the crash report?
    Hi tbscope,

    Thanks for the reply...I have attached the crash report...[Image + Call Stack + Error Report]

    ScreenShot.jpg7339_appcompat.txtCall Stack.txt

  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: QGLWidget renderText() problem in openGL.

    I can't see a problem in the stack trace.

    Can you post the full code please (make it minimal and compilable)?

  7. #7
    Join Date
    Jul 2010
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget renderText() problem in openGL.

    Quote Originally Posted by tbscope View Post
    I can't see a problem in the stack trace.

    Can you post the full code please (make it minimal and compilable)?
    Once again thanks for the reply...

    I have attached the code along with this mail, and also I have added the snippet of code for your reference...

    Qt Code:
    1. //main.cpp
    2. #include "openglsceneexample.h"
    3.  
    4. #include <QtGui>
    5. #include <QApplication>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10. OpenGLSceneExample w;
    11. w.show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //openglsceneexample.cpp
    2.  
    3. #include "openglsceneexample.h"
    4.  
    5. #include <QtGui>
    6. #include <QGLWidget>
    7.  
    8. ////////////////////////////////////////////////////////////////////////////////////////////////
    9. //////////////////// OPEN GL WIDGET
    10.  
    11. CEasyGLWidget::CEasyGLWidget(QWidget *parent)
    12. : QGLWidget(QGLFormat(QGL::SampleBuffers/*|QGL::AlphaChannel|QGL::HasOverlay|QGL::Rgba*/),parent)
    13.  
    14. {
    15. m_pOGLObject = 0;
    16. }
    17.  
    18. CEasyGLWidget::~CEasyGLWidget()
    19. {
    20.  
    21. }
    22.  
    23.  
    24. /*! \brief Draws a single circular or elliptical arc.
    25.  *
    26.  *\param cx Upper Left corner of the in x coordinates
    27.  *\param cy Upper Left corner of the in y coordinates
    28.  *\param r Radius of the arc.
    29.  *\param start_angle Start position in angles (radian)
    30.  *\param arc_angle Angle of the Arc in radian
    31.  *\param num_segments Point count, which represent the arc
    32.  */
    33. void CEasyGLWidget::DrawArc(GLdouble cx, GLdouble cy, GLdouble r, GLdouble start_angle, GLdouble arc_angle, int num_segments)
    34. {
    35. //Theta is calculated from the arc angle, the -1 bit comes from the fact that the arc is open
    36. GLdouble theta = arc_angle / GLdouble(num_segments -1);
    37. GLdouble tangetial_factor = tanf(theta);
    38.  
    39. GLdouble radial_factor = cosf(theta);
    40.  
    41. //start at the start angle
    42. GLdouble x = r * cosf(start_angle);
    43. GLdouble y = r * sinf(start_angle);
    44.  
    45. glBegin(GL_LINE_STRIP);
    46.  
    47. for(int i = 0; i < num_segments; i++)
    48. {
    49. glVertex2f(x + cx + r, y + cy +r); //, 0.5);
    50.  
    51. GLdouble tx = -y;
    52. GLdouble ty = x;
    53.  
    54. x += tx * tangetial_factor;
    55. y += ty * tangetial_factor;
    56.  
    57. x *= radial_factor;
    58. y *= radial_factor;
    59. }
    60.  
    61. glEnd();
    62. }
    63.  
    64.  
    65. /*! \brief Draws a single line.
    66.  *
    67.  * \param[in] cStartPoint Start Point of the line to be drawn.
    68.  * \param[in] cDestPoint Destination Point of the line to be drawn.
    69.  */
    70. void CEasyGLWidget::DrawLine(QPointF cStartPoint, QPointF cDestPoint)
    71. {
    72. glBegin(GL_LINE_STRIP);
    73.  
    74. glVertex2f(cStartPoint.rx(), cStartPoint.ry());
    75. glVertex2f(cDestPoint.rx(), cDestPoint.ry());
    76.  
    77. glEnd();
    78. }
    79.  
    80. void CEasyGLWidget::initializeGL()
    81. {
    82. qglClearColor( QColor(90,125,164,255)); //set background color
    83.  
    84. qglColor(QColor(255,0,0,255));
    85.  
    86. //create a display list containing information about the object we want to display
    87. m_pOGLObject = glGenLists(1);
    88.  
    89. glNewList(m_pOGLObject, GL_COMPILE);
    90. DrawArc(0.0, 0.0, 30.0, 0.0, 2*PI, 60);
    91.  
    92. DrawLine(QPointF(0.0,0.0), QPointF(200.0,200.0));
    93. glEndList();
    94.  
    95. //activate alpha values for transparent colors of the bitmaps
    96. glEnable(GL_ALPHA_TEST);
    97. glAlphaFunc(GL_GREATER,0.1);
    98.  
    99. //set up the rendering process to use a particular shading model and rendering flags:
    100. glShadeModel(GL_FLAT);
    101. }
    102.  
    103. void CEasyGLWidget::resizeGL(int iWidth, int iHeight)
    104. {
    105. // Set size of the view port (display size)
    106. glViewport(0,0,(int)MIN(iWidth,iHeight), (int)MIN(iWidth,iHeight)); // always use width=height
    107.  
    108. glMatrixMode(GL_PROJECTION); //select protection matrix
    109. glLoadIdentity(); //reset matrix position to start
    110.  
    111. // Open gl always draws on the same matrix (width x height = fix defined hud size)
    112. // the size is defined by the Ortho Matrix.
    113. // This matrix is scaled to the destination size.
    114. glOrtho(0, 400,400, 0, -1,1);
    115.  
    116. glMatrixMode(GL_MODELVIEW); //select model view (result) matrix
    117. }
    118.  
    119. void CEasyGLWidget::paintGL()
    120. {
    121.  
    122. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear background using background color
    123. glLoadIdentity(); //reset matrix position to start
    124. glTranslatef(0.0, 0.0, 0.0) ; // move object to dest position
    125. glCallList(m_pOGLObject); // call the display list containing the rendering commands for the object
    126.  
    127. QFont myFont("Verdana", 24);
    128. myFont.setStyleStrategy(QFont.OpenGLCompatible);
    129. renderText(20,20, 0,"test", myFont);
    130.  
    131. }
    132.  
    133.  
    134. /////////////////////////////////////////////////////////////////////////////////////////////
    135. /////////////////////////// ITEM
    136.  
    137. CEasyGraphicsItem::CEasyGraphicsItem(QWidget *parent)
    138. {
    139. m_OGLItem = new CEasyGLWidget();
    140. m_OGLItem->resize(200,200);
    141. m_Pixmap = m_OGLItem->renderPixmap(600,600);
    142. }
    143. CEasyGraphicsItem::~CEasyGraphicsItem()
    144. {
    145.  
    146. }
    147.  
    148. void CEasyGraphicsItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
    149. {
    150. QRectF rect(0.0,0.0,200.0,200.0);
    151.  
    152. //Works fine !!
    153. //painter->drawArc(rect,0,16*360);
    154.  
    155. // Open gl element is not displayed!!!
    156. //m_pPixmap = QPixmap::grabWidget(m_OGLItem,0,0,600,600);
    157. m_Pixmap = m_OGLItem->renderPixmap(600,600);
    158. painter->drawPixmap(0,0,600,600,m_Pixmap);
    159. //m_Image = m_OGLItem->grabFrameBuffer();
    160. //painter->drawImage(rect, m_Image);
    161. }
    162.  
    163. QRectF CEasyGraphicsItem::boundingRect() const
    164. {
    165. return QRectF(0,0,600,600); //m_CurrWindowWidth,m_CurrWindowHeight);
    166. }
    167. ////////////////////////////////////////////////////////////////////////////////////////////////////
    168. ////////////////// MAIN Window
    169.  
    170. OpenGLSceneExample::OpenGLSceneExample(QWidget *parent)
    171. : QMainWindow(parent)
    172. {
    173. // ui.setupUi(this);
    174.  
    175. m_pMainGraphicsScene = new QGraphicsScene();
    176. m_pMainGraphicsScene->setSceneRect(QRectF(0,0,630,630)); // Dummy window size
    177. m_pMainGraphicsView = new QGraphicsView(m_pMainGraphicsScene);
    178.  
    179. m_pMainGLWidget = new QGLWidget();
    180. //m_pMainGraphicsView->setViewport(m_pMainGLWidget);
    181.  
    182. setCentralWidget(m_pMainGraphicsView);
    183.  
    184.  
    185. // Add open gl item to view (not part of the scene!!) works fine!
    186. /*m_OGLItem = new CEasyGLWidget(m_pMainGraphicsView);
    187. m_OGLItem->resize(400,400);
    188. m_pMainGraphicsScene->addWidget(m_OGLItem);
    189. */
    190.  
    191. // add graphics item which contain a open gl element
    192. m_pItem = new CEasyGraphicsItem();
    193. m_pMainGraphicsScene->addItem(m_pItem);
    194.  
    195.  
    196. }
    197.  
    198. OpenGLSceneExample::~OpenGLSceneExample()
    199. {
    200.  
    201. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //openglsceneexample.h
    2.  
    3. #ifndef OPENGLSCENEEXAMPLE_H
    4. #define OPENGLSCENEEXAMPLE_H
    5.  
    6. #include <QtGui>
    7. #include <QGLWidget>
    8.  
    9. //#include "ui_openglsceneexample.h"
    10. #define MIN(a,b) ((a < b)? a : b)
    11. #define PI 3.14159265358979323846 /*!< constant PI */
    12.  
    13. class CEasyGLWidget : public QGLWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18.  
    19. CEasyGLWidget(QWidget *parent = 0);
    20. ~CEasyGLWidget();
    21.  
    22. void initializeGL();
    23. void resizeGL(int w, int h);
    24. void paintGL();
    25.  
    26. void DrawArc(GLdouble cx, GLdouble cy, GLdouble r, GLdouble start_angle, GLdouble arc_angle, int num_segments);
    27. void DrawLine(QPointF cStartPoint, QPointF cDestPoint);
    28. private:
    29.  
    30. GLuint m_pOGLObject;
    31.  
    32. };
    33.  
    34.  
    35. class CEasyGraphicsItem : public QGraphicsItem
    36. {
    37. //Q_OBJECT
    38.  
    39. public:
    40. CEasyGraphicsItem(QWidget *parent = 0);
    41. ~CEasyGraphicsItem();
    42.  
    43. virtual QRectF boundingRect() const;
    44. virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
    45.  
    46. private:
    47. CEasyGLWidget *m_OGLItem;
    48. QPixmap m_Pixmap;
    49. QImage m_Image;
    50.  
    51. };
    52.  
    53.  
    54. class OpenGLSceneExample : public QMainWindow
    55. {
    56. Q_OBJECT
    57.  
    58. public:
    59. OpenGLSceneExample(QWidget *parent = 0);
    60. ~OpenGLSceneExample();
    61.  
    62. private:
    63. //Ui::OpenGLSceneExampleClass ui;
    64.  
    65.  
    66. QGraphicsScene *m_pMainGraphicsScene;
    67. QGraphicsView *m_pMainGraphicsView;
    68. QGLWidget *m_pMainGLWidget;
    69.  
    70. CEasyGraphicsItem *m_pItem;
    71.  
    72. CEasyGLWidget *m_OGLItem;
    73. };
    74.  
    75. #endif // OPENGLSCENEEXAMPLE_H
    To copy to clipboard, switch view to plain text mode 

    OpenGLScene.zip

  8. #8
    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: QGLWidget renderText() problem in openGL.

    Your problem is the use of m_pOGLObject and gl...list functions.

    I don't know enough of OpenGl to help you with that. But I guess switching displays inside the paint function is the problem (if that is what glCallList does)

Similar Threads

  1. Qt3 -> Qt4 QGLWidget::renderText problem
    By cometlinear in forum Qt Programming
    Replies: 6
    Last Post: 1st October 2006, 01:52
  2. QGLWidget renderText()
    By ToddAtWSU in forum Qt Programming
    Replies: 8
    Last Post: 21st July 2006, 13:20
  3. QGLWidget renderText
    By Rayven in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2006, 21:02
  4. QGLWidget renderText problem
    By mbjerkne in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2006, 21:35
  5. About QGLWidget::renderText
    By showhand in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 13:50

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.