Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to draw a text with drop shadow in the image using QPainter ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RaSMS
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved