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 doesn't work
Qt 6.11 is out! See what's new in the release blog

QPainter::drawText doesn't work

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 6.6k Views 1 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    Hello!

    I am trying to write a pdf file with QPainter. I have a problem because the drawText function doesn't work. I use drawText and drawImage and the second one works correctly. This is my function code:

    void report_tab::on_pdfButton_clicked()
    {
        QIcon icon(":/MainIcons/Resources/pdf_pressed_2.png");
        icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf_pressed_2.png"));
        ui->pdfButton->setIcon(icon);
    
        printer.setPageSize(QPrinter::A4);
        printer.setOrientation(QPrinter::Portrait);
        printer.setOutputFormat(QPrinter::PdfFormat);
    
        QString fileName = QFileDialog::getSaveFileName(this, "Save pdf...", QCoreApplication::applicationDirPath(), "PDF (*.pdf)" );
        if (!fileName.isNull())
        {
            printer.setOutputFileName(fileName);
            QImage imagen = imageShot.toImage();
            QImage graph = profileShot.toImage();
            QImage table = tableShot.toImage();
            QImage fileInf = fileInfo.toImage();
            QImage generalDat = genData.toImage();
    
            //imagen = imagen.scaled(imagen.width() - 300, imagen.height() - 100);
            QPainter painter(&printer);
            QRect rect = painter.viewport();
            rect.setSize(QSize(imagen.width(), imagen.height()));
            QSize size = imagen.size();
            size.scale(rect.size(), Qt::KeepAspectRatio);
            double xscale = printer.pageRect().width() / double(imagen.width()*1.19);
            double yscale = printer.pageRect().height() / double(imagen.height());
    
            double scale = qMin(xscale, yscale);
            painter.scale(scale, scale);
            painter.translate(140, 220);
    
            QFont font("Courier New");
            font.setStretch(QFont::ExtraExpanded);
            painter.setFont(font);
            QTextOption options;
            options.setWrapMode(QTextOption::WordWrap);
    
            painter.setPen(Qt::SolidLine);
            painter.drawText(0, 0, ui->projectLabel->text());
            painter.drawImage(0, ui->projectLabel->height() + 30, imagen);
            painter.drawImage(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30, fileInf);
            painter.drawImage(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + fileInf.rect().height() + 30, generalDat);
            painter.drawImage(imagen.rect().width() - graph.rect().width(), ui->projectLabel->height() + 30 + imagen.rect().height() + 30, graph);
            painter.drawImage(imagen.rect().width() - table.rect().width(), ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30, table);
            painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30, ui->projectLabel->text());
            painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30 + ui->projectLabel->height() + 30, ui->codeLabel->text());
            painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 +
                             table.rect().height() + 30 + ui->projectLabel->height() + 30 + ui->codeLabel->height() + 30, ui->dateEdit->text());
            painter.drawText(QRectF(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30 + ui->projectLabel->height() + 30 + ui->codeLabel->height() + 30 + ui->dateEdit->height() + 30, imagen.width(), 300), ui->descriptionLabel->toPlainText(), options);
    
            painter.end();
    
            QIcon icon(":/MainIcons/Resources/pdf.png");
            icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
            ui->pdfButton->setIcon(icon);
            pdfButtonClicked = false;
            QMessageBox::information(NULL, "Export PDF", "PDF created.", QMessageBox::Ok);
        } else {
            QIcon icon(":/MainIcons/Resources/pdf.png");
            icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
            ui->pdfButton->setIcon(icon);
            pdfButtonClicked = false;
        }
    
    }
    

    How can I fix this problem? Thank you very much!

    m.sueM raven-worxR 2 Replies Last reply
    0
    • ivanicyI ivanicy

      Hello!

      I am trying to write a pdf file with QPainter. I have a problem because the drawText function doesn't work. I use drawText and drawImage and the second one works correctly. This is my function code:

      void report_tab::on_pdfButton_clicked()
      {
          QIcon icon(":/MainIcons/Resources/pdf_pressed_2.png");
          icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf_pressed_2.png"));
          ui->pdfButton->setIcon(icon);
      
          printer.setPageSize(QPrinter::A4);
          printer.setOrientation(QPrinter::Portrait);
          printer.setOutputFormat(QPrinter::PdfFormat);
      
          QString fileName = QFileDialog::getSaveFileName(this, "Save pdf...", QCoreApplication::applicationDirPath(), "PDF (*.pdf)" );
          if (!fileName.isNull())
          {
              printer.setOutputFileName(fileName);
              QImage imagen = imageShot.toImage();
              QImage graph = profileShot.toImage();
              QImage table = tableShot.toImage();
              QImage fileInf = fileInfo.toImage();
              QImage generalDat = genData.toImage();
      
              //imagen = imagen.scaled(imagen.width() - 300, imagen.height() - 100);
              QPainter painter(&printer);
              QRect rect = painter.viewport();
              rect.setSize(QSize(imagen.width(), imagen.height()));
              QSize size = imagen.size();
              size.scale(rect.size(), Qt::KeepAspectRatio);
              double xscale = printer.pageRect().width() / double(imagen.width()*1.19);
              double yscale = printer.pageRect().height() / double(imagen.height());
      
              double scale = qMin(xscale, yscale);
              painter.scale(scale, scale);
              painter.translate(140, 220);
      
              QFont font("Courier New");
              font.setStretch(QFont::ExtraExpanded);
              painter.setFont(font);
              QTextOption options;
              options.setWrapMode(QTextOption::WordWrap);
      
              painter.setPen(Qt::SolidLine);
              painter.drawText(0, 0, ui->projectLabel->text());
              painter.drawImage(0, ui->projectLabel->height() + 30, imagen);
              painter.drawImage(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30, fileInf);
              painter.drawImage(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + fileInf.rect().height() + 30, generalDat);
              painter.drawImage(imagen.rect().width() - graph.rect().width(), ui->projectLabel->height() + 30 + imagen.rect().height() + 30, graph);
              painter.drawImage(imagen.rect().width() - table.rect().width(), ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30, table);
              painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30, ui->projectLabel->text());
              painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30 + ui->projectLabel->height() + 30, ui->codeLabel->text());
              painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 +
                               table.rect().height() + 30 + ui->projectLabel->height() + 30 + ui->codeLabel->height() + 30, ui->dateEdit->text());
              painter.drawText(QRectF(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30 + ui->projectLabel->height() + 30 + ui->codeLabel->height() + 30 + ui->dateEdit->height() + 30, imagen.width(), 300), ui->descriptionLabel->toPlainText(), options);
      
              painter.end();
      
              QIcon icon(":/MainIcons/Resources/pdf.png");
              icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
              ui->pdfButton->setIcon(icon);
              pdfButtonClicked = false;
              QMessageBox::information(NULL, "Export PDF", "PDF created.", QMessageBox::Ok);
          } else {
              QIcon icon(":/MainIcons/Resources/pdf.png");
              icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
              ui->pdfButton->setIcon(icon);
              pdfButtonClicked = false;
          }
      
      }
      

      How can I fix this problem? Thank you very much!

      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi @ivanicy

      What is it that does not work? No text at all, wrong position, wrong font, wrong whatever?

      I have no problem with PDF text output. Well I do not use these options, though:

      font.setStretch(QFont::ExtraExpanded);
      options.setWrapMode(QTextOption::WordWrap);
      

      -Michael.

      ivanicyI 1 Reply Last reply
      0
      • m.sueM m.sue

        Hi @ivanicy

        What is it that does not work? No text at all, wrong position, wrong font, wrong whatever?

        I have no problem with PDF text output. Well I do not use these options, though:

        font.setStretch(QFont::ExtraExpanded);
        options.setWrapMode(QTextOption::WordWrap);
        

        -Michael.

        ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        @m.sue The text doesn't appear in the pdf. These options only format the text. I tried commenting these lines without changes.

        m.sueM 1 Reply Last reply
        0
        • ivanicyI ivanicy

          Hello!

          I am trying to write a pdf file with QPainter. I have a problem because the drawText function doesn't work. I use drawText and drawImage and the second one works correctly. This is my function code:

          void report_tab::on_pdfButton_clicked()
          {
              QIcon icon(":/MainIcons/Resources/pdf_pressed_2.png");
              icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf_pressed_2.png"));
              ui->pdfButton->setIcon(icon);
          
              printer.setPageSize(QPrinter::A4);
              printer.setOrientation(QPrinter::Portrait);
              printer.setOutputFormat(QPrinter::PdfFormat);
          
              QString fileName = QFileDialog::getSaveFileName(this, "Save pdf...", QCoreApplication::applicationDirPath(), "PDF (*.pdf)" );
              if (!fileName.isNull())
              {
                  printer.setOutputFileName(fileName);
                  QImage imagen = imageShot.toImage();
                  QImage graph = profileShot.toImage();
                  QImage table = tableShot.toImage();
                  QImage fileInf = fileInfo.toImage();
                  QImage generalDat = genData.toImage();
          
                  //imagen = imagen.scaled(imagen.width() - 300, imagen.height() - 100);
                  QPainter painter(&printer);
                  QRect rect = painter.viewport();
                  rect.setSize(QSize(imagen.width(), imagen.height()));
                  QSize size = imagen.size();
                  size.scale(rect.size(), Qt::KeepAspectRatio);
                  double xscale = printer.pageRect().width() / double(imagen.width()*1.19);
                  double yscale = printer.pageRect().height() / double(imagen.height());
          
                  double scale = qMin(xscale, yscale);
                  painter.scale(scale, scale);
                  painter.translate(140, 220);
          
                  QFont font("Courier New");
                  font.setStretch(QFont::ExtraExpanded);
                  painter.setFont(font);
                  QTextOption options;
                  options.setWrapMode(QTextOption::WordWrap);
          
                  painter.setPen(Qt::SolidLine);
                  painter.drawText(0, 0, ui->projectLabel->text());
                  painter.drawImage(0, ui->projectLabel->height() + 30, imagen);
                  painter.drawImage(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30, fileInf);
                  painter.drawImage(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + fileInf.rect().height() + 30, generalDat);
                  painter.drawImage(imagen.rect().width() - graph.rect().width(), ui->projectLabel->height() + 30 + imagen.rect().height() + 30, graph);
                  painter.drawImage(imagen.rect().width() - table.rect().width(), ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30, table);
                  painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30, ui->projectLabel->text());
                  painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30 + ui->projectLabel->height() + 30, ui->codeLabel->text());
                  painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 +
                                   table.rect().height() + 30 + ui->projectLabel->height() + 30 + ui->codeLabel->height() + 30, ui->dateEdit->text());
                  painter.drawText(QRectF(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30 + ui->projectLabel->height() + 30 + ui->codeLabel->height() + 30 + ui->dateEdit->height() + 30, imagen.width(), 300), ui->descriptionLabel->toPlainText(), options);
          
                  painter.end();
          
                  QIcon icon(":/MainIcons/Resources/pdf.png");
                  icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
                  ui->pdfButton->setIcon(icon);
                  pdfButtonClicked = false;
                  QMessageBox::information(NULL, "Export PDF", "PDF created.", QMessageBox::Ok);
              } else {
                  QIcon icon(":/MainIcons/Resources/pdf.png");
                  icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
                  ui->pdfButton->setIcon(icon);
                  pdfButtonClicked = false;
              }
          
          }
          

          How can I fix this problem? Thank you very much!

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @ivanicy said in QPainter::drawText doesn't work:
          instead of

          painter.setPen(Qt::SolidLine);
          

          try

          peinter.setPen( QPen(Qt::black,1) );
          

          At a short glance this might be the problem, not necessarily though.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          ivanicyI 1 Reply Last reply
          0
          • ivanicyI ivanicy

            @m.sue The text doesn't appear in the pdf. These options only format the text. I tried commenting these lines without changes.

            m.sueM Offline
            m.sueM Offline
            m.sue
            wrote on last edited by
            #5

            Hi @ivanicy

            I use Tahoma font, I explicitly set a pen with color via QBrush. I do not explicitly call painter.end(). I do not use QRect textDraw, just positional text draw with QPoint. Nothing special, as it seems.

            Which OS, which Qt version anyway?

            -Michael.

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @ivanicy said in QPainter::drawText doesn't work:
              instead of

              painter.setPen(Qt::SolidLine);
              

              try

              peinter.setPen( QPen(Qt::black,1) );
              

              At a short glance this might be the problem, not necessarily though.

              ivanicyI Offline
              ivanicyI Offline
              ivanicy
              wrote on last edited by
              #6

              @raven-worx This give me the same result. No text

              1 Reply Last reply
              0
              • ivanicyI Offline
                ivanicyI Offline
                ivanicy
                wrote on last edited by
                #7

                I use the drawText function in a QPrintPreviewDialog and the text appears correctly. I don't know why when I want to write a pdf, the text doesn't appear. Here is my QPrintPreviewDialog code:

                void report_tab::print(QPrinter *p) {
                    QImage imagen = imageShot.toImage();
                    QImage graph = profileShot.toImage();
                    QImage table = tableShot.toImage();
                    QImage fileInf = fileInfo.toImage();
                    QImage generalDat = genData.toImage();
                
                    //imagen = imagen.scaled(imagen.width() - 200, imagen.height() - 100);
                    QPainter painter(p);
                    QRect rect = painter.viewport();
                    rect.setSize(QSize(imagen.width(), imagen.height()));
                    QSize size = imagen.size();
                    size.scale(rect.size(), Qt::KeepAspectRatio);
                    double xscale = printer.pageRect().width() / double(imagen.width()*1.19);
                    double yscale = printer.pageRect().height() / double(imagen.height());
                
                    double scale = qMin(xscale, yscale);
                    painter.scale(scale, scale);
                    painter.translate(160, 220);
                
                    QFont font("Courier New");
                    font.setStretch(QFont::ExtraExpanded);
                    painter.setFont(font);
                    QTextOption options;
                    options.setWrapMode(QTextOption::WordWrap);
                
                    painter.drawText(0, 0, ui->projectLabel->text());
                    painter.drawImage(0, ui->projectLabel->height() + 30, imagen);
                    painter.drawImage(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30, fileInf);
                    painter.drawImage(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + fileInf.rect().height() + 30, generalDat);
                    painter.drawImage(imagen.rect().width() - graph.rect().width(), ui->projectLabel->height() + 30 + imagen.rect().height() + 30, graph);
                    painter.drawImage(imagen.rect().width() - table.rect().width(), ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30, table);
                    painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30, ui->projectLabel->text());
                    painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30 + ui->projectLabel->height() + 30, ui->codeLabel->text());
                    painter.drawText(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 +
                                     table.rect().height() + 30 + ui->projectLabel->height() + 30 + ui->codeLabel->height() + 30, ui->dateEdit->text());
                    painter.drawText(QRectF(0, ui->projectLabel->height() + 30 + imagen.rect().height() + 30 + graph.rect().height() + 30 + table.rect().height() + 30 + ui->projectLabel->height() + 30 + ui->codeLabel->height() + 30 + ui->dateEdit->height() + 30, imagen.width(), 300), ui->descriptionLabel->toPlainText(), options);
                
                }
                

                Thank you

                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