PDA

View Full Version : problem printing a QTableWidget



rmagro
29th December 2009, 09:30
Hi all,
the code that follows stop working correctly when I upgrade from Qt4.2.2 to Qt4.4.3..
The only think printed is a bold black line..

Any clue about how I can solve?

Thx very mutch



QPrinter printer(QPrinter::HighResolution);
printer.setOrientation(QPrinter::Landscape);

QTableWidget *newTableWidget = tableWidget;

QPrintDialog dlg(&printer, this);

if (dlg.exec() == QDialog::Accepted)
{
// calculate the total width/height table would need without scaling
const int rows = newTableWidget ->model()->rowCount();
const int cols = newTableWidget ->model()->columnCount();

double totalWidth = 0.0;

for (int c = 0; c < cols; ++c)
{
totalWidth += newTableWidget ->columnWidth(c);
}

double totalHeight = 0.0;

for (int r = 0; r < rows; ++r)
{
totalHeight += newTableWidget ->rowHeight(r);
}

double totalHeight1;
int num_page = 1;

while (totalHeight > 900)
{
totalHeight1 = 900;

// redirect table's painting on a pixmap
QPixmap pixmap(totalWidth, newTableWidget->horizontalHeader()->height());
QPixmap pixmap1(totalWidth, totalHeight1);

QPaintEvent event(QRect(0, 0, totalWidth, newTableWidget->horizontalHeader()->height()));
QPaintEvent event1(QRect(0, 0, totalWidth, totalHeight1));

QPainter::setRedirected(newTableWidget->horizontalHeader()->viewport(), &pixmap);
QApplication::sendEvent(newTableWidget->horizontalHeader()->viewport(), &event);
QPainter::restoreRedirected(newTableWidget->horizontalHeader()->viewport());

QPainter::setRedirected(newTableWidget->viewport(), &pixmap1);
QApplication::sendEvent(newTableWidget->viewport(), &event1);
QPainter::restoreRedirected(newTableWidget->viewport());

// print scaled pixmap
QPainter painter(&printer);

QDateTime dataDiOggi = QDateTime::currentDateTime();
QString dataDiOggiStringa = dataDiOggi.toString("dd.MM.yyyy hh:mm:ss");

painter.drawText(0, 250, tr("Qt 4.3.3 - Date: %1 -") .arg(dataDiOggiStringa));
painter.drawText(0, 150, tr("Page %1") .arg(num_page));

painter.scale(4.5,4.5);
painter.drawPixmap(0, 100, totalWidth, newTableWidget->horizontalHeader()->height(), pixmap);
painter.drawPixmap(0, 100+newTableWidget->horizontalHeader()->height(), totalWidth, totalHeight1, pixmap1);

totalHeight = totalHeight - totalHeight1;
printer.newPage();
numero_pagina = numero_pagina + 1;
}

QPixmap pixmap(totalWidth, newTableWidget->horizontalHeader()->height());
QPixmap pixmap1(totalWidth, totalHeight);

QPaintEvent event(QRect(0, 0, totalWidth, newTableWidget->horizontalHeader()->height()));
QPaintEvent event1(QRect(0, 0, totalWidth, totalHeight));

QPainter::setRedirected(newTableWidget->horizontalHeader()->viewport(), &pixmap);
QApplication::sendEvent(newTableWidget->horizontalHeader()->viewport(), &event);
QPainter::restoreRedirected(newTableWidget->horizontalHeader()->viewport());

QPainter::setRedirected(newTableWidget->viewport(), &pixmap1);
QApplication::sendEvent(newTableWidget->viewport(), &event1);
QPainter::restoreRedirected(newTableWidget->viewport());

// print scaled pixmap
QPainter painter(&printer);

QDateTime dataDiOggi = QDateTime::currentDateTime();
QString dataDiOggiStringa = dataDiOggi.toString("dd.MM.yyyy hh:mm:ss");

painter.drawText(0, 250, tr("Qt 4.3.3 - Date: %1 - ") .arg(dataDiOggiStringa));
painter.drawText(0, 150, tr("Page %1") .arg(num_page));

painter.scale(4.5,4.5);
painter.drawPixmap(0, 100, totalWidth, newTableWidget->horizontalHeader()->height(), pixmap);
painter.drawPixmap(0, 100+newTableWidget->horizontalHeader()->height(), totalWidth, totalHeight, pixmap1);
}

high_flyer
29th December 2009, 10:08
With out looking to much in to the code, if this is NOT in a paintEvent(), this should not have worked on Qt4.X at all.

rmagro
29th December 2009, 10:21
Surely QPaintEvent is used


QPaintEvent event(QRect(0, 0, totalWidth, newTableWidget->horizontalHeader()->height()));
QPaintEvent event1(QRect(0, 0, totalWidth, totalHeight));

high_flyer
29th December 2009, 10:27
EDIT
Deleted

rmagro
11th January 2010, 13:06
Any help??
with Qt 4.2.2 the code works good. With higher version it stops working correctly. I mean it does not print QtableWidget but only a blod black line..
Thx
Roby

faldzip
11th January 2010, 14:59
I didn't inspect your code much but I imagine you use paint events to paint table widget on a pixmap? If so then just use QWidget::render() - much more simple, so much less space for mistake.