Results 1 to 2 of 2

Thread: print Table onto pdf

  1. #1
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default print Table onto pdf

    i want to build an empty table in Qt which output into a pdf

    here is what i got so far but im having issue. Is there something im missing, it's a bit messy i know i was doing some testing

    Qt Code:
    1. QApplication a(argc, argv);
    2.  
    3. float j= 5;
    4. QPrinter printer;
    5. printer.setPageMargins(j,j,j,j,QPrinter::Inch);
    6. printer.setOutputFormat(QPrinter::PdfFormat);
    7. printer.setPageSize(QPrinter::A4);
    8. printer.printRange();
    9. printer.setOutputFileName("C:\\qtpdf\\test.pdf");
    10. QPainter painter;
    11. if (! painter.begin(&printer)) { // failed to open file
    12. qWarning("failed to open file, is it writable?");
    13. return 1;
    14. }
    15.  
    16.  
    17. printer.newPage();
    18. //creates writing format
    19. QTextCharFormat NormalFormat;
    20.  
    21.  
    22. //create pointer to current cursor location
    23. QTextCursor cursor;
    24. cursor.beginEditBlock();
    25.  
    26. QTextTableFormat tableFormat;
    27.  
    28. tableFormat.setBackground(QColor("#ffffff"));
    29. tableFormat.setCellPadding(4);
    30. tableFormat.setCellSpacing(3);
    31. tableFormat.setBottomMargin(2);
    32.  
    33. QTextTable *tab = cursor.insertTable(30,30);
    34.  
    35.  
    36. // tab->insertRows(20,1000);
    37. // tab->insertColumns(20,10);
    38.  
    39. // QTextFrame *frame=cursor.currentFrame();
    40. // QTextFrameFormat frameFormat;
    41. // frameFormat.setBorder(0);
    42. // frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Inset);
    43. // frameFormat.setPageBreakPolicy(QTextFormat::PageBreak_Auto);
    44. // cursor.movePosition(QTextCursor::NextBlock);
    45. // QTextCharFormat format_entete_tab;
    46. // format_entete_tab.setFontPointSize(15);
    47. // format_entete_tab.setFontWeight(QFont::Bold);
    48.  
    49. // QTextCharFormat format_cellule;
    50. // format_cellule.setFontPointSize(11);
    51.  
    52. //// for(int i=0;i<100;i++)
    53. //// {
    54. //// //QTextTableCell titre = tab->cellAt(0,i);
    55. //// tab->cellAt(0,i);
    56. //// //QTextCursor cellCursor=titre.firstCursorPosition();
    57. //// QTextCursor cellCursor;
    58. //// cellCursor.insertText("hello");
    59. //// }
    60. // QTextTableCell cell;
    61.  
    62. // QTextCursor cellCursor;
    63. // for(int row=1;row<500;row++)
    64. // {
    65. // for(int col=0;col<60;col++)
    66. // {
    67. // cell.firstPosition();
    68. // //cellCursor=cell.firstCursorPosition();
    69. // cellCursor.insertText("bye");
    70. // }
    71. // }
    72.  
    73. // cursor.endEditBlock();
    74.  
    75. painter.drawText(10, 10, "Test");
    76. painter.drawText(100,100,"hello world");
    77. // ret=renderToImage();
    78.  
    79. int x,y,z,h;
    80. h= 950;
    81. z= 950;
    82. y= 450;
    83. x= 10;
    84. QRect rec(x,y,z,h);
    85. QRect rec2(20,0,100,100);
    86.  
    87. if (h>j)
    88. {
    89.  
    90. printer.newPage();
    91.  
    92. }
    93.  
    94. painter.drawRect(rec);
    95. painter.drawRect(rec2);
    96. // painter.drawRect(rec3);
    97. printer.pageRect();
    98. painter.drawText(rec,Qt::AlignLeft,"Left");
    99. painter.drawText(rec,Qt::AlignCenter,"Center");
    100. painter.drawText(rec,Qt::AlignRight,"Right");
    101. painter.drawText(rec,Qt::AlignBottom,"low");
    102. painter.drawText(rec2,Qt::AlignLeft,"No");
    103. painter.drawText(rec2,Qt::AlignCenter,"Maybe");
    104. painter.drawText(rec2,Qt::AlignRight,"Yes");
    105.  
    106.  
    107. printer.newPage();
    108. printer.fullPage();
    109. if (! printer.newPage()) {
    110. qWarning("failed in flushing page to disk, disk full?");
    111. return 1;
    112. }
    113. painter.setBrush(Qt::SolidPattern);
    114. int r,e;
    115. r=10;
    116. e=50;
    117. QString hi = "aaaaaaa";
    118. QPen pen(Qt::black, 2, Qt::SolidLine);
    119. painter.setPen(pen);
    120. QLineF line(10,10,685,10);
    121. painter.drawLine(line);
    122.  
    123. painter.end();
    124.  
    125.  
    126.  
    127.  
    128. MainWindow w;
    129. w.show();
    130.  
    131. return a.exec();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: print Table onto pdf

    You cannot do anything that requires the Qt event loop in main(), because the event loop is not started until you call QApplication::exec() (which you don't do until the end of your main()).

    All of the printing code needs to be moved somewhere into your MainWindow class (not the constructor, because exec() still hasn't been called at that point).

    If you don't want to go to the trouble of creating a menu with a print command on it, put the printing code inside some slot in MainWindow. Add a signal (anything at all) to MainWindow, then add a handler for the showEvent. In the constructor, connect the signal to the printing slot. In showEvent, fire the signal. At that point, the event loop will be fully up and running and you can test your printing code.

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

    asweetroxxi (28th November 2012)

Similar Threads

  1. What is best way to print multiple pages long table?
    By squizzz in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2012, 20:34
  2. Replies: 1
    Last Post: 8th June 2011, 14:13
  3. Validator for the table widget item in table
    By mukunda in forum Qt Programming
    Replies: 4
    Last Post: 6th June 2011, 23:07
  4. Cancelling Long-running Print/Print Preview
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 16th June 2009, 23:05
  5. Replies: 9
    Last Post: 21st June 2007, 10:27

Tags for this Thread

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.