QPrinter margins



  • I need to print some text and I´m trying to use QtextEdit or QtextDocument and it´s impossible to set margin to 0.

    I need to put the text without margins at the paper.

    What am I doing wrong?

    here is my code:

    ```
    

    QString text =
    "inicio sit amet, consectetur adipisicing elit, sed do\n"
    "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\n"
    "enim ad minim veniam, quis nostrud exercitation ullamco laboris\n"
    "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor\n"
    "in reprehenderit in voluptate velit esse cillum dolore eu fugiat\n"
    "nulla pariatur. Excepteur sint occaecat cupidatat non proident,\n"
    "sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
    "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n"
    "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\n"
    "enim ad minim veniam, quis nostrud exercitation ullamco laboris\n"
    "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor\n"
    "in reprehenderit in voluptate velit esse cillum dolore eu fugiat\n"
    "nulla pariatur. Excepteur sint occaecat cupidatat non proident,\n"
    "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\n"
    "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor\n"
    "in reprehenderit in voluptate velit esse cillum dolore eu fugiat\n";

    QPrinter printer;
    
    QPrintDialog *dialog = new QPrintDialog(&printer);
    dialog->setWindowTitle("Imprimir Nota");
    
    qreal left=0, right=0, top=0, bottom=0;
    printer.setPageMargins(left, right, bottom, top, QPrinter::Millimeter );
    printer.getPageMargins(&left, &top, &right, &bottom, QPrinter::Millimeter);
    qDebug() << left << right << top << bottom;
    
    if (dialog->exec() != QDialog::Accepted)
        return;
    

    // test ok but there is a margin!!!!
    QTextEdit teste3;
    teste3.setPlainText(text);
    teste3.print(&printer);

    QTextDocument *document = new QTextDocument();
    printer.setFullPage(true);
    document->setPlainText(text);
    document->print(&printer);
    

    //* no margins here!!!!****
    QPainter painter;
    painter.begin(&printer);

    painter.drawText(0, 0, 1000, 300, Qt::AlignLeft|Qt::AlignTop, text);
    
    painter.end();
    
    tks

  • Qt Champions 2016

    Hi
    Most printers have a non printable margin where its not possible to print.
    Often around 6.35 mm



  • Hi, thank you for your response...

    But when I use Painter to print it´s posssible with margin=0.

    I made 3 tests:
    1)using painter.drawtext(0,0,0,0, text) and the text was printed at the top of the paper with margin 0

    2)using :
    QTextEdit teste3;
    teste3.setPlainText(text);
    teste3.print(&printer);
    This print the same text, but with a paper margin.

    3)using
    QTextDocument *document = new QTextDocument();
    printer.setFullPage(true);
    document->setPlainText(text);
    document->print(&printer);
    The results are the same of test (2)

    I tried to set printer.setPageMargins(0,0,0,0,QPrinter::Millimeter)...nothing

    when I use printer.setPageMargins(10,10,10,10,QPrinter::Millimeter)...the margin increase.

    My app will print to a ribbon printer (I don´t know if it´s a correct name ...I´m from Brazil )...this will used for print restaurant order food in a small ribbon printer ).


  • Qt Champions 2016

    @Guapo
    Oh
    Its a ribbon printer. ( Google agree its the correct name :)

    If you can print at 0,0 and text is shown then it must be able to print
    to the very border. Maybe it reports some margin from the OS/driver

    Can you try something like
    printer.setFullPage(true); // must be before setmargin
    printer.setPageMargins(QMarginsF(0,0,0,0));
    qDebug() << "new margins:" << printer->pageLayout().margins();

    I think it might relate to
    https://bugreports.qt.io/browse/QTBUG-32987



  • @mrjj said in QPrinter margins:

    If you can print at 0,0 and text is shown then it must be able to print
    to the very border. Maybe it reports some margin from the OS/drive

    Hi @mrjj,
    let me explain better....I will use a ribbon printer, but now I´m using EPSON L355 bulk ink.

    this is the result after new setup: >> new margins: QMarginsF(0, 0, 0, 0)
    it looks like the change the printer config but the results are the same.

    I include 2 files with the result from printer and painter:

    printer: ( up and left margins )
    0_1499017127260_PRINTER-1.jpg

    using painter: this print doesn´t have any margins (up and left)...I used this paper to make several tests....:)
    0_1499017155024_PRINTER-2.jpg


  • Qt Champions 2016

    @Guapo

    Must mean that QTextDocument looks up its own margins.
    I could not find any further info on this.

    You could look in the source code to see what is going on.

    If anything else fails, you can define a new custom page for the printer that has no
    margins.



  • @mrjj, I think you all right. You help me to understand I´m going to the right way.

    Thanks for your help.


  • Qt Champions 2016

    @Guapo

    I think after you set printers margin to zero you need something like
    document.setPageSize(QSizeF(printer.pageRect().size()));
    So doc get its page size updated.

    as in

    printer.setFullPage(true); // must be before setmargin
    printer.setPageMargins(QMarginsF(0,0,0,0));
    document.setPageSize(QSizeF(printer.pageRect().size()));
    ..print..



  • @mrjj, almost there....this hint cuts the first line and first column. But print at 0,0.

    I´m sure this is the way.



  • @Guapo said in QPrinter margins:

    @mrjj, almost there....this hint cuts the first line and first column. But print at 0,0.

    I´m sure this is the way.

    I don´t know what´s happen before with the firs line....this is not true...only cuts the first column. I´m trying to understand.



  • In another thread I saw QtextDocument unit is point (pt) and

    const qreal PT_MM = 25.4/72.0;
    document->setPageSize(QSizeF(printer.pageRect().size()));
    document->setDocumentMargin(5.0/PT_MM); // sets margin to 5mm

    the complete function is here:


    QString text ="\n"
            "1PRINT BY PRINTER            sectetur adipisicing elit, sed do\n"
            "2eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\n"
            "3eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\n"
            "4nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor\n"
            "5in reprehenderit in voluptate velit esse cillum dolore eu fugiat\n"
            "6nulla pariatur. Excepteur sint occaecat cupidatat non proident,\n"
            "7sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
            "8Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n"
            "9eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\n"
            "20sunt in culpa qui officia deserunt mollit anim      testao.\n";
    
    QPrinter printer;
     printer.setFullPage(true); // must be before setmargin
     qDebug() << "new margins:" << printer.pageLayout().margins();
     printer.setPageMargins(QMarginsF(0,0,0,0));
     qDebug() << "new margins:" << printer.pageLayout().margins() << printer.pageRect().size();
    
    QPrintDialog *dialog = new QPrintDialog(&printer);
    dialog->setWindowTitle("Imprimir Nota");
    
    if (dialog->exec() == QDialog::Rejected) return;
    
    const qreal PT_MM = 25.4/72.0;
    
    QTextDocument *document = new QTextDocument();
    document->setPageSize(QSizeF(printer.pageRect().size()));
    document->setDocumentMargin(5.0/PT_MM);
    document->setPlainText(text);
    document->print(&printer);
    

    it´s working!!!!

    @mrjj, thank you very much for your help!!!


  • Qt Champions 2016

    Ahh super
    it was in points, missed that.


Log in to reply
 

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