-1

I have created a class CalculationManager which has a public slot process() emitting a signal finished().

void CalculationManager::process()
{
    cout << "calc FFT: process()" << endl;
    ...
    emit finished();
}

This is used with a QThread from the gui (calculationManager is a QScopedPointer)

void MainWindow::on_pushButtonStartFFT_clicked()
{
    cout << "on_pushButtonStartFFT_clicked()" << endl;

    ...
    calculationManager->moveToThread(thread);

    connect(thread, SIGNAL (started()), calculationManager.data(), SLOT (process()));
    connect(calculationManager.data(), SIGNAL (finished()), thread, SLOT (quit()));
    connect(calculationManager.data(), SIGNAL (finished()), this, SLOT (getResultsAndPlot()));

    cout << "FFT thread started." << endl;
    thread->start();
}

with a slot for the plots

void MainWindow::getResultsAndPlot()
{
    fftAction doShift = static_cast<fftAction>(calculationManager->shiftBeforeFFT());

    updatePlotData(ui->qplot1, calculationManager->data(doShift) );
    cout << "update plots" << endl;
}

At startup of the gui the function is called. and the output is as expected:

on_pushButtonStartFFT_clicked()
FFT thread started.
calc FFT: process()
update plots

however every further click on the buttons calls process multiple times and getResultsAndPlot() even more often. I have not clue why this happens. How can I debug or solve this ?

on_pushButtonStartFFT_clicked()
FFT thread started.
calc FFT: process()
calc FFT: process()
update plots
update plots
update plots
update plots
on_pushButtonStartFFT_clicked()
FFT thread started.
calc FFT: process()
calc FFT: process()
calc FFT: process()
update plots
update plots
update plots
update plots
update plots
update plots
update plots
update plots
update plots
Matthias Pospiech
  • 3,130
  • 18
  • 55
  • 76
  • if you really want to connect the signals and slots from inside the slot's scope you need to pass `Qt::UniqueConnection` to `connect` like: `connect(thread, SIGNAL (started()), calculationManager.data(), SLOT (process()), Qt::UniqueConnection);` – tobilocker Sep 07 '16 at 13:42
  • 4
    http://stackoverflow.com/questions/3530590/qt-signals-and-slot-connected-twice-what-happens (duplicate ?) – HazemGomaa Sep 07 '16 at 18:30

3 Answers3

4

Every time you click the button, you create this connections

connect(thread, SIGNAL (started()), calculationManager.data(), SLOT (process()));
connect(calculationManager.data(), SIGNAL (finished()), thread, SLOT (quit()));
connect(calculationManager.data(), SIGNAL (finished()), this, SLOT (getResultsAndPlot()));

So, after first click there is one connection and slot process() called once. After second click there are two connections and this slot called twice. And so on. Just make this connections outside slot on_pushButtonStartFFT_clicked().

Evgeny
  • 3,910
  • 2
  • 20
  • 37
1

Doing all connections withint the pushbotton slot was the reason. The connection is thus done multiple times.

void MainWindow::on_pushButtonStartFFT_clicked()
{
    ...
    connect(thread, SIGNAL (started()), calculationManager.data(), SLOT (process()));
}

Once this is moved to the gui constructor everything works fine

Matthias Pospiech
  • 3,130
  • 18
  • 55
  • 76
0

Agree with @Matthias Pospiech. One option you have if you want to use connect on button click is to use disconnect before connect, as per http://doc.qt.io/qt-4.8/qobject.html#disconnect ... This way you ensure that your signal is connected only one time ... for example,

void MainWindow::on_pushButtonStartFFT_clicked()
{
... 

 disconnect(thread, SIGNAL (started()), calculationManager.data(), SLOT(process()));
 connect(thread, SIGNAL (started()), calculationManager.data(), SLOT(process())); 

...
}
HazemGomaa
  • 1,620
  • 2
  • 14
  • 21