
Originally Posted by
ChasW
Just for clarification, given the code above, doesn't the first character in the string get skipped? In other words, for length "0" which is what n would be for the first iteration, on my system, width() is returning 0. As well the final value is not being stored. Is that what should be happening?
example: using the above code on my system, given that text is a string "abc" for a fixed font whose width is 7 pixels, my vector is getting these values, 0, 7, 14.
In order to get the 0th element of the charExtents vector to be the pixel width for that character, I am using fm.width(text, n+1). In other words, to get 7, 14, 21.
Is this correct, or could I be doing something wrong?
Thank you in advance for any clarification.
Yes, you're right. It should be:
std::vector<int> charExtents;
for (int n = 0; n < text.length(); ++n)
charExtents.push_back(fm.width(text, n + 1));
std::vector<int> charExtents;
QFontMetrics fm(font());
for (int n = 0; n < text.length(); ++n)
charExtents.push_back(fm.width(text, n + 1));
To copy to clipboard, switch view to plain text mode
Bookmarks