PDA

View Full Version : How to convert QString to QImage



gunturrohith
22nd July 2015, 11:01
Hi,

How to convert QString to QImage ,give me any suggestions

Thanks.

yeye_olive
22nd July 2015, 11:57
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.

gunturrohith
22nd July 2015, 13:36
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.

anda_skoa
22nd July 2015, 14:05
You can draw on a QImage even in a headless application.

Cheers,
_

gunturrohith
22nd July 2015, 14:08
Hi anda_skoa,


Thanks for your reply.
Can yoou provide sample code.

anda_skoa
23rd July 2015, 09:12
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,
_

gunturrohith
24th July 2015, 07:23
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.

anda_skoa
24th July 2015, 08:57
What does it mean "not working"?
Have you changed your application object to QCoreApplication?

Cheers,
_

gunturrohith
24th July 2015, 09:25
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

anda_skoa
25th July 2015, 10:49
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,
_

gunturrohith
27th July 2015, 06:42
Hi anda_skoa,

we are not using Qt-5,we are using Qt-4.7.4

anda_skoa
27th July 2015, 11:34
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,
_

gunturrohith
28th July 2015, 08:34
Hi anda_skoa,

we are getting problem with QPainter.
Can you provide any link or documentation for Xvfb installation or configuration