MelMed
MelMed

Reputation: 255

Linking a function to a timer with Qt

I have a function whose declaration is above:

double image_function(double SUM, double AVR, double &Value)

I have read that SIGNALS and SLOTS must have the same arguments, how is it possible to adjust that condition when applyinh a timer to my function as follow:

connect(timer, SIGNAL(timeout()), this, SLOT(image_function()));
timer->start(0);    

Upvotes: 1

Views: 4402

Answers (1)

user362638
user362638

Reputation:

That's not possible. Your function needs 3 parameters, you have to give them. How could the timer know anything about your function's parameters?

Create a slot function (without any parameters) for the timer's timeout. From there call the image_function with parameters you want.

Let's say your class is a mainwindow. You need to declare the slot for the QTimer's timeout signal:

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    ...

private slots:
    void timer_image_function();

};

Then in the .cpp, you somewhere create a QTimer and connect its signal to this new slot:

connect(timer, SIGNAL(timeout()), this, SLOT(timer_image_function()));
timer->start(0);  

And of course, you need to implement the slot function, which actually calls the image_function:

void MainWindow::timer_image_function()
{
    double result = image_function(SUM, AVR, Value);
}

Upvotes: 4

Related Questions