Search code examples
performanceqtqimageqpixmap

QGraphicsPixmapItem::setPixmap() Performance?


Since it seems I need to improve the performance of my code, I wanted to ask, how good is the performance of QGraphicsPixmapItem::setPixmap(*Qimage)? My Image is 1024x1024 px and is updated about every 2.5seconds. But I need it to be updated much faster (4096x every 2.5 seconds at best). Is it possible with QGraphicsPixmapItem::setPixmap(*Qimage)? I am filling every single pixel of QImage directly with an array: array[y*SCENEWIDTH+x] = color.

But with that speed QGraphicsPixmapItem::setPixmap(*Qimage) seems to freeze my GUI. Goal is to display huge ammounts of data incoming in polar coordinates (azimuth per azimuth) (radar video).

Any suggestions? Thank you!


Solution

  • Rather than using QGraphicsPixmapItem and setting the image every time, I suggest creating your own class derived from QGraphicsItem and updating a member QImage. Here's an example which shows a smooth transition of updating a 1024 x 1024 image (note that it uses C++ 11)

    class MyImage : public QGraphicsItem
    {
    public:
    
        MyImage()
            :QGraphicsItem(NULL)
        {
            img = QImage(1024, 1024, QImage::Format_RGB32);
            static int red = 0;
            static int green = 0;
            static int blue = 0;
            img.fill(QColor(red++%255, green++%255, blue++%255));
    
            QTimer* pTimer = new QTimer;
            QObject::connect(pTimer, &QTimer::timeout, [=](){
    
                // C++ 11 connection to a lambda function, with Qt 5 connection syntax
                img.fill(QColor(red++%255, green++%255, blue++%255)); 
                update();
            });
    
            pTimer->start(1000 / 30); // 30 frames per second
        }
    
    private:
        virtual QRectF boundingRect() const
        {
            return QRectF(0, 0, 1024, 1024);
        }
    
        QImage img;
    
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
        {
            painter->drawImage(0, 0, img);
        }
    };
    

    If you instantiate an instance of this class and add it to a QGraphicsScene, you should see a smooth transition of the image being drawn, changing colour from black through to white.