Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsEffect and sourcePixmap()
Qt 6.12 LTS is out! See what's new in the release blog

QGraphicsEffect and sourcePixmap()

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.5k 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.
  • Y Offline
    Y Offline
    yonnak
    wrote on last edited by
    #1

    Hi,

    I've got some QGraphicsItem on wich I apply a custom QGraphicsEffect with its draw() function modified. To acheive the desired effect I use the sourcePixmap() function like described in the doc.
    All is working well but when I scale my view (zoom In) the QPixmap returned by sourcePixmap() get huge and I get "out of memory" errors and slowness.

    I could not find the source code of the sourcePixmap() function... Is there a way to render only the exposed rect of a QGraphicsItem into a QPixmap?

    Thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you share your code ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Y Offline
        Y Offline
        yonnak
        wrote on last edited by
        #3

        Here is a simplified code that reproduce the problem:

        #include <QApplication>
        #include <QtGui>
        #include <QGraphicsScene>
        #include <QGraphicsView>
        #include <QGraphicsRectItem>
        #include <QGraphicsEffect>
        #include <QPainter>
        #include <QBitmap>
        #include <QDebug>
        
        class MYEffect : public QGraphicsEffect
        {
        public:
            MYEffect() : QGraphicsEffect() {}
            QRectF boundingRectFor(const QRectF& sourceRect) const{
                return sourceRect;
            }
        
        protected:
            void draw(QPainter* painter){
                QPoint offset;
                Qt::CoordinateSystem system = sourceIsPixmap() ? Qt::LogicalCoordinates : Qt::DeviceCoordinates;
                QPixmap pixmap = sourcePixmap(system, &offset, QGraphicsEffect::NoPad);
        
                if (pixmap.isNull())
                    return;
        
                qDebug() << pixmap.size();
        
                painter->save();
                QPainter pixmapPainter(&pixmap);
                pixmapPainter.setRenderHints(painter->renderHints());
                pixmapPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
                QTransform worldTransform = painter->worldTransform();
                worldTransform *= QTransform::fromTranslate(-offset.x(), -offset.y());
                pixmapPainter.setWorldTransform(worldTransform);
                //pixmapPainter.fillRect(sourceBoundingRect(), Qt::blue);
                painter->setWorldTransform(QTransform());
                painter->setOpacity(0.5);
                painter->drawPixmap(offset, pixmap);
                painter->restore();
            }
        };
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            QGraphicsScene scene;
            scene.setSceneRect(0,0,50000,50000);
        
            MYEffect effect;
        
            QGraphicsRectItem rItem;
            rItem.setRect(20000,20000,10000,10000);
            rItem.setBrush(Qt::red);
            rItem.setGraphicsEffect(&effect);
        
            scene.addItem(&rItem);
        
            QGraphicsView view(&scene);
            view.show();
        
            view.scale(2,2);
        
            return app.exec();
        }
        

        The behaviour is the same with the build in QGraphicsOpcityEffect

        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
        • Unsolved