Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sizing and scaling widgets on a single PDF page



  • Here is my problem. I want my program to generate a PDF report which contains 2 elements on the same page :

    1. A QGraphicsScene, already generated and displayed in my program’s main window, and
    2. A QTableWidget

    Here is the code so far:

    void D15::on_pB_Sauvegarde_clicked()
    {
        QString fileName = QFileDialog::getSaveFileName(this, "Export PDF", QString(), "*.pdf");
        QPrinter printer(QPrinter::HighResolution);
        printer.setPageSize(QPrinter::Letter);
        printer.setOrientation(QPrinter::Portrait);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName(fileName);
    
        QPainter p(&printer);
    
        p.setWindow(0, 0, 85, 110);
        QRectF rectangle(1, 30, 80, 80);
        scene->render(&p, rectangle);
    
        QTableWidget *pdftable = new QTableWidget(1, 15, this);
    
        for (int i = 0; i < 15; i++) {
                QTableWidgetItem *pdfitem = new QTableWidgetItem(cases[i]->text()); 
                pdftable->setItem(0, i, pdfitem);
        }
    
        p.setWindow(0, 0, 1600, 2200);
    
        pdftable->setFixedSize(800, 50);
       // pdftable->adjustSize();
        pdftable->render(&p);
    
        p.end();
    }
    
    

    scene is a QGraphicsScene displayed on the main window.
    cases[] is an array of QTableWidgetItem used in a QTableWidget in the main window.
    The line p.setWindow(0, 0, 85, 110); is used to define coordinates in tenth of an inch on the page.

    The scene is rendered perfectly on the page (scaling, size and position). The problem is with the QTableWidget pdftable. It seems that I have to rely on trial and error with p.setWindow() and pdftable->setFixedSize() to have the table shown entirely, at the size and position I want. But I haven't succeded yet, as the table is croped and scroll bars appear around it. pdftable->adjustSize() doesn't seem to do anything here. Is there a way to ensure that the table is showned entirely and that I can place and size it easily (for example by using the same units (tenth of inch) I use for the QGraphicsScene?

    Thank you in advance and have a nice day,
    Marc


Log in to reply