Results 1 to 6 of 6

Thread: how to print an html file?

  1. #1
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to print an html file?

    I have a "QFile myhtmlFile", is there a way to print that easy with
    Qt4? I know there is QPrinter but I couldn't find where to set the
    file I want to print, anyone?

    thanx in advance

    Patcito

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to print an html file?

    Use QPrinter.
    Qt 5.3 Opensource & Creator 3.1.2

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to print an html file?

    QPrinter is a paint device. You need to layout that html file using Qt richtext capabilities (QDocumentLayout probably) and paint that layout "on" the printer (like you were using a QPainter).

  4. #4
    Join Date
    Aug 2006
    Location
    Chisinau, Moldova
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: how to print an html file?

    To print a content of a HTML file, you would need to use a QTextDocument. I would use something like the following to do it:

    HTH

    Qt Code:
    1. void print()
    2. {
    3. QFile file("myHTML.html");
    4. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    5. return;
    6.  
    7. QString htmlContent;
    8. QTextStream in(&file);
    9. in >> htmlContent;
    10.  
    11. QTextDocument *document = new QTextDocument();
    12. document->setHtml(htmlContent);
    13.  
    14. QPrinter printer;
    15.  
    16. QPrintDialog *dialog = new QPrintDialog(&printer, this);
    17. if (dialog->exec() != QDialog::Accepted)
    18. return;
    19.  
    20. document->print(&printer);
    21.  
    22. delete document;
    23. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: how to print an html file?

    and what to do if the file I have to print is a binary file (for instance pdf file, .doc file )??
    Thank you for your help
    Roby

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to print an html file?

    You have to have an application that understands these types of files.

Similar Threads

  1. print to pdf a doc file
    By rmagro in forum Qt Programming
    Replies: 4
    Last Post: 31st January 2009, 12:39
  2. How to Print a doc file (or binary file) to printer
    By rmagro in forum Qt Programming
    Replies: 15
    Last Post: 5th September 2008, 15:46
  3. Print html on fixed size
    By mamyte03@gmail.com in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2007, 10:33
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Replies: 1
    Last Post: 18th July 2006, 12:06

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.