How to draw a text with drop shadow in the image using QPainter ?



  • Hello everyone.,

    In my program i have open the image and draw the image in the pixmap and I also want to draw the text with drop shadow effect in the image can somebody please help me to draw the text with drop shadow effect using the drawText method or other options in the QPainter class.

    I did not add the text to any widgets.,
    I want to add the text bottom right corner of the image ( like add copyright information to the image ) with drop shadow effect.

    I have add the text like this

    bq. aPainter.drawText( aRect, Qt::AlignRight , inString );

    aPainter draw the text in the given aRect location without any issues. But i want to add the dropShadow effect to the inString.

    Anybody help me.

    Thanks in advance.


  • Moderators

    Hi, welcome to devnet.

    The easiest way is to draw the text multiple times with an offset, eg.
    @
    aPainter.setPen(Qt::gray);
    aPainter.drawText(aRect.adjusted(0,1,1,0), Qt::AlignRight, inString);
    aPainter.setPen(Qt::black);
    aPainter.drawText(aRect, Qt::AlignRight, inString);
    @

    You can control the direction of the shadow with parameters of adjusted().
    If you want a smoother shadow you can draw multiple times with different offsets eg.
    @
    aPainter.setPen(QColor(Qt::gray).lighter(130));
    aPainter.drawText(aRect.adjusted(0,2,2,0), Qt::AlignRight, inString);
    aPainter.setPen(Qt::gray);
    aPainter.drawText(aRect.adjusted(0,1,1,0), Qt::AlignRight, inString);
    aPainter.setPen(Qt::black);
    aPainter.drawText(aRect, Qt::AlignRight, inString);
    @

    Of course if you need to do it multiple times it's a good idea to wrap that into parametrized function.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.