Quantcast
Channel: Jobs
Viewing all articles
Browse latest Browse all 18427

Trying to collapse per-character QPainter::drawText() Qt commands into a single string drawText(). Monospacing format is not being preserved...

$
0
0
Hello everyone, I have poked around for quite some time on the net trying to find insight into this problem, but I’ve had no luck so far. Below is an example of what draws correctly: for (int i = 0; i < string.size(); ++i)         {             QString char_string(string.at(i));             QRect box(current_pos, char_size);             m_painter->drawText(box, Qt::AlignCenter, char_string);         } So here I am calling drawText() on each character of the string in order to print it in a monospaced format. Each character is printed neatly in the center of its own box, and the spacing between characters remains constant. char_size is a QSize obj which is a rect in which i print each character. I need to modify this and just pass ‘string’ into drawText(), making only one function call per string. The problem is that no matter how I configure my font, it will not actually print each character aligned how I need them the way that the above snippet does. Here is my current approximation of what I need to do:         QRect box(pos, char_size);         box.setWidth(char_size.width() * string.size());         m_painter->drawText(box, Qt::AlignVCenter /*| Qt::AlignHCenter*/ | Qt::AlignJustify, string); Here is a snapshot of roughly how I’m trying to set up the font. You can see I’m trying commenting and uncommenting certain lines together to try to replicate the spacing configuration I need: QFont QtCanvas::GetFont(const FontData &font_data)     {         QFont font;           font.setFamily("Droid Sans Mono");         //font.setFamily("Monospace");         //font.setStyleHint(QFont::Monospace);         //font.setStyleHint(QFont::TypeWriter);           font.setBold(font_data.m_style_bold);         font.setItalic(font_data.m_style_italic);         font.setStrikeOut(font_data.m_style_crossed_out);         font.setUnderline(font_data.m_style_underlined);           font.setFixedPitch(true);         font.setPixelSize(font_data.m_size.height);         font.setKerning(false);           QFontMetrics font_metrics(font);         //font.setStretch(QFont::Expanded);           //font.setLetterSpacing(QFont::AbsoluteSpacing, font_data.m_size.width - font_metrics.boundingRect(QChar('H')).width());         //font.setLetterSpacing(QFont::AbsoluteSpacing, font_data.m_size.width - font_metrics.boundingRect(QChar('0')).width());         //font.setLetterSpacing( QFont::AbsoluteSpacing, ( font_data.m_size.width );           //font.setWordSpacing( -font_data.m_size.width + (font_metrics.boundingRect(QChar('H')).width()) );         //font.setWordSpacing(font_data.m_size.width  - font_metrics.boundingRect(QChar('H')).width() * -1);           return font;     } The font I’m using is a monospaced font loaded from the resource file, though for some silly reason when I delve into the QFontMetrics and print out each character’s bounding rect, each character has a varied width (no longer monospaced, fixed-width). So, when I tell it to draw, it’s drawing the characters in sequence using their truncated widths and destroys the monospacing. This is bad… I need each character to line up and take up the same width of every other character. Spacing and character width should be uniform. Does anyone have any useful insight? All help is appreciated, I can provide more information if things aren’t clear!

Viewing all articles
Browse latest Browse all 18427

Trending Articles