Important: Please read the Qt Code of Conduct -

QPrintPreviewDialog show Text from Database

  • Im actually loading long Textblocks from a Database and want to show them in a QPrintPreviewDialog. But i dont find something how to append the textblocks to the page. How is it possible to append the textblocks from top to the bottom? everytime i need to set the QPainter Rect and the text overwrites in the first line. I didnt found anything in the Internet. there are only Examples how to show single lines, but not many textblocks. maybe i use QTextDocument too? Here is my actually code.

    void PrintDateRangeForm::on_pbAccept_clicked()
        GDatabase db;
        QVector<QByteArray> cryptedEntrys;
        BigAES crypt;
        QDate from, to;
        from = QDate::fromString(ui->leFrom->text());
        to = QDate::fromString(ui->leTo->text());
        db.selectEntryBetween(&cryptedEntrys, &date, from, to);
        for (int i = 0; i < cryptedEntrys.count(); i++)
            entrys.append(crypt.Decrypt(QByteArray::fromHex(cryptedEntrys[i]), aesKey));
        QPrinter printer;
        printer.setPageMargins (15,15,15,15,QPrinter::Millimeter);
        QPrintPreviewDialog preview(&printer, this);
        preview.setWindowFlags ( Qt::Window );
        connect(&preview, SIGNAL(paintRequested(QPrinter *)), SLOT(printPreview(QPrinter *)));
    void PrintDateRangeForm::printPreview(QPrinter *printer)
        QPainter *painter = new QPainter(printer);
        int w = printer->pageRect().width();
        int h = printer->pageRect().height();
        QRect page( 0, 0, w, h );
        for (int i = 0; i < entrys.count(); i++)
            painter->drawText(page, Qt::AlignTop | Qt::AlignLeft, date[i].toString());
            //painter->drawText(10, 11, QString(entrys[i]));

  • I have found a way to do that, but now i have another problem. From the Documentation i read that setPageSize() automatically sets page breaks for new pages, but that doesnt happen. someone have an idea?


  • nobody has an idea how to make page breaks?

Log in to reply