简体   繁体   English

Qt更新显示的QImage

[英]Qt updating displayed QImage

I acquire images from a camera by using a SDK which returns the data in an unsigned char array. 我通过使用SDK从相机获取图像,该SDK以未签名的char数组返回数据。 However this SDK doesn't offer functionality to display the data, so I tried to implement this by using Qt 4.8 under Ubuntu 12.04. 但是,此SDK不提供显示数据的功能,因此我尝试通过在Ubuntu 12.04下使用Qt 4.8来实现此功能。

At the moment my code looks like this: 目前,我的代码如下所示:

#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView view(&scene);

    while (..) // condition which breaks after a number of images where grabbed
    {
        // Get the image from the third party SDK into an unsigned char array
        // named pImageBuffer

        QImage image ((unsigned char *) pImageBuffer, 
            GetWidth(), 
            GetHeight(), 
            QImage::Format_Indexed8);

        QVector<QRgb> colorTable(256);
        for(int i=0;i<256;i++)
            colorTable[i] = qRgb(i,i,i);
        image.setColorTable(colorTable);

        QPixmap pixmap = QPixmap::fromImage(image);
        QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
        scene.addItem(item);

        view.show();
        a.exec();
    }
}

This works like expected, the images are displayed properly. 像预期的那样工作,图像正确显示。 However a.exec() blocks the main thread until I close the QtWindow. 但是a.exec()会阻塞主线程,直到我关闭QtWindow。

Is there any easy way to modify this, so the window stays open all the time and just updates the displayed image? 有什么简单的方法可以修改它,从而使窗口始终保持打开状态并仅更新显示的图像? Performance doesn't matter at all at the moment, but I need to keep the code simple. 目前,性能根本不重要,但是我需要保持代码简单。

While a call to QApplication::processEvents will work, it's just a hack and not the best solution. 虽然对QApplication :: processEvents的调用将起作用,但这只是一个hack,而不是最佳的解决方案。

Ideally the image grabber should run on a separate thread as an object derived from QObect. 理想情况下,图像采集器应在单独的线程上作为从QObect派生的对象运行。 This object emits signals of the images that it receives, which are received by an object on the main thread. 该对象发出其接收的图像信号,该信号由主线程上的对象接收。

The receiving object can then set the image on the QGraphicsPixmapItem object. 然后,接收对象可以在QGraphicsPixmapItem对象上设置图像。

Note that the code in the question creates a new QGraphicsPixmapItem for every image that is received from the grabber. 请注意,问题中的代码会为从采集卡接收到的每个图像创建一个新的QGraphicsPixmapItem。 Assuming you're wanting to create an animated image, you should only be creating and adding a single QGraphicsPixmapItem to the scene. 假设您要创建动画图像,则只应创建一个QGraphicsPixmapItem并将其添加到场景中。

Using QThread is very easy and if you've not done it before, I suggest you read this article , which clearly explains what to do, with example code. 使用QThread非常容易,如果您以前没有做过,建议您阅读本文 ,其中清楚地说明了如何使用示例代码进行操作。

class ImageGrabber
{
   Q_OBJECT
public:
   ImageGrabber(QPixmapItem* item) : _item(item)
   {
      connect( &timer, SIGNAL(timeout()), this, SLOT(grabImage()) )
      _timer.start(33); // 33 ms between timeouts.
   }

public slots:
   void grabImage()
   {
      // Update image
      QImage image(...);

      _item->setPixmap( QPixmap::fromImage(image) );
   }

private:
   QPixmapItem* _item;
   QTimer _timer;
};

int main(...)
{
   QApplication a(argc,argv);
   ...
   view.show();
   QGraphicsPixmapItem* pixmapItem = scene.addPixmap(QPixmap());

   ImageGrabber ig(pixmapItem);

   return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM