QPainter draw text renders the text into some weird characters



  • Hi Everyone,
    So I am rendering some 3D element in openGL with the help of QQuickFramebufferObject.
    When user is done with the drawing they add a name to the object and I want to render the name next to the object. Here is a code that I am using to achieve this:

    QOpenGLPaintDevice device(viewportSize_);
    QPainter painter;
    painter.begin(&device);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing);
    QFont font;
    font.setPointSize(10 * window_->effectiveDevicePixelRatio());
    font.setBold(true);
    QFontMetrics fontMatrics(font);
    int textWidth = fontMatrics.width(veName);
    int textHeight = fontMatrics.height() ;
    
    QRect nameRect(winX - textWidth, viewportSize_.height() - winY - textHeight, textWidth, textHeight);
            
    painter.save();
    painter.setPen(QPen(veColor, 1));
    painter.setBrush(QColor(0, 0, 0, 200));
    painter.drawRect(nameRect);
    painter.restore();
    
    painter.setFont(font);
    painter.setPen(QPen(Qt::white));
    painter.drawText(nameRect, Qt::AlignCenter, veName);
    painter.end();
    

    This works fine on MacOS and Linux but on windows the QString "veName" changes to some weird characters. The bounding rectangle and the font size and positions are correct but the characters turn to some unreadable characters.
    I think something is wrong with drawText function under windows but I can't find anything like this online and I am out of ideas on how to solve this.

    Any help would be appreciated.
    And by the way I am using Qt 5.8.0 and the problem happens on windows 7 and 10 both 64bit.


  • Moderators

    This sounds like you are having font issues. I've never seen that on windows, but I've seen it on Linux.

    Make sure you have your Qt fonts dir set up correctly, or use the QT_QPA_FONTDIR to point to it. I may be off on that var name, it's been a while. That should allow you to google it though to figure out.

    Also I know I've posted about it on these forums before. It was in regards to linux but the answer should get you to the same solution. Just search the forums.



  • @ambershark Thanks for your reply. I don't think the problem is with finding the font since I can change the font and QFontInfo returns the correct value. Also everything else can be changed and has a positive effect such as getting the QFontMetrics and etc.
    Also sometimes the one or two correct characters get rendered. So as a sample when I try to render "HELLO", the out put is something like this:
    0_1489760802353_image.jpg


  • Moderators

    @hhoushiar You didn't show the sample output for HELLO. :) It may give me or someone else another idea on why you are having the rendering issue.



  • @ambershark Thanks for your reply and sorry for the late reply.
    I was out of office for a couple of weeks.
    Here is the sample output for HELLO:
    alt text


  • Lifetime Qt Champion

    Hi,

    Are you using a special font ?



  • @SGaist No I use the default font. Basically I do not set the font family. I also have tried setting different fonts but the result is almost the same. The characters look different but still not a correctly rendered. I also check the QFontInfo since the font specified by the QFont might not match the window system font. But if I don't change the default font it returns the expected information.
    My code works just fine on Linux and MacOS the problem is only on Windows. For the moment on Windows I am using QPainterPath to render the text which I believe is much slower and the rendered text does not look as nice.


  • Lifetime Qt Champion

    Can you share a minimal buildable sample application that reproduces this ?


  • Moderators

    @hhoushiar said in QPainter draw text renders the text into some weird characters:

    @ambershark Thanks for your reply and sorry for the late reply.
    I was out of office for a couple of weeks.
    Here is the sample output for HELLO:
    alt text

    That is very weird. As @SGaist says definitely share a simple example that can duplicate the problem if you can. That might help us figure out what's wrong. I've never seen that before. It looks like it hates vowels. ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.