Rotate text 180 degrees on QT5
-
Hello everyone.
I just want to rotate 180 (upside down) a specific text line on my application. I tried: But I only get a blank area.QStringList printers = QPrinterInfo::availablePrinterNames(); QPrinterInfo printerInfo = QPrinterInfo::printerInfo(printers.at(0)); QPrinter *printer = new QPrinter(printerInfo,QPrinter::HighResolution); printer->setPrinterName("default printer"); printer->setNumCopies(numcopies); printer->setPaperSize(QSizeF(2, 4), QPrinter::Inch); // Current label size 2 inch length by 4.00 inch wide. printer->setPageMargins(QMarginsF(0,0,0,0)); QPainter painter(this); //instruction to paint this object called painter painter.begin(printer); //initialize painter object //barcode font setup and for text font setup and size QFont fonto39 =QFont("IDAHC39M Code 39 Barcode",6,QFont::Normal); //original size in 8 QFont fonto128 =QFont("Code 128",8,QFont::Light); QFont carlito =QFont("Carlito",9,QFont::Black); //original is 13 QFont carlito2 =QFont("Carlito",13,QFont::Black); //original is 13 //fonto128.setLetterSpacing(QFont::AbsoluteSpacing,5.0); //separadito // setup fpr code39 (barcode) size and margins (X, Y) values X close to 0 moves to left Y closes to 0 moves upwards QString myaxterix = ("*"); QString numpartecode = ui->lineEdit->text(); QString concatenado = (myaxterix + numpartecode + myaxterix); painter.setFont(fonto39); painter.drawText(5000,1500, concatenado ); //best is 1800,500 //setup for text alignment on label using same (X,Y) values to properly adjust as desired QString mybarcode = (ui->lineEdit->text()); painter.setFont(carlito); painter.drawText(6000,1000, mybarcode ); //280,380 (best is 25,380) (3000, 380 previous) //I want to rotate this line 180 (upside down) QString mybarcode2 = (ui->lineEdit->text()); painter.setFont(carlito2); painter.rotate(180); painter.save(); painter.drawText(100,4600, mybarcode2 ); painter.restore(); painter.end(); //close painter }
-
rotate()
does not rotate painting operations. It rotates coordinate system of the painter. You can think about it as you have a piece of paper and rotate it around its upper left corner (the 0x0 point). It doesn't rotate around a word you wrote on it.So to rotate the text around some custom point you need to do some geometry calculations.
drawText(100,4600,...
draws the text in a box which has bottom left corner at 100x4600. If you want to rotate it around that point you can do it like this:painter.translate(100, 4600); // move "page" so that 100x4600 becomes the upper left corner painter.rotate(180.0f); // rotate "page" around that point painter.translate(-100, -4600); // move back painter.drawText(100, 4600, mybarcode2); // draw at rotated coordinates
Since the last two operations cancel each other you can shorten it to:
painter.translate(100, 4600); painter.rotate(180.0f); painter.drawText(0, 0, mybarcode2);
As I said, this will rotate the text around its bottom left corner. If you want to rotate around its center you need to calculate the size of the box the text occupies and add the offset to the center of that box to the translates above.
Also note that you have your
save()
call in the wrong place. Call it before you start to modify the coord system, not after, because you'll restore to the already modified coord system. -
PERFECT !
Worked great.
Many Thanks @Chris-Kawathis is the code that worked once I adjusted the new translate as you said:
painter.setFont(carlito2); painter.translate(100, 4600); // move "page" so that 100x4600 becomes the upper left corner painter.rotate(180.0f); // rotate "page" around that point painter.translate(-1500, -4500); // move back and adjust to offset new coordinates to print rotated 180 text. painter.drawText(100, 4600, mybarcode6 ); //print at new coordinates rotated 180 text.