QGraphicsPixmapItem::setPixmap() Performance?

痴心易碎 提交于 2019-12-11 10:18:54

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/33781485/qgraphicspixmapitemsetpixmap-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!