Results 1 to 9 of 9

Thread: How to draw freetype fonts on QGraphicsScene?

  1. #1
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to draw freetype fonts on QGraphicsScene?

    Hey i am trying to draw freetype font on QGraphicsScene....

    I have attached my header and source files for program.

    I am using freetype lib functions to get glyph outlins for a given character from a give font
    I am drawing character on image then converting it to pixmap and drawing that pixmap on scene.
    is this the right way to draw fonts on QGraphicsScene?
    how to make that character move, or select or re scale?
    what is the another way to draw fonts in Qt?

    I also want to pop up this font drawing window when i click on the option or say button of my base aplication.
    how to do that?
    i tried doing that also ...even the window comes up but when I close the window...the 'Application output' tab shows that 'The program has unexpectedly finished' .
    I can't figure out why is that?

    how to solve these problems??
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw freetype fonts on QGraphicsScene?

    Quote Originally Posted by sarbh20ss View Post
    is this the right way to draw fonts on QGraphicsScene?
    No. Use QGraphicsSimpleTextItem.

    I also want to pop up this font drawing window when i click on the option or say button of my base aplication.
    how to do that?
    Use signals and slots.

    i tried doing that also ...even the window comes up but when I close the window...the 'Application output' tab shows that 'The program has unexpectedly finished' .
    I can't figure out why is that?
    You can use a debugger to debug your code to see where it crashes and why.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to draw freetype fonts on QGraphicsScene?

    hey thanks for quick reply.

    I read the documentation of QGraphicsSimpleTextItem.
    It allows to draw simple text with given font,pen.

    But in my program I want to render the glyph outlines read from a font file for a give character.
    So i don't directly have a text to draw but I want to draw these glyphs on scene so that I can make it selectable,movable, re scalable.
    Further when this succeeds I want to implement a logic to edit these outlines.
    So is it possible to do this with QGraphicsScene.
    I want to do my whole application in Qt because it is very simple to develop GUI using Qt.

    I am using Signals and Slots to pop out the window but as I mentioned it gives me the output as 'The program has unexpectedly finished'.

    So how do I solve this problem?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw freetype fonts on QGraphicsScene?

    You can draw the character on a painter path and then convert that path to a curve. That is essentially what the text item does.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to draw freetype fonts on QGraphicsScene?

    Hey sorry for late reply..got busy with some work at home.
    After I followed your advice I did this:

    QString myfname="FreeMono.ttf";
    QRawFont *myfont=new QRawFont(myfname,324,QFont::PreferDefaultHinting);
    //this creates a valid raw font
    myfont->glyphIndexesForChars(mychars,numofchars,myglyphin dexes,&numofglyphs);
    //this gives me glyph indexes for characters I stored in array 'mychars'
    QPainterPath mypath=myfont->pathForGlyph(myglyphindexes[0]);
    //this path I am drawing on pixmap using painter.

    With this I was able to draw the glyph outline for a character at given index.
    I am setting this pixmap to Graphics Scene.

    is this the right way?
    Also how will I come to know that how that outline is being drawn?
    I mean how many curves or arcs it contains?

    Thanks in advance for help.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw freetype fonts on QGraphicsScene?

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to draw freetype fonts on QGraphicsScene?

    Hey I am able to get no. of elements and elements at given index.

    mypath.elementCount();
    //this return count as 49
    mypath.elementAt(i);
    //return the QPointF values for i from 0 to 48;

    now is this the right way to use 'elementAt(index)' function?

    I am storing these points in array. I am passing these points to path with lineTo() function and drawing this path on a pixmap. I do this for all 49 points. This draws outline of given character.
    But when I change the no of iterations to even as less as 2 it still draws the outline correctly. Also when I change the function from 'lineTo( QPointF )' to 'moveTo( QPointF ) it still draws same outline correctly. I can not understand why this is happening?
    Also how can I know that out of the 49 elements which are of 'moveTo' or 'lineTo' or any other type?

    I have attached my code.

    Please help.
    Attached Files Attached Files

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw freetype fonts on QGraphicsScene?

    Quote Originally Posted by sarbh20ss View Post
    But when I change the no of iterations to even as less as 2 it still draws the outline correctly.

    Also when I change the function from 'lineTo( QPointF )' to 'moveTo( QPointF ) it still draws same outline correctly. I can not understand why this is happening?
    You are always drawing the full path so that's hardly surprising:

    Qt Code:
    1. p.drawPath(mypath);
    To copy to clipboard, switch view to plain text mode 


    Also how can I know that out of the 49 elements which are of 'moveTo' or 'lineTo' or any other type?
    elementAt() returns a type where one of the attributes is the type of the element.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to draw freetype fonts on QGraphicsScene?

    hey thanks a lot !
    I was able to sort out which points are connected using which function.
    many Thanks..


    Added after 48 minutes:


    hey I am able to connect each point and draw the path using either 'cubicTo' or 'lineTo' or 'moveTo'.

    Now if i click on any point on scene how to check if that point is on the curve or not?
    also how to make the path follow the dragging of mouse pointer on scene and modify it's shape?

    Thanks in advance.
    Last edited by sarbh20ss; 11th September 2013 at 10:40.

Similar Threads

  1. How to Draw spline on QGraphicsScene
    By seguprasad in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2011, 05:28
  2. To Draw Text at given position on QGraphicsScene
    By volcano in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2010, 08:16
  3. Replies: 5
    Last Post: 21st January 2010, 15:55
  4. QGraphicsScene and fonts/shapes sizes
    By olelukoie in forum Qt Programming
    Replies: 15
    Last Post: 28th January 2009, 21:46
  5. want to draw points in QGraphicsScene
    By ntp in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2008, 18:14

Tags for this Thread

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.