View Full Version : experiencing problem at printing out QTableWidget
rmagro
27th June 2007, 11:18
Hi Qt Community,
I'm using the following code to print out a QTableWidget,
but I only get the the contents inside the table and not the horizontalHeader..
QPrinter printer(QPrinter::HighResolution);
printer.setOrientation(QPrinter::Landscape);
QPrintDialog dlg(&printer, this);
if (dlg.exec() == QDialog::Accepted)
{
// calculate the total width/height table would need without scaling
const int rows = tableWidget->model()->rowCount();
const int cols = tableWidget->model()->columnCount();
double totalWidth = 0.0;
for (int c = 0; c < cols; ++c)
{
totalWidth += tableWidget->columnWidth(c);
}
double totalHeight = 0.0;
for (int r = 0; r < rows; ++r)
{
totalHeight += tableWidget->rowHeight(r);
}
// redirect table's painting on a pixmap
QPixmap pixmap(totalWidth, totalHeight);
QPainter::setRedirected(tableWidget->viewport(), &pixmap);
QPaintEvent event(QRect(0, 0, totalWidth, totalHeight));
QApplication::sendEvent(tableWidget->viewport(), &event);
QPainter::restoreRedirected(tableWidget->viewport());
// print scaled pixmap
QPainter painter(&printer);
painter.scale(4,4);
//painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
}
What to add to get it?
Thanks you very much for your help,
Roby
Methedrine
27th June 2007, 11:34
I don't see where you paint the header? You're only printing the rows of the table, while the horizontal- and verticalHeaders are neither rows, nor columns.
For simplification you could use the render() functionality to easily print your tablewidget into a pixmap.
rmagro
27th June 2007, 12:25
Thanks Methedrine for your reply,
Indeed I did not print the header because I didn't know how to do it combined with the rows.
Then, thanks also for the tips tou gave me about the render()..
Would you be so kind to some examples ?
Roby
Methedrine
27th June 2007, 13:09
Thanks Methedrine for your reply,
Indeed I did not print the header because I didn't know how to do it combined with the rows.
Then, thanks also for the tips tou gave me about the render()..
Would you be so kind to some examples ?
Roby
See the docs, there's an example on what you need:
QWidget::render()
:cool:
rmagro
27th June 2007, 13:26
but QWidget::render() is not a member of QTableWidget
and I need to print a QTableWidget..
Thank you,
Roberto.
jacek
27th June 2007, 13:49
but QWidget::render() is not a member of QTableWidget
QTableWidget is a QWidget too. Although that method was introduced in Qt 4.3, so if you use some earlier Qt version, you can't use it.
rmagro
27th June 2007, 13:54
I am using QT 4 and render is not a render is not a member of QWidget anymore..
any suggestions about how to get the point using the first code I posted?
Thank you,
Roby
Methedrine
27th June 2007, 14:12
I am using QT 4 and render is not a render is not a member of QWidget anymore..
Precisely, you are using a Qt 4 version < 4.3.0, as such, render is not yet a member of QWidget :p
any suggestions about how to get the point using the first code I posted?
Thank you,
Roby
Just fetch the horizontalHeader() from QTableWidget, get its height and draw it onto the pixmap before you draw the rows you have.
rmagro
27th June 2007, 14:28
I did like this ...
QPrinter printer(QPrinter::HighResolution);
printer.setOrientation(QPrinter::Landscape);
QPrintDialog dlg(&printer, this);
if (dlg.exec() == QDialog::Accepted)
{
// calculate the total width/height table would need without scaling
const int rows = tableWidget->model()->rowCount();
const int cols = tableWidget->model()->columnCount();
double totalWidth = 0.0;
for (int c = 0; c < cols; ++c)
{
totalWidth += tableWidget->columnWidth(c);
}
double totalHeight = tableWidget->horizontalHeader()->height();
for (int r = 0; r < rows; ++r)
{
totalHeight += tableWidget->rowHeight(r);
}
// redirect table's painting on a pixmap
QPixmap pixmap(totalWidth, totalHeight );
QPainter::setRedirected(tableWidget->horizontalHeader()->viewport(), &pixmap);
QPainter::setRedirected(tableWidget->viewport(), &pixmap);
QPaintEvent event(QRect(0, 0, totalWidth, totalHeight ));
QApplication::sendEvent(tableWidget->horizontalHeader()->viewport(), &event);
QApplication::sendEvent(tableWidget->viewport(), &event);
QPainter::restoreRedirected(tableWidget->horizontalHeader()->viewport());
QPainter::restoreRedirected(tableWidget->viewport());
// print scaled pixmap
QPainter painter(&printer);
painter.scale(4,4);
//painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
}
but I do not get the point..
What's wrong with that code??
Thank you for your patience
Roby
rmagro
27th June 2007, 16:04
If I change the order of the instructions in the section
" // redirect table's painting on a pixmap "
I get both the horizontal header and the rows printed out BUT they are overlapped..
How to adjust the situation??
// redirect table's painting on a pixmap
QPixmap pixmap(totalWidth, totalHeight);
QPainter::setRedirected(tableWidget->viewport(), &pixmap);
QPainter::setRedirected(tableWidget->horizontalHeader()->viewport(), &pixmap);
QPaintEvent event(QRect(0, 0, totalWidth, totalHeight));
QApplication::sendEvent(tableWidget->viewport(), &event);
QApplication::sendEvent(tableWidget->horizontalHeader()->viewport(), &event);
QPainter::restoreRedirected(tableWidget->viewport());
QPainter::restoreRedirected(tableWidget->horizontalHeader()->viewport());
// print scaled pixmap
QPainter painter(&printer);
painter.scale(4,4);
//painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
Thanks for help,
Roby
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.