PDA

View Full Version : Printing values from QTableWidget???



prabhudev
6th August 2012, 10:51
Hi i am using qtablewidget in my project..it has 10 columns & 5000 rows...i want to print the whole tablewidget... When i use the below code i get a empty pdf for 5000 rows...if i use it for less rows like 50 ,it prints only the visible area of the table widget...
How to solve this issue?
Is there any way do display a widget page wise , say 100 rows per page and take printout of 100 rows and print another 100 from 2nd page?
kindly help

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 = ui->Packet_Info->model()->rowCount();
const int cols = ui->Packet_Info->model()->columnCount();

double totalWidth = 0.0;

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

double totalHeight = ui->Packet_Info->horizontalHeader()->height();

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


// redirect table's painting on a pixmap
QPixmap pixmap(totalWidth, totalHeight );

QPainter::setRedirected(ui->Packet_Info->horizontalHeader()->viewport(), &pixmap);
QPainter::setRedirected(ui->Packet_Info->viewport(), &pixmap);

QPaintEvent event(QRect(0, 0, totalWidth, totalHeight ));

QApplication::sendEvent(ui->Packet_Info->horizontalHeader()->viewport(), &event);
QApplication::sendEvent(ui->Packet_Info->viewport(), &event);

QPainter::restoreRedirected(ui->Packet_Info->horizontalHeader()->viewport());
QPainter::restoreRedirected(ui->Packet_Info->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());

}

Qtonimo
6th August 2012, 11:20
Look at this project http://qt-apps.org/content/show.php/TableView+Printer?content=76616.
It uses QTableView, but maybe it is useful.

prabhudev
6th August 2012, 11:46
Thanks...I will go through it...Is there any other option coz i am using tablewidget...

Qtonimo
6th August 2012, 12:47
You can do it by your own.
If you want a good layout I would recommend QTextDocument, QTextTable etc. ....

prabhudev
7th August 2012, 11:55
Can someone please tell how to display tablewidget in page wise...

markjan1
7th August 2012, 21:07
This works just fine for me. Perhaps your table does not have an item at those coordinates?

prabhudev
21st August 2012, 10:19
http://qt-apps.org/content/show.php/...?content=76616.
This worked fine...had to change some code in that to use my qtablewidget...
Thanks Qtonimo