Hi,
How to convert QString to QImage ,give me any suggestions
Thanks.
Hi,
How to convert QString to QImage ,give me any suggestions
Thanks.
I will assume that what you want is to render a piece of text and get the result as a QImage. Here are the steps towards achieving this goal:
- Construct a QFont that describes the font with which to render the text.
- Construct a QFontMetrics for this QFont, and call QFontMetrics::boundingRect() to obtain the size of the rectangle needed to render your text.
- Construct a QImage of this size.
- Construct a QPainter to paint on this QImage and set its various parameters (color, QFont, etc.).
- Paint the text with QPainter::drawText().
- After the QPainter has been destroyed, the text should be rendered on the QImage.
Read the documentation of these various classes and methods to understand how they work.
Hi yeye_olive,
Thanks for your reply.
our actual requirement is convert QString to monochrome bmp.
we are able to do this with following code.
QString filename = "sample.bmp";
QString format = "bmp";
QBitmap bitmap(384,60);
bitmap.fill(Qt::white);
QPainter painter(&bitmap);
QFont serifFont("Times", 18, QFont::Bold);
painter.setFont(serifFont);
painter.setPen(Qt::black);
QStaticText strText(txtStr);
strText.setTextFormat(Qt::RichText);
painter.drawStaticText(10,0,strText);
painter.save();
bitmap.save(filename,format.toAscii());
painter.restore();
Here we are using Gui classes like QBitmap and QPainter and all.
Now we are doing this application for device(with out LCD)
If it is possible,conversion from QString to monochrome bmp with out GUI classes.
can you guide me and provide sample code.
You can draw on a QImage even in a headless application.
Cheers,
_
Hi anda_skoa,
Thanks for your reply.
Can yoou provide sample code.
It is the same code you already have, just using a QImage instead of QBitmap.
QPainter works on any QPaintDevice, QBitmap and QImage are two implementations of that interface.
Cheers,
_
Hi anda_skoa,
I tried this one with the following code.
QString filename = "sample.bmp";
QString format = "bmp";
QImage img(384,60,QImage::format_MonoLSB);
img.fill(Qt::white);
QPainter painter(&img);
QFont serifFont("Times", 18, QFont::Bold);
painter.setFont(serifFont);
painter.setPen(Qt::black);
QStaticText strText(txtStr);
strText.setTextFormat(Qt::RichText);
painter.drawStaticText(10,0,strText);
painter.save();
img.save(filename,format.toAscii());
painter.restore();
I used QImage in place of QBitmap .But still it is not working in device.
I think QPainter is gui class so that it is not working.
so can you provide another alternative solution.
What does it mean "not working"?
Have you changed your application object to QCoreApplication?
Cheers,
_
Hi anda_skoa,
I changed my application object to QCoreApplication.I didn't get any error in compile time,
but i am getting error in run time like this
QScreenLinuxFb::connect: No such file or directory
Error opening framebuffer device /dev/fb0
QScreenLinuxFb::connect: No such file or directory
Error opening framebuffer device /dev/fb0
QScreenLinuxFb::connect: No such file or directory
Error opening framebuffer device /dev/fb0
QScreenLinuxFb::connect: No such file or directory
Error opening framebuffer device /dev/fb0
our device don't have any LCD .Can you provide any solution
Very strange, the QCoreApplication does not have code to load any QPA plugin, only QGuiApplication does.
Still, you could try providing the qminimal QPA plugin and using the -platform commandline switch to load it explicitly.
Cheers,
_
Hi anda_skoa,
we are not using Qt-5,we are using Qt-4.7.4
I see.
You'll have to check where the request for the framebuffer connection comes from, QImage quite certainly doesn't need it.
Alternatively you could look into providing a virtual framebuffer that the application would then work with, or a virtual X server, e.g. Xvfb
Cheers,
_
Hi anda_skoa,
we are getting problem with QPainter.
Can you provide any link or documentation for Xvfb installation or configuration
Bookmarks