Results 1 to 2 of 2

Thread: Printing some data

  1. #1
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Printing some data

    Hi guys,

    I'm currently busy with a printing function for my program. But I can't get some scales right.

    The problem is that the text is parsed on one line and when the paper ends it doesn't start at a new line?

    This is my current code

    Qt Code:
    1. #include "print.h"
    2. #include <QPrinter>
    3. #include <QPrintDialog>
    4. #include <QPainter>
    5.  
    6. void print::printOrder(const QStringList client, const QStringList data){
    7. //generate printing instance
    8. QPrinter printer;
    9. printer.setPageSize(printer.A4);
    10.  
    11. //create print dialog, pass printer instance
    12. QPrintDialog *pDlg = new QPrintDialog(&printer, this);
    13. pDlg->setWindowTitle(tr("Bestelling printen"));
    14.  
    15. //debugging
    16. printer.setOutputFileName("rtest.pdf");
    17.  
    18.  
    19. if(pDlg->exec())
    20. return;
    21. qDebug("printen");
    22.  
    23.  
    24. //generate QPainter instance
    25. QPainter painter;
    26.  
    27. //pass printer reference to the painter.begin() function
    28. painter.begin(&printer);
    29.  
    30. painter.setPen(Qt::black);
    31. painter.setFont(QFont("Arial", 14));
    32.  
    33. painter.drawText(rect(), Qt::AlignLeft, "Bestelling overzicht");
    34. painter.translate(0, 20);
    35.  
    36. int count = client.size();
    37.  
    38. for(int i = 1; i < count; i++){
    39. painter.drawText(rect(), Qt::AlignLeft, client.at(i).toLocal8Bit().constData());
    40. painter.translate(0, 20);
    41. }
    42.  
    43. painter.translate(0, 40);
    44. painter.drawText(rect(), Qt::AlignLeft, "Bestelling");
    45. painter.translate(0, 20);
    46.  
    47. int countData = data.size();
    48.  
    49. for(int i = 1; i < countData; i++){
    50. painter.drawText(0, 0, 100, 100, Qt::AlignLeft, data.at(i).toLocal8Bit().constData());
    51. painter.translate(0, 20);
    52. }
    53.  
    54. painter.save();
    55.  
    56.  
    57. }
    58.  
    59. void print::printWorkingOrder(){
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 

    The text is parsed in the multiple lines except for the text in the QStringList that is to long to parse on the page and some how doesn't want to be parsed on multiple lines?

    Does somebody know how to parse long lines of text within the boundaries of the paper on multiple lines?

    Greets,

    Cyberboy
    Last edited by cyberboy; 9th April 2008 at 15:45. Reason: spelling error

  2. #2
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Printing some data

    After a little break and a clean mind I went further with the printing code.

    This is what i've got until now :

    Qt Code:
    1. void print::printOrder(const QStringList client, const QStringList data){
    2. //generate printing instance
    3. QPrinter printer;
    4. printer.setPageSize(printer.A4);
    5.  
    6. //create print dialog, pass printer instance
    7. QPrintDialog *pDlg = new QPrintDialog(&printer, this);
    8. pDlg->setWindowTitle(tr("Bestelling printen"));
    9.  
    10. //debugging
    11. printer.setOutputFileName("rtest.pdf");
    12.  
    13. //get page scales
    14. const double xScale = printer.pageRect().width();
    15. const double yScale = printer.pageRect().height();
    16.  
    17. double yPos = 0;
    18. double xPos = 0;
    19.  
    20. qDebug("xScale:%f",xScale);
    21. qDebug("yScale:%f", yScale);
    22.  
    23. if(pDlg->exec())
    24. return;
    25.  
    26.  
    27.  
    28. //generate QPainter instance
    29. QPainter painter;
    30.  
    31. //pass printer reference to the painter.begin() function
    32. painter.begin(&printer);
    33.  
    34. //set scale
    35.  
    36. painter.setPen(Qt::black);
    37. painter.setFont(QFont("Arial", 14));
    38.  
    39. painter.drawText(rect(), Qt::AlignLeft, "Bestelling overzicht");
    40. painter.translate(0, 20);
    41. yPos += 20;
    42.  
    43. int count = client.size();
    44.  
    45. for(int i = 1; i < count; i++){
    46. QRectF rectt = painter.boundingRect(QRect(), Qt::AlignLeft, client.at(i).toLocal8Bit().constData());
    47.  
    48. qDebug("height:%f", rectt.height());
    49. qDebug("width:%f", rectt.width());
    50.  
    51.  
    52. yPos += rectt.height();
    53.  
    54. if( yPos < yScale ){
    55.  
    56. painter.drawText(rect(), Qt::AlignLeft, client.at(i).toLocal8Bit().constData());
    57. painter.translate(0, rectt.height());
    58.  
    59. }else{
    60. //new print page
    61. printer.newPage();
    62. painter.resetTransform();
    63. yPos = 0;
    64.  
    65. painter.drawText(rect(), Qt::AlignLeft, client.at(i).toLocal8Bit().constData());
    66. painter.translate(0, rectt.height());
    67.  
    68. }
    69. }
    70.  
    71. painter.drawText(rect(), Qt::AlignLeft, "Bestelling");
    72. painter.translate(0, 20);
    73. yPos += 20;
    74.  
    75. int countData = data.size();
    76.  
    77. for(int i = 1; i < countData; i++){
    78. QRectF rectt = painter.boundingRect(QRect(), Qt::AlignLeft, data.at(i).toLocal8Bit().constData());
    79.  
    80. qDebug("height:%f", rectt.height());
    81. qDebug("width:%f", rectt.width());
    82.  
    83. yPos += rectt.height();
    84.  
    85. if( yPos < yScale){
    86.  
    87. painter.drawText(rect(), Qt::AlignLeft, data.at(i).toLocal8Bit().constData());
    88. painter.translate(0, rectt.height());
    89.  
    90. }else{
    91.  
    92. printer.newPage();
    93. painter.resetTransform();
    94. yPos = 0;
    95.  
    96. painter.drawText(rect(), Qt::AlignLeft, data.at(i).toLocal8Bit().constData());
    97. painter.translate(0, rectt.height());
    98.  
    99.  
    100. }
    101.  
    102. }
    103.  
    104. painter.save();
    105. }
    To copy to clipboard, switch view to plain text mode 

    And it works, the y coordinates and the translations works fine.
    I can check if the text length is to big but what then, I don't know how to chop the text in half and start on a newline?

    Can somebody help me with that?

    Greetz,

    Cyberboy

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. Replies: 4
    Last Post: 19th October 2007, 19:47
  3. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.