asweetroxxi
20th November 2012, 21:59
i created a pdf with QtextDocument. I have a table in the pdf but the bottom of the page cuts the text instead of printing on a new page(for example my table has 300 rows, page 1 shows 1-50,page 2 shows 100-50 and so on. is there anyway to prevent the overflow.
this is the standered pdf :
1.
// QPrinter *printer=new QPrinter(QPrinter::HighResolution);
2.
// printer->setPaperSize(QPrinter::A4);
3.
// printer->setOutputFormat(QPrinter::PdfFormat);
4.
// printer->setOrientation(QPrinter::Portrait);
5.
// printer->setFullPage(true);
6.
// printer->setOutputFileName(fileName);
//and this is the table
1.
// Print Table
2.
QTableWidget *table=new QTableWidget();
3.
m_mainApp->m_table->SetTableWidget(table);
4.
table->setGeometry(0,0,printer.pageRect().width(),printer .pageRect().height());
5.
table->setAutoScroll(false);
6.
m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
7.
int nbPage=m_mainApp->m_table->GetNbPrintPage(index,printer.pageRect().height());
8.
for(int page=0;page<nbPage;page++)
9.
{
10.
m_mainApp->m_table->currentIndex=-1;
11.
m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
12.
m_mainApp->m_table->FillTablePrint(index,printer.pageRect().height(),p age);
13.
painter.save();
14.
QTextDocument *editor=QTableWidget2QTextDocument(table);
15.
painter.scale(printer.resolution()/(screenResolution*1.5f), printer.resolution()/(screenResolution*1.5f));
16.
editor->drawContents(&painter);
17.
painter.restore();
18.
printer.newPage();
19.
}
amleto
20th November 2012, 23:39
you have to figure out how many rows to print on each page and draw 'separate' tables
asweetroxxi
21st November 2012, 13:47
1. how would i draw the seperators sorry im just starting so coding is weak.
2. what if i cant draw separate tables
amleto
21st November 2012, 15:32
use a table that will fit on a page and pass data to it
asweetroxxi
28th November 2012, 19:54
so i didnt set this code up it was given to me and the person who gave it to me isnt able to be around to help here is what i got so far.
this function prints and painter on a pdf, it prints a text document table and renders a graph
int index=ui->m_tableWidgetLogger->currentRow();
if(index<0)
return;
QPrinter screenPrinter(QPrinter::ScreenResolution);
int screenResolution = screenPrinter.resolution();
QPrinter printer(QPrinter::HighResolution);
printer.setOrientation(QPrinter::Portrait);
printer.setPaperSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
QPainter painter;
painter.begin(&printer);
QPen pen(Qt::black, 2, Qt::SolidLine);
painter.setPen(pen);
painter.drawRoundRect(0,0,7400,1050,25,25);
// QPen pen(Qt::black, 20, Qt::SolidLine);
// painter.setPen(pen);
// QLineF line(10,10,15000,10);
// painter.drawLine(line);
painter.scale(0.75f,0.75f);
// painter.setFont(QFont("Helvetica",24,QFont::Bold));
// QRect rect(0,0,painter.viewport().width(),painter.fontMe trics().height());
// painter.drawText(rect,"report",QTextOption(Qt::AlignCenter));
// Print Summary
QTableWidget *summaryTab=new QTableWidget();
m_mainApp->m_table->SetTableWidget(summaryTab);
summaryTab->setGeometry(6,8,printer.pageRect().width(),printer .pageRect().height());
summaryTab->setAutoScroll(false);
m_mainApp->m_table->InitTableSummary(summaryTab);
m_mainApp->m_table->FillTableSummary(summaryTab,index);
summaryTab->setAlternatingRowColors(true);
summaryTab->setFrameShape(QFrame::NoFrame);
painter.save();
QTextDocument *editor = QTableWidget2QTextBrowser(summaryTab);
painter.scale(printer.resolution()/(screenResolution*2.5f),printer.resolution()/screenResolution*.5f);
editor->drawContents(&painter);
painter.restore();
if(m_mainApp->mLoggerList[index].recCurrentAddress)
{
// Print Graph
QwtPlotRenderer renderer;
renderer.setDiscardFlag(QwtPlotRenderer::DiscardBa ckground,false);
renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames ,true);
renderer.render(m_plot,&painter,QRect(0,printer.height()/2,printer.width(),printer.height()/2.2));
printer.newPage();
// Print Table
QTableWidget *table=new QTableWidget();
m_mainApp->m_table->SetTableWidget(table);
table->setGeometry(0,0,printer.pageRect().width(),printer .pageRect().height());
table->setAutoScroll(true);
m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
int nbPage=m_mainApp->m_table->GetNbPrintPage(index,printer.pageRect().height());
for(int page=0;page<nbPage;page++)
{
m_mainApp->m_table->currentIndex=-1;
m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
m_mainApp->m_table->FillTablePrint(index,printer.pageRect().height(),p age);
painter.save();
QTextDocument *editor = QTableWidget2QTextBrowser(table);
painter.scale(printer.resolution()/(screenResolution*1.5f),printer.resolution()/screenResolution*.45f);
editor->drawContents(&painter);
painter.restore();
printer.newPage();
}
// Print Histogram
renderer.setDiscardFlag(QwtPlotRenderer::DiscardBa ckground,false);
renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames ,true);
// renderer.render(m_histogram,&painter,QRect(0,0,printer.width(),printer.height()/2.2));
renderer.render(m_histogram,&painter,QRect(0,printer.height()/2,printer.width(),printer.height()/2.2));
painter.end();
}
this is the textdocument that desgins the table
QTextDocument *editor = new QTextDocument;
editor->setDocumentMargin(25);
//creates writing format
QTextCharFormat NormalFormat;
NormalFormat.setFont(QFont("Helvetica",24,QFont::Bold));
NormalFormat.setFontWordSpacing(20);
NormalFormat.setTableCellRowSpan(200);
//create pointer to current cursor location
QTextCursor cursor (editor);
cursor.beginEditBlock();
cursor.insertImage(QImage(":/Pictures/escort.png"));
cursor.insertText("Escort Data Logger Report",NormalFormat);
// QTextBlockFormat block = cursor.blockFormat();
// Qt::Alignment vertAlign = block.alignment() & Qt::AlignVertical_Mask;
// Qt::Alignment horzAlign = block.alignment() & Qt::AlignHorizontal_Mask;
// Qt::Alignment combAlign = horzAlign | vertAlign;
// block.setAlignment(combAlign);
// cursor.setBlockFormat(block);
QTextTableFormat tableFormat;
// tableFormat.setAlignment(Qt::AlignCenter);
tableFormat.setBackground(QColor("#ffffff"));
tableFormat.setCellPadding(4);
tableFormat.setCellSpacing(3);
tableFormat.setRightMargin(9000);
QTextTable *tab=cursor.insertTable(tabW->rowCount()+1,tabW->columnCount(),tableFormat);
//QTextTable *tab=cursor.insertTable(tabW->setRowCount(200),tabW->setColumnCount(10));
QTextFrame *frame=cursor.currentFrame();
QTextFrameFormat frameFormat=frame->frameFormat();
frameFormat.setBorder(0);
frameFormat.setBorderStyle(QTextFrameFormat::Borde rStyle_Inset);
frame->setFrameFormat(frameFormat);
frameFormat.setPageBreakPolicy(QTextFormat::PageBr eak_Auto);
cursor.movePosition(QTextCursor::NextBlock);
// frameFormat.setBottomMargin(3);
frameFormat.setLeftMargin(5);
QTextCharFormat format_entete_tab;
format_entete_tab.setFontPointSize(15);
format_entete_tab.setFontWeight(QFont::Bold);
QTextCharFormat format_cellule;
format_cellule.setFontPointSize(11);
for(int i=0;i<tabW->columnCount();i++)
{
QTextTableCell titre = tab->cellAt(0,i);
QTextCursor cellCursor=titre.firstCursorPosition();
cellCursor.insertText(tabW->horizontalHeaderItem(i)->text(),format_entete_tab);
}
QTextTableCell cell;
QTextCursor cellCursor;
for(int row=1;row<tab->rows();row++)
{
for(int col=0;col<tab->columns();col++)
{
cell=tab->cellAt(row,col);
cellCursor=cell.firstCursorPosition();
}
}
cursor.endEditBlock();
and this is what populates that table. given that the info is stored in a database and then imported onto the table. my issue maxVerticalSize is too large but it's only refrenced here and i cant seem to change the number.
int CTable::GetNbPrintPage(int index,int maxVerticalSizes)
{
int nbPage;
int currentRowHeight=18;
int nbRowInPage=(maxVerticalSizes-mTab->horizontalHeader()->height())/currentRowHeight;
nbRowInPage*=3;
int nbRec=m_main->mLoggerList[index].recCurrentAddress;
nbPage=nbRec/nbRowInPage;
if(nbRec%nbRowInPage)
nbPage++;
return nbPage;
}
void CTable::FillTablePrint(int index,int maxVerticalSizes ,int page)
{
QTableWidgetItem *newItem;
QString strDate,strValue,strElapsed;
QDateTime startTime;
QTime elapsedTime;
int nbRec,startRec,col,nbRowInPage;
int currentRowHeight=18;
double data;
int val;
if(index==-1)
return;
///// newItem->setTextAlignment(30);
nbRowInPage=(maxVerticalSizes-mTab->horizontalHeader()->height())/currentRowHeight;
nbRec=m_main->mLoggerList[index].recCurrentAddress;
startRec=nbRowInPage*page*2;
if((nbRec-startRec)<(nbRowInPage*2))
nbRowInPage=(nbRec-startRec)/2;
// mTab->resizeRowsToContents();
mTab->setRowCount(nbRowInPage);
QFont font("Courrier",8,8,false);
font.setBold(true);
// mTab->setFont(font);
startTime=m_main->mLoggerList[index].recStartTime;
if(page)
{
startTime=startTime.addSecs(m_main->mLoggerList[index].prgInterval*startRec);
elapsedTime=elapsedTime.addSecs(m_main->mLoggerList[index].prgInterval*startRec);
}
for(int i=0;nbRowInPage && (startRec<nbRec);i++)
{
col=!(i&0x01)?0:6;
as a test i created a new project and and used setrowcount() and was able to figure that i needed 200 rows per page. i tried to test for how to create page breaks or split it but nothing seems to work. i was also joping to center that table which is also giving issues.
any help would be great and im sorry i am entry level so im not that quick and coding is not so amazing, iunderstand best by seeing to examples are a huge help
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.