How to rotate draw text only in QPainter
-
How to rotate text only in QPainter.
My code :
void MainWindow::on_pushButton_clicked() { QPrinter *printer=new QPrinter(QPrinter::HighResolution); printer->setPrinterName("POSTEK C18/500+ #2"); printer->setPaperSize(QSizeF(3.35, 0.39), QPrinter::Inch); QImage imge("E:/QT Offline/Qt5.12.12/Barcode/build-Barcode- Desktop_Qt_5_12_12_MinGW_64_bit-Debug/img1.png"); QPainter painter1; painter1.begin(printer); painter1.setFont(QFont("times",40 , QFont::Bold)); double xscale = printer->pageRect().width() / double(imge.width()); double yscale = printer->pageRect().height() / double(imge.height()); double scale = qMin(xscale, yscale); painter1.translate(printer->paperRect().x() + printer->pageRect().width() / 2, printer->paperRect().y() + printer->pageRect().height() / 2); painter1.scale(scale, scale); QFont font = painter1.font() ; QString str = "MENU"; QString str2= "EXIT"; painter1.drawText(-2800,-330 ,str); painter1.drawText(+2800,-330 ,str2); painter1.drawImage(-1000 ,-280, imge); painter1.end(); }
My output is,
How to set up, I want to set up as per below image.
Thanks in advance..
-
@Ramkumar-Mohan
My guess would be callQPainter::rotate()
before thedrawText
, undo/restore beforedrawImage()
.
Plenty of web examples from GoogleQPainter rotate text
, why not try them first and ask here if you get stuck? -
@Ramkumar-Mohan
And what did you do about theundo/restore before
drawImage()
.Please don't tell me you did nothing.
So, did you compare your code against the examples you got from Google
QPainter rotate text
? Are you saying those examples do not work? Or what are you asking us? -
Now , my code is,
for (int i=0; i<1;++i ) { QThread::msleep(1000); QImage imge("E:/QT Offline/Qt5.12.12/Barcode/build-Barcode- Desktop_Qt_5_12_12_MinGW_64_bit-Debug/img1.png"); QPainter painter1; painter1.begin(printer); painter1.setFont(QFont("times",40 , QFont::Bold)); double xscale = printer->pageRect().width() / double(imge.width()); double yscale = printer->pageRect().height() / double(imge.height()); double scale = qMin(xscale, yscale); painter1.translate(printer->paperRect().x() + printer->pageRect().width() / 2, printer->paperRect().y() + printer->pageRect().height() / 2); painter1.scale(scale, scale); QFont font = painter1.font() ; QString str = "MENU"; QString str2 = "EXIT"; painter1.save(); painter1.rotate(-90); painter1.drawText(-2500,-150,str); painter1.drawText(+2800,-150 ,str2); painter1.restore(); painter1.drawImage(-1000 ,-280, imge); painter1.end(); }
but text not printed ,
I don't know where i'm doing wrong.
Thanks,
-
@Ramkumar-Mohan
My last word on this, since I have never done it. After rotation, are you sure the (x, y) coordinates fordrawText()
don't need altering? Are you now drawing, say, outside the visible area? -
rotate
does not rotate the following draw operation. It rotates the coordinate space of the painter i.e. the "canvas" on which you draw. It is also not rotated around arbitrary center of the following operation's argument (like text or image). It is always rotated around the (0,0) point in that space.
Since you're doing a translation to the center the rotation axis is the center of the page.So
rotate(-90)
basically rotates your "page" around the center of the page. If you then draw at-2500,-150
it's out of the page (somewhere below the bottom border I'm guessing).What you need to do is first move the coordinate system so that the center is where the center of the text should be (use
translate
for that). Next, rotate the canvas usingrotate
. After that you can translate back and draw the text at (0,0) or pass the back translation to the drawing function directly.See this thread for example of such transformation.
Btw. Why do you have
QThread::msleep(1000);
in a loop? That will freeze your UI and is not a good experience. -
@Ramkumar-Mohan said in How to rotate draw text only in QPainter:
How to rotate text only in QPainter.
My code :
void MainWindow::on_pushButton_clicked()
{
QPrinter *printer=new QPrinter(QPrinter::HighResolution);
printer->setPrinterName("POSTEK C18/500+ #2");
printer->setPaperSize(QSizeF(3.35, 0.39), QPrinter::Inch);QImage imge("E:/QT Offline/Qt5.12.12/Barcode/build-Barcode- Desktop_Qt_5_12_12_MinGW_64_bit-Debug/img1.png"); QPainter painter1; painter1.begin(printer); painter1.setFont(QFont("times",40 , QFont::Bold)); double xscale = printer->pageRect().width() / double(imge.width()); double yscale = printer->pageRect().height() / double(imge.height()); double scale = qMin(xscale, yscale); painter1.translate(printer->paperRect().x() + printer->pageRect().width() / 2, printer->paperRect().y() + printer->pageRect().height() / 2); painter1.scale(scale, scale); QFont font = painter1.font() ; QString str = "MENU"; QString str2= "EXIT"; painter1.drawText(-2800,-330 ,str); painter1.drawText(+2800,-330 ,str2); painter1.drawImage(-1000 ,-280, imge); painter1.end();
}
My output is,
How to set up, I want to set up as per below image.
It looks like you're trying to rotate text in your QPainter code, but it's not currently working as you expect. One way to rotate text in QPainter is to use the rotate() function. This function takes an angle in degrees as its argument and rotates the painter's coordinate system by that amount. For example, if you want to rotate your text by 90 degrees, you would use the following code:
painter1.rotate(90);
painter1.drawText(-2800,-330 ,str);
painter1.rotate(-90);You can also use the setTransform() function to set the transformation matrix for the painter, which allows you to rotate, scale, and translate the painter's coordinate system all at once.
Another thing you can try is use QGraphicsTextItem and set the rotate property of it.
QGraphicsTextItem* text = new QGraphicsTextItem(str);
text->setRotation(90);You can also try to use QTextOption to set the rotation of text.
QTextOption option;
option.setUseDesignMetrics(true);
option.setTextDirection(Qt::RightToLeft);
option.setAlignment(Qt::AlignRight);
painter1.drawText(QRectF(0,0,100,100),str,option);It's hard to say exactly what's causing the problem without seeing the rest of your code, but hopefully, these suggestions will help you get the rotation working as you expect.