Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter draw text renders the text into some weird characters
QtWS25 Last Chance

QPainter draw text renders the text into some weird characters

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hhoushiar
    wrote on 16 Mar 2017, 21:39 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ambershark
      wrote on 17 Mar 2017, 02:45 last edited by
      #2

      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.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      H 1 Reply Last reply 17 Mar 2017, 14:27
      1
      • A ambershark
        17 Mar 2017, 02:45

        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.

        H Offline
        H Offline
        hhoushiar
        wrote on 17 Mar 2017, 14:27 last edited by
        #3

        @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

        A 1 Reply Last reply 21 Mar 2017, 04:13
        0
        • H hhoushiar
          17 Mar 2017, 14:27

          @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

          A Offline
          A Offline
          ambershark
          wrote on 21 Mar 2017, 04:13 last edited by
          #4

          @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.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          H 1 Reply Last reply 4 Apr 2017, 12:41
          0
          • A ambershark
            21 Mar 2017, 04:13

            @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.

            H Offline
            H Offline
            hhoushiar
            wrote on 4 Apr 2017, 12:41 last edited by
            #5

            @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

            A 1 Reply Last reply 6 Apr 2017, 05:24
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 4 Apr 2017, 21:02 last edited by
              #6

              Hi,

              Are you using a special font ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              H 1 Reply Last reply 5 Apr 2017, 08:00
              0
              • S SGaist
                4 Apr 2017, 21:02

                Hi,

                Are you using a special font ?

                H Offline
                H Offline
                hhoushiar
                wrote on 5 Apr 2017, 08:00 last edited by
                #7

                @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.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 5 Apr 2017, 21:02 last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • H hhoushiar
                    4 Apr 2017, 12:41

                    @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

                    A Offline
                    A Offline
                    ambershark
                    wrote on 6 Apr 2017, 05:24 last edited by
                    #9

                    @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. ;)

                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved