Congratulations to our 2022 Qt Champions!

QPrinter ignores QTextBlockFormat Qt::AlignmentFlag

  • I have a problem when printing my QTextDocument :- parts that are horizontally centered, which appear as expected on screen, always appear aligned to the left margin when printed.

    Here's what I'm doing :-
    QTextBlockFormat TitleBlockFormat;
    TitleBlockFormat.setIndent( Indent );
    TitleBlockFormat.setTopMargin( m_DisplayOptions.m_ParagraphTopMargin );
    TitleBlockFormat.setAlignment ( (Qt::Alignment) m_DisplayOptions.m_TitleAlignment );
    a_TextCursor.setBlockFormat( TitleBlockFormat );
    QPrinter printer;
    QPrintDialog *dlg = new QPrintDialog(&printer, this);
    dlg->setOption( QAbstractPrintDialog::PrintSelection, ui.MainContentTextEdit->textCursor().hasSelection() );
    if (dlg->exec() != QDialog::Accepted)
    ui.MainContentTextEdit->print( &printer );

    Hmm - maybe I should sort out that c-style cast, but you can trust me that the m_TitleAlignment is the right value, given that it works nicely on-screen.

    To me it looks like a QPrinter bug, but I can't see any references to this problem out in the webosphere, so I'm guessing it's me.

    Do other people do aligment in some other way that works when printing ? or that maybe there's some trick I should apply to the printer for some reason to turn alignment on ?

    -Maybe it's related to paragraphs ?! You can have multiple text blocks in one paragraph, can't you...?! Maybe I've got another block on the end which has a left aligned flag...-
    I checked - I only have the one text block per paragraph as far as I can tell.

    If you have any clues, chip in...

    Qt 5.2; VS2013; Win8

Log in to reply