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. QGraphicsItem object and shadow effect behind all other items?
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem object and shadow effect behind all other items?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 597 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.
  • A Offline
    A Offline
    agarny
    wrote on last edited by
    #1

    Hi, I have several QGraphicsItem objects (well, QGraphicsPixmapItem objects to be more precise) and I want them to have a shadow.

    I therefore thought that I would use something like setGraphicsEffect(new QGraphicsDropShadowEffect()) and, indeed, I do get a shadow, but my QGraphicsItem objects can move around, which means that in some cases the shadow of a QGraphicsItem object may be rendered "above" that of another QGraphicsItem object.

    What I would like is for the shadow of all my QGraphicsItem objects to be "behind" everything in my scene. In some way, I wish there was a QGraphicsDropShadowEffect::setZValue() method that I could call, but no such function exists... so, what would be the best way to achieve what I am after?

    Cheers, Alan.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      agarny
      wrote on last edited by
      #2

      Ok, FWIW, I have fixed my problem by creating my own shadow using a QGraphicsPixmapItem object and assigning a Z value to it so that it is always "behind" everything else.

      This obviously means that I had to handle the case where my original QGraphicsPixmapItem object moves, has its pixmap changed, etc. and make sure that its shadow does something similar. At least, now, it all works fine, i.e. no shadows gets "above" a QGraphicsPixmapItem object.

      Just in case, here is the code that I have come up with to get a shadow for a QGraphicsPixmapItem object:

      QPixmap Item::shadow(const QPixmap &pPixmap)
      {
          static const int Depth = sizeof(QRgb);
      
          QPixmap res;
          QString cacheKey = QString::number(pPixmap.cacheKey());
      
          if (!QPixmapCache::find(cacheKey, &res)) {
              QImage shadow = pPixmap.toImage();
      
              for (int j = 0, jMax = shadow.height(); j < jMax; ++j) {
                  uchar *pixels = shadow.scanLine(j);
      
                  for (int i = 0, iMax = shadow.width(); i < iMax; ++i) {
                      QRgb *rgb = reinterpret_cast<QRgb *>(pixels+i*Depth);
      
                      if (*rgb)
                          *rgb = QColor(0, 0, 0, 96).rgba();
                  }
              }
      
              res = QPixmap::fromImage(shadow);
      
              QPixmapCache::insert(cacheKey, res);
          }
      
          return res;
      }
      
      1 Reply Last reply
      0

      • Login

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