PDA

View Full Version : Printing QTableWidget



rmagro
20th June 2007, 10:52
Hi all guys,

I know there was a discussion in this forum in the past,
but even if I put the best of my attention I was not able to
get the goal :confused:

Can someone help?

Thank you in advance.

Roberto

rmagro
20th June 2007, 12:05
Hi all,

How to print aQTableWidget?
I know there was a discussion in this forum in the past,
but even if I put the best of my attention I was not able to
get the goal

If someone has example code please can help?

Thank you in advance.

Roberto

rmagro
20th June 2007, 13:36
Hi

about printing a QtableWidget,
I tried again the code suggested once by JPN,
but no success..

Attached you will find what I get :eek:

Any clue? any idead..?
What could be wrong?

Thank you,

Roberto

jpn
20th June 2007, 13:42
Hi. First of all, please don't start multiple threads on the same subject.

What could be wrong?
You might want to adjust the scaling a bit. Looks like the content is scaled to fit the page.

rmagro
20th June 2007, 13:48
Thanks JPN

You might want to adjust the scaling a bit. Looks like the content is scaled to fit the page.[/QUOTE]

Which line of your suggested code should I modify?

regards

jpn
20th June 2007, 13:54
Which line of your suggested code should I modify?
Sorry, but which code snippet are you referring to? :)

rmagro
20th June 2007, 14:00
Oh sorry..

this is the link you posted once..

http://www.qtcentre.org/forum/f-qt-programming-2/t-qtableview-printing-3796.html#10

..should I add something??

If so how to complete it?

Thanka a lot

Roby

jpn
20th June 2007, 14:11
It depends how you want it to be. Maybe drawing it without scaling suits your needs:


// painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
painter.drawPixmap(printer.pageRect().topLeft(), pixmap);

Or alternatively use HTML and QTextDocument as suggested by Jacek at the end of the thread.

rmagro
20th June 2007, 14:23
JPN,

following your suggestion it Prints the entire table ..in the top left corner of the page..

only a problem...

the table is extremely little...I can't even see it ..

How come?

is there a scaling problem?

Roby

jpn
20th June 2007, 14:41
Give it a try with QPrinter::ScreenResolution (http://doc.trolltech.com/4.3/qprinter.html#PrinterMode-enum). Presumably the another option is to scale according to used resolution (http://doc.trolltech.com/4.3/qprinter.html#resolution) in case it doesn't look good enough.

rmagro
20th June 2007, 14:49
Indeed now it look grater but not all the columns fit on the page..

"Presumably the another option is to scale according to used resolution in case it doesn't look good enough"

Can you show me how to do it?

rmagro
20th June 2007, 15:08
I tried the following:

QPrinter printer;
int dpi = printer.resolution();
printer.setResolution(dpi);

but it doesn't change the fact that the table is not entirely printed..

Roby

jpn
20th June 2007, 17:47
int dpi = printer.resolution();
printer.setResolution(dpi);


I'm sorry to say but this does nothing at all. One option is to go with QPainter::scale(). A scale factor of 0.5 makes it half of the original size, 2 makes it double.

rmagro
21st June 2007, 13:52
Many thanks JPN
for your help!

I tried what you suggested but it does not look good yet..

The problem is that if I choose "painter.scale(0.5,0.5); "
it prints the entire table but che characters inside the cells
are not visible because of bad resolution(maybe too small)

Further suggestions?

Thank again

Roby

rmagro
21st June 2007, 14:36
I rolled back to

QPrinter printer(QPrinter::HighResolution);

and I added


printer.setOrientation(QPrinter::Landscape);
painter.scale(4,4);

Now it fits the page...

but hey ...where is table bar titles??

How to get it ??

Thank for your reply

Roberto.

rmagro
21st June 2007, 15:52
JPN,

I tried doing this, but nothing..yet

what is wrong with the following..


// redirect table's painting on a pixmap
QPixmap pixmap(totalWidth, totalHeight);
QPainter::setRedirected(tableWidget->horizontalHeader()->viewport(), &pixmap);
QPaintEvent event(QRect(0, 0, totalWidth, totalHeight));
QApplication::sendEvent(tableWidget->horizontalHeader()->viewport(), &event);
QPainter::restoreRedirected(tableWidget->horizontalHeader()->viewport());

QPixmap pixmap1(totalWidth, totalHeight);
QPainter::setRedirected(tableWidget->viewport(), &pixmap1);
QPaintEvent event1(QRect(0, 1, totalWidth, totalHeight));
QApplication::sendEvent(tableWidget->viewport(), &event1);
QPainter::restoreRedirected(tableWidget->viewport());

// print scaled pixmap
QPainter painter(&printer);
painter.scale(5,5);
painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
painter.drawPixmap(printer.pageRect().topRight(), pixmap1, pixmap1.rect());

Thanks for your patience,

Roby

jpn
21st June 2007, 16:12
Perhaps it would be easier to convert contents of the table to HTML and use QTextDocument::print()?

rmagro
21st June 2007, 16:50
Please provide some examples,

Thank you

Roberto

jpn
21st June 2007, 17:07
What does the table contain? Plain text? Images? Could you provide a screenshot?

rmagro
22nd June 2007, 08:48
Here it is..

Then, the following to show you how I fill in the table:

QTableWidgetItem *item = new QTableWidgetItem;
tableWidget->item(row, 1)->setText(tr("Verifica corretta"));

many thanks..
Roby

rmagro
26th June 2007, 12:22
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

jpn
27th June 2007, 21:06
Here it is..
Alright, so the table contains multi line text and icons. What do you think about something like this?