Results 1 to 4 of 4

Thread: [SOLVED] QTextDocument setHtml and HTML Tags/Properties "align=center" problems

  1. #1
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default [SOLVED] QTextDocument setHtml and HTML Tags/Properties "align=center" problems

    Hello all. My first post here. I am having a devil of a time with something that ought to be simple.

    I am trying to generate a simple text Report. It is one page only. I build and format the Report using HTML from the "Supported HTML Subset". I offer a "Save Report to PDF" Button, and a "Print Report" Button. I have played with font families, font sizes, styles, colors, etc. All the HTML codes that I have tried are working perfectly except for horizontal alignment in the Print-out. The PDF file is properly aligned, as verified by Acrobat Reader.

    I have pulled my hair out for a week trying various things, eating up a few hundred sheets of paper, trying to get the print-out to align. I humbly challenge someone to please provide the trick that makes the Print-out align properly.

    As shown in the overly-simplified, yet complete and compilable code below, I assign the HTML codes to a QString as demonstrated in many different examples throughout the web. The posted code demonstrates the problem. The PDF file is correct, the Print output is not.

    Since all other HTML Tags and Properities are working (I haven't included all of them in the simplified code), it appears to me that the "align=center" issue must have a trick to it, but if so, I think I would have found reference on the web to it. I have tried many other methods, all to no avail, which is why I would like to challenge others to see if they get the same result. Please indicate what I'm doing wrong, or leaving out a step.

    I am using Qt 5.2.0, Qt Creator 3.0.0, Windows 7 Pro 64bit, and the MSVC2010 32-bit Debug Kit.

    Thank You for your help.

    The following is main.cpp:
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include <QPrinter>
    4. #include <QPrintDialog>
    5. #include <QFileDialog>
    6. #include <QTextDocument>
    7.  
    8. /*---------------------------------------------------------------------------*/
    9. void printReportStr(QString str)
    10. {
    11.  
    12. QPrinter printer;
    13. printer.setOrientation(QPrinter::Portrait);
    14. printer.setOutputFormat(QPrinter::NativeFormat);
    15. printer.setPaperSize(QPrinter::Letter);
    16. printer.setOutputFileName(NULL);
    17.  
    18. QPrintDialog prnDlg(&printer, NULL);
    19.  
    20. if (prnDlg.exec() != QDialog::Accepted)
    21. return;
    22.  
    23.  
    24. doc.setHtml(str);
    25. doc.print(&printer);
    26. }
    27.  
    28. /*---------------------------------------------------------------------------*/
    29. void saveAsPDF(QString fileName, QString str)
    30. {
    31.  
    32. QPrinter printer;
    33. printer.setOrientation(QPrinter::Portrait);
    34. printer.setOutputFormat(QPrinter::PdfFormat);
    35. printer.setPaperSize(QPrinter::Letter);
    36.  
    37. QString path = QFileDialog::getSaveFileName(NULL,
    38. "Save as PDF...", fileName,
    39. "PDF Files (*.pdf)");
    40. if (path.isEmpty())
    41. return;
    42.  
    43. printer.setOutputFileName(path);
    44.  
    45.  
    46. doc.setHtml(str);
    47. doc.print(&printer);
    48. }
    49.  
    50. /*---------------------------------------------------------------------------*/
    51. int main(int argc, char *argv[])
    52. {
    53. QApplication a(argc, argv);
    54.  
    55. QString str;
    56.  
    57. str = "<html>" "<head>" "</head>"
    58. "<body>"
    59. "<h3 align=center>Centered h3 Header</h3>"
    60. "</body>"
    61. "</html>";
    62.  
    63.  
    64.  
    65. // The following saves as a PDF-file. Using Acrobat Reader,
    66. // it is formatted and displays correctly, and centered
    67. //
    68. saveAsPDF("myPDFfile", str);
    69.  
    70.  
    71.  
    72. // The following dumps the same thing to a printer, and it prints
    73. // correctly EXCEPT for the centering. In fact, I have done all
    74. // manner of HTML code-tags and styles... changing fonts, colors,
    75. // sizes, etc, and everything is correct except for the horizontal
    76. // centering. It is always left-aligned.
    77. //
    78. printReportStr(str);
    79.  
    80.  
    81. exit(0);
    82.  
    83. return a.exec();
    84. }
    To copy to clipboard, switch view to plain text mode 

    And here is the .pro file:
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2014-02-19T19:00:37
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui printsupport widgets
    8.  
    9. TARGET = HTMLPrintDemo
    10. CONFIG += console
    11. CONFIG -= app_bundle
    12.  
    13. TEMPLATE = app
    14.  
    15.  
    16. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextDocument setHtml and HTML Tags/Properties "align=center" problems

    I can confirm that this code does the right thing under Linux with Qt 5.2.0.

    With MSVC 2010 Qt 5.1.1 the program does the right thing with the "Microsoft XPS Writer" virtual printer.
    Ditto for MingW and Qt5.1.1.
    I do not have Qt 5.2.0 on this machine or a "real" printer attached. I used A4 paper size (otherwise exactly your code).

    Have you tried using "Microsoft XPS Writer" as your printer to eliminate a printer driver curiosity?
    Have you tried an earlier Qt version to eliminate a Qt 5.2-ism?

    BTW: Thank you for taking the time to ask a smart question.

  3. The following user says thank you to ChrisW67 for this useful post:

    jjones11 (20th February 2014)

  4. #3
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextDocument setHtml and HTML Tags/Properties "align=center" problems

    Thank you ChrisW for your informative response. As per your recommendation, I tried printing to Microsoft XPS Writer and got the same left-aligned header instead of being centered. I also tried printing to a second/different printer and got the same result.

    It is interesting that it works fine on your Linux platform. Hopefully, someone with a MSVC 2010/Qt5.2.0 development environment will respond.

    I will try the MingW Kit (that's a good idea) and if that doesn't reveal anything, then I will take your recommendation and bite off an earlier Qt 5.1.1. version.

    Thanks again.
    jjones

  5. #4
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextDocument setHtml and HTML Tags/Properties "align=center" problems

    Thank you ChrisW for the ideas. I tried the MingW Kit and got the same results.

    I upgraded to Qt 5.2.1 (couldn't find 5.1.1) and now it works correctly... and I didn't have to change a thing!

    This problem is a big fat SOLVED! I am greatly relieved.

    Thanks again.
    jjones

    P.S. I don't see a way to add "SOLVED" to the Title because apparently I can't edit the original post. If some Admin would please...
    Last edited by jjones11; 20th February 2014 at 16:19. Reason: Added P.S.

Similar Threads

  1. QTextDocument table wont align
    By asweetroxxi in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2012, 15:57
  2. QTextDocument and HTML tags
    By naga in forum Qt Programming
    Replies: 2
    Last Post: 13th December 2011, 12:22
  3. Replies: 2
    Last Post: 23rd February 2010, 08:52
  4. Matching HTML tags
    By pucara_faa in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2010, 14:19
  5. How to align list items to the center?
    By zgulser in forum Qt Tools
    Replies: 4
    Last Post: 9th February 2009, 10:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.