tags:

views:

24

answers:

2

I am developing image aquisition software. I want to run display function in a separate thread (to ensure live view for easy image capture). For now I am subclassing QObject, creating DisplayObject and using QThread::moveToThread function. Problem is that DisplayObject has to interact with GUI (images need to be displayed in QGraphicsView objects). What is the best (and the most elegant) way to do this?

My current solution:

class DisplayObject : public QObject
{
    Q_OBJECT
    public:
        inline void addDetectorSystem(cci::PGR* system);
        inline void connectGui(ImageAquisition* gui);

    public slots:
        void display();

    private:
        cci::PGR* camSystem;
        ImageAquisition* guiClass;
};

DisplayObject is initialized as below:

  DisplayObject Displayer;
        Displayer.addDetectorSystem(PGRSystem);
        Displayer.connectGui(this);
        QThread thread;
        Displayer.moveToThread(&thread);
        connect(this, SIGNAL(display()), &Displayer, SLOT(display()));
        thread.start();

I forgot to admit, that it doesn't work...

A: 

Generally the GUI is the main thread and any work is done in background threads.
This is because the main thread needs to handle mouse, redraw, iconify events etc

The nice thing about Qt is that you can send slots/signals between threads.

See threading in qt for an overview.

There is also a good new advanced Qt book that covers threadign in detail

Martin Beckett
A: 

You can use QImage in non-UI threads. This would allow you to do your capture/drawing to an image. Once an image is ready to be displayed, you could then emit a signal with the image. Your UI element should have a slot connected to that signal which takes the given image and refreshes itself to draw that image.

By using signals and slots, the data should be transferred in a thread-safe manner. By using QImages in the other threads, you can draw, and the main UI is only responsible for updating the created images, instead of spending a lot of time processing them.

Caleb Huitt - cjhuitt