QPainter::drawText doesn't work



  • 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!



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



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


  • Moderators

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



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



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



  • 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


Log in to reply
 

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