Results 1 to 13 of 13

Thread: How to convert QString to QImage

  1. #1
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default How to convert QString to QImage

    Hi,

    How to convert QString to QImage ,give me any suggestions

    Thanks.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to QImage

    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:
    1. Construct a QFont that describes the font with which to render the text.
    2. Construct a QFontMetrics for this QFont, and call QFontMetrics::boundingRect() to obtain the size of the rectangle needed to render your text.
    3. Construct a QImage of this size.
    4. Construct a QPainter to paint on this QImage and set its various parameters (color, QFont, etc.).
    5. Paint the text with QPainter::drawText().
    6. 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.

  3. #3
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to convert QString to QImage

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to QImage

    You can draw on a QImage even in a headless application.

    Cheers,
    _

  5. #5
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to convert QString to QImage

    Hi anda_skoa,


    Thanks for your reply.
    Can yoou provide sample code.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to QImage

    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,
    _

  7. #7
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to convert QString to QImage

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to QImage

    What does it mean "not working"?
    Have you changed your application object to QCoreApplication?

    Cheers,
    _

  9. #9
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to convert QString to QImage

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to QImage

    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,
    _

  11. #11
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to convert QString to QImage

    Hi anda_skoa,

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

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QString to QImage

    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,
    _

  13. #13
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to convert QString to QImage

    Hi anda_skoa,

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

Similar Threads

  1. Convert QVidoFrame to QPixMap / QImage is very slow
    By hasti in forum Qt Programming
    Replies: 4
    Last Post: 22nd September 2014, 16:25
  2. How to convert multi page Magick++ Image to QImage?
    By eadorean in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2013, 18:28
  3. Replies: 3
    Last Post: 20th June 2013, 15:50
  4. How To Convert CGImageRef to QImage
    By nareshqt in forum Qt Programming
    Replies: 0
    Last Post: 23rd June 2008, 08:21
  5. Convert RAW 8 bit pixels into a QImage
    By danielperaza in forum Qt Programming
    Replies: 3
    Last Post: 10th March 2008, 14:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.