PDA

View Full Version : How to print a data in model?



TomASS
22nd August 2009, 22:24
Hello,

I've a one simple issue.

I've a data in model from model-viev-delegate, for example:

QStandardItemModel *model= new QStandardItemModel();
and I would like to print this data. Is there any simple way to do this? Or have I print table "step by step" using drawText, drawRect (unfortunately it isn't convenient solve) etc...

Thank you very much.

vfernandez
23rd August 2009, 18:20
You might generate HTML code and use a QTextDocument to print it. For example:


void MyClass::print()
{
QString html;
html = "<html><body><table border=\"0\">";
for(int row = 0; row < m_model->rowCount(); row++) {
html += "<tr>";
for(int column = 0; column < m_model->columnCount(); column++) {
QString data = m_model->data(m_model->index(row, column), Qt::DisplayRole).toString();
html += "<td>" + data + "</td>";
}
html += "</tr>";
}
html += "</table></body></html>";

QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer);
if(dialog->exec() == QDialog::Accepted) {
QTextDocument document;
document.setHtml(html);
document.print(&printer);
}
}

TomASS
24th August 2009, 10:06
Thank you! It's working!