Results 1 to 9 of 9

Thread: QPainter::drawText

  1. #1
    Join Date
    Aug 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPainter::drawText

    I am doing following steps;

    QPainter p;
    p.begin(myGlWidget);
    ...
    // get the width and height from the glcontext
    const QGLContext *glcx = myGlWidget->context();
    width = glcx->device()->width();
    height = glcx->device()->height()
    ...
    // setting some openGL state
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, width, height);
    gluProject(x, y, z, &m[0][0], &p[0][0], &v[0], &w_x, &w_y, &w_z);
    w_y = height - w_y;
    // ??? following line does not draw text on glwidget, any reason ???
    p.drawText(QPointF(w_x, w_y), QString(text)); =================== (A)
    // restoring the openGL state
    p.end();

    Please let me know why above line (A) didn't rendered the text on glwidget.

    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter::drawText

    why are you mixing QPainter methods and GL calls?

    The doc clearly says that :

    • 3D drawing happen through 3 specific virtual protected methods and require GL calls (you may also do 2D there but that's not the point)
    • 2D drawing can be done in paintEvent() as usual WITHOUT any GL calls. QPainter automatically use a GL-based paint engine when operating on a QGLWidget to accelerate rendering so mixing QPainter/GL in paintEvent is both useless and a potential source of errors

    http://doc.trolltech.com/4.3/qglwidget.html#details

    p.s : use the [ c o d e ] tag to enclose source code you past in a post.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Aug 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter::drawText

    Hi,

    Qt Code:
    1. p.begin(myGlWidget);
    2. ...
    3. // get the width and height from the glcontext
    4. const QGLContext *glcx = myGlWidget->context();
    5. width = glcx->device()->width();
    6. height = glcx->device()->height()
    7. ...
    8. // setting some openGL state
    9. glMatrixMode(GL_PROJECTION);
    10. glLoadIdentity();
    11. glViewport(0, 0, width, height);
    12. gluProject(x, y, z, &m[0][0], &p[0][0], &v[0], &w_x, &w_y, &w_z);
    13. w_y = height - w_y;
    14. // ??? following line does not draw text on glwidget, any reason ???
    15. p.drawText(QPointF(4.5, -1.6), QString(text)); =================== (A)
    16. p.drawLine(QRect(0, 0, w_x, w_y)); ============== (B)
    17. // restoring the openGL state
    18. p.end();
    To copy to clipboard, switch view to plain text mode 

    But when I try with drawLine, it works and displays the diagonal line.
    When I tried with drawText worked but it displays the text at very top-left. How can I convert 4.5 and -1.6 into window co-ordinates ?

    Thank you.

  4. #4
    Join Date
    Aug 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter::drawText

    Hi,

    Please igone my previous code...

    Qt Code:
    1. p.begin(myGlWidget);
    2. ...
    3. // get the width and height from the glcontext
    4. const QGLContext *glcx = myGlWidget->context();
    5. width = glcx->device()->width();
    6. height = glcx->device()->height()
    7. ...
    8. // setting some openGL state
    9. glMatrixMode(GL_PROJECTION);
    10. glLoadIdentity();
    11. glViewport(0, 0, width, height);
    12. gluProject(x, y, z, &m[0][0], &p[0][0], &v[0], &w_x, &w_y, &w_z);
    13. w_y = height - w_y;
    14. // At this point it w_x, w_y should have been in windows
    15. // co-orrdinates... but still the following line draws the text at
    16. // very top-left in glwidget, any reason ???
    17. p.drawText(QPointF(w_x, w_y), QString(text));=========== (A)
    18. p.drawLine(QRect(0, 0, width, height));================ (B)
    19. // restoring the openGL state
    20. p.end();
    To copy to clipboard, switch view to plain text mode 


    So here the drawLine works fine and displays the diagonal line.

    But drawText displays the text at very top-left of the glwidget.
    1) Is it the problem that w_x, w_y not converted into windows co-ordinate?
    2) lets say glwidget width x height is 200 x 200, then suppose I want to place the text at x=4.5 and y=-1.6, then how can I convert x, y into window co-ordinates ? any steps ?

    Thank you.

  5. #5
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter::drawText

    I suppose that you should use:
    QGLWdiget : : renderText ( double x, double y, double z, const QString & str, const QFont & font = QFont(), int listBase = 2000 )

    and glLines(..), glPoints(...), .etc

  6. #6
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter::drawText

    Quote Originally Posted by h123 View Post
    Hi,

    Please igone my previous code...

    Qt Code:
    1. p.begin(myGlWidget);
    2. ...
    3. // get the width and height from the glcontext
    4. const QGLContext *glcx = myGlWidget->context();
    5. width = glcx->device()->width();
    6. height = glcx->device()->height()
    7. ...
    8. // setting some openGL state
    9. glMatrixMode(GL_PROJECTION);
    10. glLoadIdentity();
    11. glViewport(0, 0, width, height);
    12. gluProject(x, y, z, &m[0][0], &p[0][0], &v[0], &w_x, &w_y, &w_z);
    13. w_y = height - w_y;
    14. // At this point it w_x, w_y should have been in windows
    15. // co-orrdinates... but still the following line draws the text at
    16. // very top-left in glwidget, any reason ???
    17. p.drawText(QPointF(w_x, w_y), QString(text));=========== (A)
    18. p.drawLine(QRect(0, 0, width, height));================ (B)
    19. // restoring the openGL state
    20. p.end();
    To copy to clipboard, switch view to plain text mode 


    So here the drawLine works fine and displays the diagonal line.

    But drawText displays the text at very top-left of the glwidget.
    1) Is it the problem that w_x, w_y not converted into windows co-ordinate?
    2) lets say glwidget width x height is 200 x 200, then suppose I want to place the text at x=4.5 and y=-1.6, then how can I convert x, y into window co-ordinates ? any steps ?

    Thank you.
    where did you put these code? gl***() could only be called in QGLWidet's members and
    Qt Code:
    1. p.begin(myGlWidget);
    To copy to clipboard, switch view to plain text mode 
    is obvisouly cannot be there. i'm wondering what does painter do?

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter::drawText

    actually QGLWidget specialized methods that initialize GL context and render things using GL commands (I mean the methods where you can and are supposed to do this) are most likely called from the default paintEvent() which makes it possible to initialize a QPainter in them but, again, it is NOT the right thing(tm) to do...

    If all you need is simple 2D drawing QGLWidget does all the needed GL calls under the hood when you simply use QPainter inside the paintEvent().

    If you need some complex rendering to be done use these methods instead (do not touch the painEvent()) as recommended by the docs :
    • paintGL() - Renders the OpenGL scene. Gets called whenever the widget needs to be updated.
    • resizeGL() - Sets up the OpenGL viewport, projection, etc. Gets called whenever the the widget has been resized (and also when it is shown for the first time because all newly created widgets get a resize event automatically).
    • initializeGL() - Sets up the OpenGL rendering context, defines display lists, etc. Gets called once before the first time resizeGL() or paintGL() is called.

    And avoid mixing GL calls with QPainter. Even if it works it is USELESS and a clear design failure... Anything QPainter does is done using GL commands so if you're doing advanced GL rendering you certainly know how to do some simple 2D painting directly using GL or you use some of the convenience methods of QGLWidget (eg QGLWidget::renderText())

    Last but not least, GL coordinates and windows coordinates are not the same : Windows use a right/down base whose origin is at the top left corner while GL use a right/up base whose origin is a the center of the rendering context. Going from one to another requires the composition of two basic transforms : translation and symetry of axe (Ox)
    Current Qt projects : QCodeEdit, RotiDeCode

  8. #8
    Join Date
    Aug 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter::drawText

    Hi fullmetalcoder,

    Thank you for your detailed explanation.

    > Anything QPainter does is done using GL commands so if you're
    > doing advanced GL rendering you certainly know how to do some
    > simple 2D painting
    The basic requirement is to draw the text on the QGLWidget.

    I have already done experiments on QGLWidget::renderText(), but it is tooooo slow, while performing rotation with text.

    Then I looked into the internals of the renderText.
    I looked into the Qt443 code and found that in renderText() it uses the QPainter::drawText(). So I am trying to simulate the same code in my program, but I am facing this problem.


    Can you please let me know...
    1) how can I optimize the performance of renderText()?
    2) can I get better performance if I use QPainter::drawText on QGLWidget?
    3) can I get still better performance if I use QPaintEngine::drawTextItem() on QGLWidget, pl. provide example.

    And finally I don't won't to go with third-party libraries like FontGL/FTGL.

    Thank you.

  9. #9
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter::drawText

    the performances of QLWidget depends of several factors that do not affect (or not as noticably) regular widgets :

    • GPU
    • OpenGL implementation/ GPU drivers
    • AA settings

    I don't have Qt source code on my box atm so I can't comment about renderText()/drawText() though there should not be a huge difference.

    As for coordinate transform this should do :
    Qt Code:
    1. QPointF GLToWindow(const QPointF& p, int width, int height)
    2. {
    3. return QPointF(width - p.x(), height - p.y());
    4. }
    5. QPointF WindowToGL(const QPointF& p, int width, int height)
    6. {
    7. return QPointF(width - p.x(), height - p.y());
    8. }
    To copy to clipboard, switch view to plain text mode 
    Yes the two functions do exactly the same but having a different name might help to keep the code readable.

    So I think the problem actually lies in this line or in any of the preceding lines that setp up the parameters of the function :
    Qt Code:
    1. gluProject(x, y, z, &m[0][0], &p[0][0], &v[0], &w_x, &w_y, &w_z);
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. HTML text drawn with QPainter::drawText()
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2008, 01:23
  2. Replies: 4
    Last Post: 18th December 2007, 08:31
  3. Change font in QPainter::drawText
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 19th May 2006, 16:21

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.