Question

I am drawing an outline of a glyph for given character using:

QString myfname="FreeMono.ttf";
QRawFont *myfont=new QRawFont(myfname,324,QFont::PreferDefaultHinting);
QChar mychars[]={'Q','B'};
int numofchars=3;
quint32 myglyphindexes[3];
int numofglyphs;
myfont->glyphIndexesForChars(mychars,numofchars,myglyphindexes,&numofglyphs);
QPainterPath mypath=myfont->pathForGlyph(myglyphindexes[0]);

This path I am drawing on pixmap using

painter.drawPath(mypath)

I want to know how this path is drawn. I mean what type of curves or lines this outline contains. For this I tried this:

QPointF mypoints[49];
for(int i=0; i<mypath.elementAt(i); i++)
    {
        mypoints[i]=mypath.elementAt(i);
    }

This gives me array of points. But how these points are connected with each other like using a line or curve. How can I know that? Also is this a correct approach? What I need to improve?

Was it helpful?

Solution

QPainterPath::elementAt() returns an object of type QPainterPath::Element, not a QPoint (it has a QPointF operator defined).

You can use code like this:

const QPainterPath::Element &elem = path.elementAt(ii);

// You can use the type enum.
qDebug() << elem.type;

// Or you can use the functions.
if (elem.isCurveTo()) {
    qDebug() << "curve";
} else if (elem.isLineTo()) {
    qDebug() << "line";
} else if (elem.isMoveTo()) {
    qDebug() << "move";
}

OTHER TIPS

sashoalm is correct, but I would just like to add that you can use path.elementCount() to know how many elements are in your QPainterPath.

Hence, it would look like this:

for(int i=0; i<mypath.elementCount(); i++)
{
    const QPainterPath::Element & elem = mypath.elementAt(i);
    qDebug() << elem.type;

    // Or you can use the functions.
    if (elem.isCurveTo()) {
        qDebug() << "curve";
    } else if (elem.isLineTo()) {
        qDebug() << "line";
    } else if (elem.isMoveTo()) {
        qDebug() << "move";
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top