PDA

View Full Version : Printing a Qstring



harakiri
22nd June 2007, 14:17
hi,

i've got a problem with a Qstring i want to print. the string is very long an contains a table made with <tr>, <th>, <br> and so on.

Printing it with the following configuration leads to a result, in which only one line exists and ends at the end of the page. The tags like <br> are idnored -simply printed-, too.


void THIS::printQString() {

Q_ASSERT(xLabel->text());
QPrintDialog dialog(&printer, this);

if (dialog.exec()) {
QPainter painter(&printer);
QFont font("Courier", 8 );
painter.setPen( Qt::black );
painter.setFont(font);

painter.drawText(10, 10,xLabel->text());
}

}


xLabel->setText(COMPLEXTEXT); does exactely what i want but painter.drawText(10, 10,xLabel->text()); doesn't format the QString in the same way. I want the printing result look exactely the same way as the QLabel xLabel (it fits on a page for sure).

How can this be done?

marcel
22nd June 2007, 20:49
Grab the label into a pixmap and print the damn thing :).
Of course QPainter won't do anything to the text. It doesn't know HTML.

Regards

harakiri
24th June 2007, 11:35
yes, nice idea, thank you.

so far, i've tried the following:


void THIS::printX() {

QPrintDialog dialog(&printer, this);

QPixmap* tempXPixmap = new QPixmap;
tempXPixmap->grabWidget(xLabel);

if (dialog.exec()) {
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = tempXPixmap->size();
size.scale(rect.size(), Qt::KeepAspectRatio);
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
painter.setWindow(tempXPixmap->rect());
painter.drawPixmap(0, 0, *tempXPixmap);
}

}


this results in a segmentation fault..

gdb backtrace:



(gdb) backtrace
#0 0xb61bc8f7 in __divdi3 () from /lib/libgcc_s.so.1
#1 0xb62a8c0a in QSize:: scale () from /usr/lib/libQtCore.so.4
#2 0x080ba2d2 in MainWindow:: printX ()
#3 0x080f0a4d in MainWindow:: qt_metacall ()
#4 0xb6330aa8 in QMetaObject:: activate () from /usr/lib/libQtCore.so.4
#5 0xb6330d2f in QMetaObject:: activate () from /usr/lib/libQtCore.so.4
#6 0xb64bf831 in QAction:: triggered () from /usr/lib/libQtGui.so.4
#7 0xb64c01bf in QAction:: activate () from /usr/lib/libQtGui.so.4
#8 0xb67c6fd4 in ?? () from /usr/lib/libQtGui.so.4
.....


so why doesn't the scale work? i used this method to print other pixmaps, is there a problem with the grabWidget() ?

marcel
24th June 2007, 11:47
Could be because painter->viewport() returns a default rect ( 0, 0, 0, 0). Therefore a division by 0 will occur in scale.

The default rect means that the paint device has an empty size ( 0, 0 ).

See if you can set in QPrinter the output media size. Otherwise use QPainter::setViewport().

The idea is to make sure the paint device has valid rect.

Regards

marcel
24th June 2007, 11:50
OK. Forget that :)!.

grabPixmap is static in QPixmap and returns the grabbed pix.
So you called it wrong there.

Should have been:


QPixmap pix = QPixmap::grabWidget(xLabel);


Regards

harakiri
24th June 2007, 13:29
works fine - thanks marcel :D

wysota
24th June 2007, 13:52
How about:

QTextDocument doc(label->text());
QPrinter printer;
//...
doc.print(&printer);
Of course you have to setup the printer first...