PDA

View Full Version : cell division problem on QTextDocument



sipahi
6th February 2013, 17:48
I create QTextTable and populate it with data.
Then I print out table via using QTextDocument.

I can print out table successfully. But in some pages cells are divided.
For example If my cell is three lines, first line of table is in bottom of 5. page, second and third lines of table are in top of 6. page.
How do I get rid of this problem?

I create QTextTable:


TextDocument *document=new QTextDocument(this);
QTextCursor cursor(document);
cursor.movePosition(QTextCursor::Start);

QTextTableCellFormat cellFormat;
cellFormat.setLeftPadding(7);
cellFormat.setRightPadding(7);
cellFormat.setTopPadding(2);

QBrush blackBrush(Qt::SolidPattern);
QTextTableFormat tableFormat;
tableFormat.setAlignment(Qt::AlignLeft);
tableFormat.setBorderBrush(blackBrush);
tableFormat.setBorder(0.5);
tableFormat.setCellSpacing(0);
tableFormat.setBorderStyle(QTextFrameFormat::Borde rStyle_Solid);
tableFormat.setWidth(QTextLength(QTextLength::Perc entageLength, 100));
QTextTable *table = cursor.insertTable(100,10,tableFormat);

QTextTableCell cell=table->cellAt(1,0);
cell.setFormat(cellFormat);
cell.firstCursorPosition().insertText("this is long sentence");
....

And I print out:



QPrinter printer;
printer.setOrientation(QPrinter::Landscape);
printer.setPageMargins(10,10,10,50,QPrinter::Milli meter);
QPrintDialog *dlg = new QPrintDialog(&printer, this);
if (dlg->exec() != QDialog::Accepted)
return;
document->print(&printer);


sorry for my English

sipahi
7th February 2013, 16:33
I found a solution for my problem in stackoverflow.com (http://stackoverflow.com/a/1763683/1997790)

I created table using HTML but I noticed that "page-break-inside" is not supported by Qt.
Is there a way to use it?


QString css;
css="<style type=\"text/css\">";
css+="table { page-break-inside:auto; border-style:solid;border-width: 1px }";
css+="tr { page-break-inside:avoid; page-break-after:auto}";
css+="thead { display:table-header-group }";
css+="tfoot { display:table-footer-group }";
css+="</style>";

QString yazi;
yazi="<table><thead><tr><th>header</th></tr></thead><tbody>";
yazi.append("<tr><td>");
for(int i=0;i<ui->table->rowCount()-1;i++)
{
yazi.append("<tr><td>"+QString::number(i+1)+"</td>");
for(int j=0;j<ui->table->columnCount();j++)
{
yazi.append("<td width=\"10%\">"+ui->table->item(i,j)->text()+"</td>");
}
yazi.append("</tr>");
}
yazi.append("</tbody><tfoot><tr><td>footer</td></tr></tfoot></table>");

QPrinter printer;
printer.setOrientation(QPrinter::Landscape);
printer.setPageMargins(10,10,10,10,QPrinter::Milli meter);
QTextDocument *document=new QTextDocument();
document->setHtml(css+yazi);

QPrintDialog *dlg = new QPrintDialog(&printer, this);
if (dlg->exec() != QDialog::Accepted)
return;
document->print(&printer);