Results 1 to 5 of 5

Thread: editor->setHtml doesn't

  1. #1
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default editor->setHtml doesn't

    For all last year I used the following code to generate a printout of a report. I loaded a QString var, strHTMLtext, with html markup in order to create formatting. Then I'd create an editor object and use its "setHtml" function to parse the markup code into formatted text.
    Then I'd create a document object using the editor object, and send that to the printer.

    I't stopped working sometime between September of last year, the end of last year's Homestead season, and this week, the beginning of the new season. The only significant change is the upgrading to 4.2.2.

    Here's the code.

    Qt Code:
    1. QFont f("TypeWriter",10);
    2. QTextEdit *editor = new QTextEdit(this);
    3. editor->setHtml(strHTMLtext);
    4. editor->setCurrentFont(f);
    5. QTextDocument *document = editor->document();
    6. QPrinter printer;
    7. QPrintDialog *dlg = new QPrintDialog(&printer,this);
    8. if (dlg->exec() == QDialog::Accepted){
    9. document->print(&printer);
    10. }
    11. delete dlg;
    12. delete editor;
    To copy to clipboard, switch view to plain text mode 
    If I make the editor visible in a window and then send it to the printer I get an good printout, otherwise I get a single, blank page.

    As a temporary fix I've created a temp.exe which is an editor app that reads the html file from the HD, and uses setHtml(in.readALL()) to parse it into the editor. Then I print it. It's not the one button click it used to be, but it works.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: editor->setHtml doesn't

    You don't need QTextEdit at all.

    Try this:
    Qt Code:
    1. QFont f( "TypeWriter", 10 );
    2.  
    3. QTextDocument document;
    4. document.setDefaultFont( f );
    5. document.setHtml( strHTMLtext );
    6.  
    7. QPrinter printer;
    8. QPrintDialog dlg( &printer, this );
    9. if( dlg.exec() == QDialog::Accepted ) {
    10. document.print( &printer );
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    GreyGeek (17th February 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: editor->setHtml doesn't

    Quote Originally Posted by jacek View Post
    You don't need QTextEdit at all.

    Try this:
    Qt Code:
    1. QFont f( "TypeWriter", 10 );
    2.  
    3. QTextDocument document;
    4. document.setDefaultFont( f );
    5. document.setHtml( strHTMLtext );
    6.  
    7. QPrinter printer;
    8. QPrintDialog dlg( &printer, this );
    9. if( dlg.exec() == QDialog::Accepted ) {
    10. document.print( &printer );
    11. }
    To copy to clipboard, switch view to plain text mode 
    Thanks, Jacek, I'll try that.

    BTW, I found out why this code stopped working. Somewhere between 4.1 and 4.2.2 the QTextEdit object now has to display it contents on the screen before it will send them to the printer.

  5. #4
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: editor->setHtml doesn't

    Quote Originally Posted by jacek View Post
    You don't need QTextEdit at all.

    Try this:
    Qt Code:
    1. QFont f( "TypeWriter", 10 );
    2.  
    3. QTextDocument document;
    4. document.setDefaultFont( f );
    5. document.setHtml( strHTMLtext );
    6.  
    7. QPrinter printer;
    8. QPrintDialog dlg( &printer, this );
    9. if( dlg.exec() == QDialog::Accepted ) {
    10. document.print( &printer );
    11. }
    To copy to clipboard, switch view to plain text mode 
    Well, I tried this method and it produced the same results. It spits a blank page out of the printer. I've submitted this problem to Trolltech support. I'll let you know what they tell me.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: editor->setHtml doesn't

    Quote Originally Posted by GreyGeek View Post
    Well, I tried this method and it produced the same results. It spits a blank page out of the printer.
    Hmmm... It works for me. Maybe there's something wrong with strHTMLtext? Also try printing without changing the default font.

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.