hello,

I want to align my table but no matter what i try it wont align. So this is how the program is set up i have two tables one small one very large. I was able to get the large to center but no matter how hard i try the small table doesnt want to move to the the middle. here is the code
Qt Code:
  1. int index=ui->m_tableWidgetLogger->currentRow();
  2. if(index<0)
  3. return;
  4. QPrinter screenPrinter(QPrinter::ScreenResolution);
  5. int screenResolution = screenPrinter.resolution();
  6. QPrinter printer(QPrinter::HighResolution);
  7. printer.setOrientation(QPrinter::Portrait);
  8. printer.setPaperSize(QPrinter::A4);
  9. printer.setOutputFormat(QPrinter::PdfFormat);
  10. printer.setOutputFileName(fileName);
  11.  
  12.  
  13. QPainter painter;
  14. painter.begin(&printer);
  15. //////INSERTS THIS BOARDER DESGIN MIGHT HAVE TO CHANGE
  16. // QPen pen(Qt::black, 2, Qt::SolidLine);
  17. // painter.setPen(pen);
  18. // painter.drawRoundRect(0,0,9400,1050,15,15);
  19. // QPen pen(Qt::black, 20);
  20. // painter.setPen(pen);
  21. // QLineF line(30,30,15000,30);
  22. // painter.drawLine(line);
  23. // painter.setFont(QFont("Helvetica",24,QFont::Bold));
  24. QRectF rect(10,10,2000,1000);
  25. QString head;
  26. head= "Escort Data Logger Report";
  27. // QRectF source(0,0,70,40);
  28. // painter.drawText(rect,"report",QTextOption(Qt::AlignCenter));
  29. QDateTime dateTime = QDateTime::currentDateTime();
  30. QImage image(":/Pictures/escort.png");
  31. QFont font;
  32. font.setBold(true);
  33. QString dateTimeString = dateTime.toString();
  34. painter.drawImage(rect,image);
  35. painter.drawText(painter.window(),Qt::AlignCenter|Qt::AlignBottom,QString::number(1));
  36. painter.drawText(painter.window(),Qt::AlignRight|Qt::AlignJustify,QString("Date: "+dateTimeString));
  37.  
  38.  
  39. // Print Summary
  40.  
  41. QTableWidget *summaryTab=new QTableWidget();
  42. m_mainApp->m_table->SetTableWidget(summaryTab);
  43. // summaryTab->setGeometry(700,0,printer.pageRect().width(),printer.pageRect().height());
  44. summaryTab->setAutoScroll(false);
  45. m_mainApp->m_table->InitTableSummary(summaryTab);
  46. m_mainApp->m_table->FillTableSummary(summaryTab,index);
  47. summaryTab->setAlternatingRowColors(true);
  48. summaryTab->setFrameShape(QFrame::Box);
  49.  
  50.  
  51.  
  52. painter.save();
  53. QTextDocument *editor = QTableWidget2QTextBrowser(summaryTab);
  54. editor->indentWidth();
  55. painter.scale(printer.resolution()/(screenResolution*1),printer.resolution()/screenResolution*.55f);
  56. editor->drawContents(&painter);
  57.  
  58. painter.restore();
  59.  
  60. if(m_mainApp->mLoggerList[index].recCurrentAddress)
  61. {
  62. // Print Graph
  63. painter.drawText(painter.window(),Qt::AlignRight|Qt::AlignTop,QString("Date: "+dateTimeString));
  64.  
  65. QwtPlotRenderer renderer;
  66. renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground,false);
  67. renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames,true);
  68. renderer.render(m_plot,&painter,QRect(0,printer.height()/2,printer.width(),printer.height()/2.2));
  69. printer.newPage();
  70.  
  71. // Print Table
  72.  
  73. QTableWidget *table=new QTableWidget();
  74. m_mainApp->m_table->SetTableWidget(table);
  75. table->setGeometry(0,0,printer.pageRect().width(),printer.pageRect().height());
  76. table->setAutoScroll(false);
  77.  
  78. m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
  79. int nbPage=m_mainApp->m_table->GetNbPrintPage(index,printer.pageRect().height());
  80.  
  81. for(int page=0;page<nbPage;page++)
  82. {
  83. painter.drawImage(rect,image);
  84. painter.drawText(painter.window(),Qt::AlignRight|Qt::AlignTop,QString("Date: "+dateTimeString));
  85. painter.drawText(painter.window(),Qt::AlignCenter|Qt::AlignBottom,QString::number(page+2));
  86. m_mainApp->m_table->currentIndex=-1;
  87. m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
  88. m_mainApp->m_table->FillTablePrint(index,printer.pageRect().height(),page);
  89.  
  90. painter.save();
  91. QTextDocument *editor = QTableWidget2QTextBrowser(table);
  92. painter.scale(printer.resolution()/(screenResolution*1.2f),printer.resolution()/screenResolution*.55f);
  93. editor->drawContents(&painter);
  94. painter.restore();
  95. printer.newPage();
  96.  
  97. }
  98. // Print Histogram
  99. painter.drawImage(rect,image);
  100. painter.drawText(painter.window(),Qt::AlignRight|Qt::AlignTop,QString("Date: "+dateTimeString));
  101. painter.drawText(painter.window(),Qt::AlignCenter|Qt::AlignBottom,QString::number(nbPage+2));
  102. renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground,false);
  103. renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames,true);
  104. // renderer.render(m_histogram,&painter,QRect(0,0,printer.width(),printer.height()/2.2));
  105. renderer.render(m_histogram,&painter,QRect(0,printer.height()/6,printer.width(),printer.height()/2.5));///was 2.2
  106.  
  107. painter.end();
  108.  
  109. }
  110.  
  111.  
  112.  
  113. #if(DEBUG_MODE==1)
  114. qDebug() << "GeneratePdf...";
  115. #endif
  116. }
  117.  
  118.  
  119. QTextDocument *MainWindow::QTableWidget2QTextBrowser(QTableWidget *tabW)
  120. {
  121. // editor->setDocumentMargin(25);
  122.  
  123.  
  124. //creates writing format
  125. QTextCharFormat NormalFormat;
  126. NormalFormat.setFont(QFont("Helvetica",24,QFont::Bold));
  127.  
  128. //create pointer to current cursor location
  129. QTextCursor cursor (editor);
  130.  
  131. cursor.beginEditBlock();
  132. cursor.movePosition(QTextCursor::Start);
  133. // // cursor.insertImage(QImage(":/Pictures/escort.png"));
  134.  
  135. cursor.insertBlock();
  136. cursor.insertBlock();
  137. cursor.insertBlock();
  138. cursor.insertBlock();
  139. cursor.insertBlock();
  140. cursor.insertBlock();
  141. cursor.insertBlock();
  142. cursor.insertBlock();
  143. cursor.insertBlock();
  144. cursor.insertBlock();
  145. cursor.insertBlock();
  146. cursor.insertText(" ",NormalFormat);
  147. cursor.insertText("Escort Data Logger Report",NormalFormat);
  148. cursor.insertBlock();
  149. cursor.insertBlock();
  150. cursor.insertBlock();
  151.  
  152.  
  153.  
  154. QTextTableFormat tableFormat;
  155. QTextEdit textEdit;
  156. textEdit.setAlignment(Qt::AlignCenter);
  157.  
  158.  
  159. QTextTable *tab=cursor.insertTable(tabW->rowCount()+1,tabW->columnCount(),tableFormat);
  160. tableFormat.setBackground(QColor("#ffffff"));
  161. tableFormat.setCellPadding(0);
  162. tableFormat.setCellSpacing(4);
  163. //tableFormat.setHeight(QTextLength(QTextLength::PercentageLength,10));
  164.  
  165. QTextFrame *frame=cursor.currentFrame();
  166.  
  167. QTextFrameFormat frameFormat=frame->frameFormat();
  168. frameFormat.setBorder(0);
  169. frameFormat.setLeftMargin(50);
  170. frameFormat.setTopMargin(10);
  171. // frameFormat.setBottomMargin(50);
  172. cursor.insertFrame(frameFormat);
  173. cursor.setPosition(frame->lastPosition());
  174. frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Inset);
  175. frame->setFrameFormat(frameFormat);
  176.  
  177.  
  178. QTextCharFormat format_entete_tab;
  179. format_entete_tab.setFontPointSize(15);
  180. format_entete_tab.setFontWeight(QFont::Bold);
  181.  
  182. QTextCharFormat format_cellule;
  183. format_cellule.setFontPointSize(11);
  184.  
  185. for(int i=0;i<tabW->columnCount();i++)
  186. {
  187. QTextTableCell titre = tab->cellAt(0,i);// change 0 it moves the title
  188. QTextCursor cellCursor=titre.firstCursorPosition();
  189. cellCursor.insertText(tabW->horizontalHeaderItem(i)->text(),format_entete_tab);
  190. }
  191.  
  192. QTextCursor cellCursor;
  193.  
  194. for(int row=1;row<tab->rows();row++)
  195. {
  196. for(int col=0;col<tab->columns();col++)
  197. {
  198. cell=tab->cellAt(row,col);
  199. cellCursor=cell.firstCursorPosition();
  200. cellCursor.insertText(tr("%1").arg(tabW->item(row-1,col)->text()),format_cellule);
  201. }
  202. }
  203.  
  204. cursor.endEditBlock();
  205. return editor;
To copy to clipboard, switch view to plain text mode