Results 1 to 1 of 1

Thread: Printing problem - solved

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Printing problem - solved

    I'm trying to print an address label. Tried two different methods but neither one is perfect. Method one, using QTextDocument prints all 3 lines perfectly to a laser printer, or to Snagit, but just ejects a blank label from the label printer.

    Method two prints each line of text in the same place on the same line each line over writing the other, but it does print on the label printer as well as the other devices.
    There must be a simple way to get this right, but I don't see it.
    Thanks for any suggestions.

    Qt Code:
    1. // Method 1
    2.  
    3. QPrinter *printer = new QPrinter();
    4. printer->setOrientation(QPrinter::Landscape);
    5.  
    6. QPrintDialog *dialog = new QPrintDialog(printer, this);
    7. if (dialog->exec() != QDialog::Accepted)
    8. {
    9. return;
    10. }
    11.  
    12. QString nm = "My Name\n";
    13. QString st = "My Address\n";
    14. QString cty = "My City My State My Zipcode";
    15.  
    16. QString addr = nm + st + cty;
    17.  
    18. doc.setPlainText(addr);
    19.  
    20. // prints ok on the laser but nothing on the label printer
    21. doc.print(printer);
    22.  
    23. delete dialog;
    24. delete printer;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // Method 2
    2.  
    3. QPrinter printer;
    4. printer.setOrientation(QPrinter::Landscape);
    5.  
    6. QPrintDialog *dialog = new QPrintDialog(&printer, this);
    7. if (dialog->exec() != QDialog::Accepted)
    8. {
    9. return;
    10. }
    11.  
    12. list.append("My Name\n");
    13. list.append("My Address\n");
    14. list.append("My City My State My Zipcode");
    15.  
    16. QPainter painter(&printer);
    17. painter.setPen(Qt::black);
    18. QRectF textArea(
    19. printer.pageRect().left() +printer.resolution() *0.1,
    20. printer.pageRect().top() +printer.resolution() *0.1,
    21. printer.pageRect().width() +printer.resolution() *3.5,
    22. printer.pageRect().height() +printer.resolution() *1.125);
    23. painter.drawRect(textArea);
    24.  
    25. // Prints everything on the same line
    26. for (int i = 0; i < list.size(); i++)
    27. {
    28. painter.drawText(textArea, QTextOption::WordWrap | Qt::AlignLeft, list[i]);
    29. }
    30.  
    31. delete dialog;
    To copy to clipboard, switch view to plain text mode 

    Update - I took a different approach and it works fine.
    Qt Code:
    1. int x = 10;
    2. int y = 20;
    3. for (int i = 0; i < list.size(); i++)
    4. {
    5. painter.drawText(x, y, list[i]);
    6. y = y +15;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by waynew; 2nd October 2010 at 19:53. Reason: updated contents

Similar Threads

  1. Printing Problem
    By cutie.monkey in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2010, 04:59
  2. printing problem
    By #andi# in forum Newbie
    Replies: 0
    Last Post: 9th August 2009, 21:01
  3. printing problem
    By vishnu5 in forum Newbie
    Replies: 3
    Last Post: 17th December 2007, 19:22
  4. Printing problem in windows
    By joseph in forum Qt Programming
    Replies: 6
    Last Post: 12th July 2007, 08:04
  5. printing problem
    By igor_x in forum Newbie
    Replies: 3
    Last Post: 8th November 2006, 10:50

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.