Results 1 to 7 of 7

Thread: Printing a Qstring

  1. #1
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Printing a Qstring

    hi,

    i've got a problem with a Qstring i want to print. the string is very long an contains a table made with <tr>, <th>, <br> and so on.

    Printing it with the following configuration leads to a result, in which only one line exists and ends at the end of the page. The tags like <br> are idnored -simply printed-, too.
    Qt Code:
    1. void THIS::printQString() {
    2.  
    3. Q_ASSERT(xLabel->text());
    4. QPrintDialog dialog(&printer, this);
    5.  
    6. if (dialog.exec()) {
    7. QPainter painter(&printer);
    8. QFont font("Courier", 8 );
    9. painter.setPen( Qt::black );
    10. painter.setFont(font);
    11.  
    12. painter.drawText(10, 10,xLabel->text());
    13. }
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    xLabel->setText(COMPLEXTEXT); does exactely what i want but painter.drawText(10, 10,xLabel->text()); doesn't format the QString in the same way. I want the printing result look exactely the same way as the QLabel xLabel (it fits on a page for sure).

    How can this be done?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing a Qstring

    Grab the label into a pixmap and print the damn thing .
    Of course QPainter won't do anything to the text. It doesn't know HTML.

    Regards

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

    harakiri (24th June 2007)

  4. #3
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing a Qstring

    yes, nice idea, thank you.

    so far, i've tried the following:
    Qt Code:
    1. void THIS::printX() {
    2.  
    3. QPrintDialog dialog(&printer, this);
    4.  
    5. QPixmap* tempXPixmap = new QPixmap;
    6. tempXPixmap->grabWidget(xLabel);
    7.  
    8. if (dialog.exec()) {
    9. QPainter painter(&printer);
    10. QRect rect = painter.viewport();
    11. QSize size = tempXPixmap->size();
    12. size.scale(rect.size(), Qt::KeepAspectRatio);
    13. painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
    14. painter.setWindow(tempXPixmap->rect());
    15. painter.drawPixmap(0, 0, *tempXPixmap);
    16. }
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    this results in a segmentation fault..

    gdb backtrace:

    (gdb) backtrace
    #0 0xb61bc8f7 in __divdi3 () from /lib/libgcc_s.so.1
    #1 0xb62a8c0a in QSize:: scale () from /usr/lib/libQtCore.so.4
    #2 0x080ba2d2 in MainWindow:: printX ()
    #3 0x080f0a4d in MainWindow:: qt_metacall ()
    #4 0xb6330aa8 in QMetaObject:: activate () from /usr/lib/libQtCore.so.4
    #5 0xb6330d2f in QMetaObject:: activate () from /usr/lib/libQtCore.so.4
    #6 0xb64bf831 in QAction:: triggered () from /usr/lib/libQtGui.so.4
    #7 0xb64c01bf in QAction:: activate () from /usr/lib/libQtGui.so.4
    #8 0xb67c6fd4 in ?? () from /usr/lib/libQtGui.so.4
    .....
    so why doesn't the scale work? i used this method to print other pixmaps, is there a problem with the grabWidget() ?

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing a Qstring

    Could be because painter->viewport() returns a default rect ( 0, 0, 0, 0). Therefore a division by 0 will occur in scale.

    The default rect means that the paint device has an empty size ( 0, 0 ).

    See if you can set in QPrinter the output media size. Otherwise use QPainter::setViewport().

    The idea is to make sure the paint device has valid rect.

    Regards

  6. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing a Qstring

    OK. Forget that !.

    grabPixmap is static in QPixmap and returns the grabbed pix.
    So you called it wrong there.

    Should have been:
    Qt Code:
    1. QPixmap pix = QPixmap::grabWidget(xLabel);
    To copy to clipboard, switch view to plain text mode 

    Regards

  7. The following user says thank you to marcel for this useful post:

    harakiri (24th June 2007)

  8. #6
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing a Qstring

    works fine - thanks marcel

  9. #7
    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: Printing a Qstring

    How about:
    Qt Code:
    1. QTextDocument doc(label->text());
    2. QPrinter printer;
    3. //...
    4. doc.print(&printer);
    To copy to clipboard, switch view to plain text mode 
    Of course you have to setup the printer first...

Similar Threads

  1. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  4. Printing Copyright Cicle C from QString
    By jakamph in forum Newbie
    Replies: 7
    Last Post: 13th February 2006, 22:04
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

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.