Qt slot from thread called more than once [duplicate]

假装没事ソ 提交于 2019-12-13 10:26:19

问题


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

回答1:


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().




回答2:


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




回答3:


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())); 

...
}


来源:https://stackoverflow.com/questions/39370841/qt-slot-from-thread-called-more-than-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!