PDA

View Full Version : Printing problem - solved



waynew
2nd October 2010, 16:53
I'm trying to print an address label. Tried two different methods but neither one is perfect. Method one, using QTextDocument prints all 3 lines perfectly to a laser printer, or to Snagit, but just ejects a blank label from the label printer.

Method two prints each line of text in the same place on the same line each line over writing the other, but it does print on the label printer as well as the other devices.
There must be a simple way to get this right, but I don't see it.
Thanks for any suggestions.



// Method 1

QPrinter *printer = new QPrinter();
printer->setOrientation(QPrinter::Landscape);

QPrintDialog *dialog = new QPrintDialog(printer, this);
if (dialog->exec() != QDialog::Accepted)
{
return;
}

QString nm = "My Name\n";
QString st = "My Address\n";
QString cty = "My City My State My Zipcode";

QString addr = nm + st + cty;

QTextDocument doc;
doc.setPlainText(addr);

// prints ok on the laser but nothing on the label printer
doc.print(printer);

delete dialog;
delete printer;





// Method 2

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

QPrintDialog *dialog = new QPrintDialog(&printer, this);
if (dialog->exec() != QDialog::Accepted)
{
return;
}

QStringList list;
list.append("My Name\n");
list.append("My Address\n");
list.append("My City My State My Zipcode");

QPainter painter(&printer);
painter.setPen(Qt::black);
QRectF textArea(
printer.pageRect().left() +printer.resolution() *0.1,
printer.pageRect().top() +printer.resolution() *0.1,
printer.pageRect().width() +printer.resolution() *3.5,
printer.pageRect().height() +printer.resolution() *1.125);
painter.drawRect(textArea);

// Prints everything on the same line
for (int i = 0; i < list.size(); i++)
{
painter.drawText(textArea, QTextOption::WordWrap | Qt::AlignLeft, list[i]);
}

delete dialog;



Update - I took a different approach and it works fine.


int x = 10;
int y = 20;
for (int i = 0; i < list.size(); i++)
{
painter.drawText(x, y, list[i]);
y = y +15;
}