PDA

View Full Version : print Table onto pdf



asweetroxxi
27th November 2012, 21:43
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


QApplication a(argc, argv);

float j= 5;
QPrinter printer;
printer.setPageMargins(j,j,j,j,QPrinter::Inch);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPageSize(QPrinter::A4);
printer.printRange();
printer.setOutputFileName("C:\\qtpdf\\test.pdf");
QPainter painter;
if (! painter.begin(&printer)) { // failed to open file
qWarning("failed to open file, is it writable?");
return 1;
}


printer.newPage();
//creates writing format
QTextCharFormat NormalFormat;


//create pointer to current cursor location
QTextCursor cursor;
cursor.beginEditBlock();

QTextTableFormat tableFormat;

tableFormat.setBackground(QColor("#ffffff"));
tableFormat.setCellPadding(4);
tableFormat.setCellSpacing(3);
tableFormat.setBottomMargin(2);

QTextTable *tab = cursor.insertTable(30,30);


// tab->insertRows(20,1000);
// tab->insertColumns(20,10);

// QTextFrame *frame=cursor.currentFrame();
// QTextFrameFormat frameFormat;
// frameFormat.setBorder(0);
// frameFormat.setBorderStyle(QTextFrameFormat::Borde rStyle_Inset);
// frameFormat.setPageBreakPolicy(QTextFormat::PageBr eak_Auto);
// cursor.movePosition(QTextCursor::NextBlock);
// 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<100;i++)
//// {
//// //QTextTableCell titre = tab->cellAt(0,i);
//// tab->cellAt(0,i);
//// //QTextCursor cellCursor=titre.firstCursorPosition();
//// QTextCursor cellCursor;
//// cellCursor.insertText("hello");
//// }
// QTextTableCell cell;

// QTextCursor cellCursor;
// for(int row=1;row<500;row++)
// {
// for(int col=0;col<60;col++)
// {
// cell.firstPosition();
// //cellCursor=cell.firstCursorPosition();
// cellCursor.insertText("bye");
// }
// }

// cursor.endEditBlock();

painter.drawText(10, 10, "Test");
painter.drawText(100,100,"hello world");
// ret=renderToImage();

int x,y,z,h;
h= 950;
z= 950;
y= 450;
x= 10;
QRect rec(x,y,z,h);
QRect rec2(20,0,100,100);

if (h>j)
{

printer.newPage();

}

painter.drawRect(rec);
painter.drawRect(rec2);
// painter.drawRect(rec3);
printer.pageRect();
painter.drawText(rec,Qt::AlignLeft,"Left");
painter.drawText(rec,Qt::AlignCenter,"Center");
painter.drawText(rec,Qt::AlignRight,"Right");
painter.drawText(rec,Qt::AlignBottom,"low");
painter.drawText(rec2,Qt::AlignLeft,"No");
painter.drawText(rec2,Qt::AlignCenter,"Maybe");
painter.drawText(rec2,Qt::AlignRight,"Yes");


printer.newPage();
printer.fullPage();
if (! printer.newPage()) {
qWarning("failed in flushing page to disk, disk full?");
return 1;
}
painter.setBrush(Qt::SolidPattern);
int r,e;
r=10;
e=50;
QString hi = "aaaaaaa";
QPen pen(Qt::black, 2, Qt::SolidLine);
painter.setPen(pen);
QLineF line(10,10,685,10);
painter.drawLine(line);

painter.end();




MainWindow w;
w.show();

return a.exec();

d_stranz
27th November 2012, 22:02
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.