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 drawtext blurred text
Forum Updated to NodeBB v4.3 + New Features

QPainter drawtext blurred text

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 7.8k Views 3 Watching
  • 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.
  • PabloArgP Offline
    PabloArgP Offline
    PabloArg
    wrote on last edited by
    #6

    Also I tried font.setStyleStrategy(QFont::PreferAntialias); unsuccessfully as well

    E 1 Reply Last reply
    0
    • PabloArgP PabloArg

      Also I tried font.setStyleStrategy(QFont::PreferAntialias); unsuccessfully as well

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #7

      @PabloArg
      I'm not sure if I have understood the code snippets correctly, but in any case antialiasing makes the text (or any edge) blurred, that's its purpose, to smoothen out sharp edges. It should be turned off if you want sharp pixels.

      1 Reply Last reply
      0
      • PabloArgP Offline
        PabloArgP Offline
        PabloArg
        wrote on last edited by
        #8

        Hi Eeli K, Well when you run the code Sharp edges are because setbold is false.
        When setbold is true it looks blurry.

        I will post my code again, when you get the picture open with GIMP or Other and make zoom up to 300 and will be very clear the problem.

        QString Texto = QDateTime::currentDateTime().toString();
        QFont font;
        font.setBold(true);
        font.setFamily("Helvetica");
        font.setStyleStrategy(QFont::PreferAntialias);
        font.setPixelSize(12);
        font.setStyleHint(QFont::SansSerif,QFont::PreferOutline);
        QFontMetrics fm(font);
        qreal ancho = fm.width(Texto);
        qreal alto = fm.height();
        
        QImage image(ancho,alto, QImage::Format_ARGB32);
        //    image.fill(0);
        image.fill(Qt::white);
        QPainter painter;
        painter.begin(&image);
        painter.setRenderHints(QPainter::TextAntialiasing,true);
        painter.setRenderHints(QPainter::Antialiasing,true);
        painter.save();
        //    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
        painter.setFont(font);
        painter.setPen(QPen(Qt::black, 0, Qt::SolidLine, Qt::RoundCap));
        QRectF r1(0, 0, ancho-1,alto-1);
        //    painter.drawRect(r1);
        painter.drawText(r1,Qt::AlignCenter,Texto);
        //    painter.drawText(10,10,100,100,Qt::AlignCenter,Texto);
        image.save("Texto.png");
        painter.restore();
        painter.end();
        
        jsulmJ 1 Reply Last reply
        0
        • PabloArgP PabloArg

          Hi Eeli K, Well when you run the code Sharp edges are because setbold is false.
          When setbold is true it looks blurry.

          I will post my code again, when you get the picture open with GIMP or Other and make zoom up to 300 and will be very clear the problem.

          QString Texto = QDateTime::currentDateTime().toString();
          QFont font;
          font.setBold(true);
          font.setFamily("Helvetica");
          font.setStyleStrategy(QFont::PreferAntialias);
          font.setPixelSize(12);
          font.setStyleHint(QFont::SansSerif,QFont::PreferOutline);
          QFontMetrics fm(font);
          qreal ancho = fm.width(Texto);
          qreal alto = fm.height();
          
          QImage image(ancho,alto, QImage::Format_ARGB32);
          //    image.fill(0);
          image.fill(Qt::white);
          QPainter painter;
          painter.begin(&image);
          painter.setRenderHints(QPainter::TextAntialiasing,true);
          painter.setRenderHints(QPainter::Antialiasing,true);
          painter.save();
          //    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
          painter.setFont(font);
          painter.setPen(QPen(Qt::black, 0, Qt::SolidLine, Qt::RoundCap));
          QRectF r1(0, 0, ancho-1,alto-1);
          //    painter.drawRect(r1);
          painter.drawText(r1,Qt::AlignCenter,Texto);
          //    painter.drawText(10,10,100,100,Qt::AlignCenter,Texto);
          image.save("Texto.png");
          painter.restore();
          painter.end();
          
          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @PabloArg Nothing is blurred on my Windows 7 machine with your code. What I see is what antialising is doing.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • PabloArgP Offline
            PabloArgP Offline
            PabloArg
            wrote on last edited by
            #10

            The problem is, that I generate a PDF and when I print it looks bad.
            Anyway I got this:
            0_1486039313695_upload-6d303660-7657-49d8-90d4-9699ec719fce

            jsulmJ 1 Reply Last reply
            0
            • PabloArgP PabloArg

              The problem is, that I generate a PDF and when I print it looks bad.
              Anyway I got this:
              0_1486039313695_upload-6d303660-7657-49d8-90d4-9699ec719fce

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @PabloArg Image sharing is usually not working here. Please upload the picture to image sharing service and post the link here.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • PabloArgP Offline
                PabloArgP Offline
                PabloArg
                wrote on last edited by
                #12

                Hi jsulm, I cannot find the link that you suggest.

                mrjjM 1 Reply Last reply
                0
                • PabloArgP PabloArg

                  Hi jsulm, I cannot find the link that you suggest.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #13

                  @PabloArg
                  Hi i use
                  www.postimage.org
                  and then just paste link here.
                  ( the Direct Link)

                  1 Reply Last reply
                  0
                  • PabloArgP Offline
                    PabloArgP Offline
                    PabloArg
                    wrote on last edited by
                    #14

                    https://postimg.org/image/yteihu8gz/9766cc13/

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      Ahh
                      Its when inside the PDF ?
                      Since you print it to image and then print image to pdf, its not
                      a vector font anymore.
                      its simply a image of a font/text and will get ugly when zoomed.

                      1 Reply Last reply
                      1
                      • PabloArgP Offline
                        PabloArgP Offline
                        PabloArg
                        wrote on last edited by
                        #16

                        Mrjj you say that the Helvetic font is not vectorial, so if you know could you tell me some font that is vectorial on windows I will appreciate

                        mrjjM 1 Reply Last reply
                        0
                        • PabloArgP PabloArg

                          Mrjj you say that the Helvetic font is not vectorial, so if you know could you tell me some font that is vectorial on windows I will appreciate

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #17

                          @PabloArg
                          No, not like that.
                          It is vector but you make it part of an image and i think
                          what you mean is that is not scaling nicely as other text in pdf normally do ?
                          The reason for that is its no longer text, just pixels on image.

                          Try drawing the text directly on the Printer painter
                          QPrinter printer;
                          QPainter painter(&printer);
                          printer.setOutputFileName("c:\temp\file.pdf");
                          printer.setOutputFormat(QPrinter::PdfFormat);
                          painter.drawText(xxx)

                          1 Reply Last reply
                          1
                          • PabloArgP Offline
                            PabloArgP Offline
                            PabloArg
                            wrote on last edited by
                            #18

                            mrjj. Make sense that you say. I saw that if I draw directly using qpdfwriter the text looks good.
                            So, I will follow your advise, write directly on QPrinter or qpdfwriter.

                            Many thanks everybody

                            1 Reply Last reply
                            1

                            • Login

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