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