Viatorus
Viatorus

Reputation: 1903

Qt -> Set Value from Gui->Thread->Qtimer

I have got a special camera class, which should called as often as possible to get the next picture. The gui itself contains some sliders to change settings of the camera parameters and an image output.

Here is a simple structure of my classes:

class CameraWorker : public QObject
{
    Q_OBJECT

public:
    CameraWorker();

signals:
    void imageReady(const QImage &);

private slots:
    void onCamera();
    void setValue(int value);

private:
    Camera camera;
};

class CameraThread : public QThread
{
    Q_OBJECT

public:
    CameraThread();

signals:
    void imageReady(const QImage &);
    void gotValue(int value);

private slots:
    void getImage(const QImage &img);
    void setValue(int value);

private:
    void run();
};

My main window contains:

connect(ui.value, SIGNAL(valueChanged(int)), cameraThread, SLOT(setValue(int)));

My camera thread contains:

void CameraThread::run()
{
    CameraWorker worker;
    QTimer timer;
    connect(&timer, SIGNAL(timeout()), &worker, SLOT(onCamera()));
    connect(&worker, SIGNAL(imageReady(const QImage &)), this, SLOT(getImage(const QImage &)));
    connect(this, SIGNAL(gotValue(int)), &worker, SLOT(setValue(int)));

    timer.start(0);
    exec();
}

void CameraThread::setValue(int value)
{
    emit gotValue(value);
}

And my camera worker contains:

void CameraWorker::setValue(value)
{
    camera.setValue(value);
}

So the problem is, that I must call Signal -> Slot -> Signal -> Slot -> camera.setValue to get one value to the camera, but I have 20 values. Is there any better way to do this?

Upvotes: 0

Views: 407

Answers (1)

Silicomancer
Silicomancer

Reputation: 9166

You may put the 20 values into an copy-by-value object. That way you can pass them quickly with a single signal/slot cascade. Don't forget to register your custom type for usage in queued connections.

You may want to avoid the signal/slot overhead completely. You can write a standard producer/consumer construction using QSemaphore (search for "Semaphores Example" in the documentation).

You also may send a custom event (containing your data) to your thread, which should be faster than using signal/slot.

Upvotes: 1

Related Questions