QFutureWatcher, signal paused()
-
Hi there,
I tried to use Qtoncurrent::mapped (Qt4.7.3) and want to offer a pause/resume button (pushbutton_2).
Everything works fine, except the paused slot. When I click pushbutton_2 the first time, the calculation is interrupted, but I didn't get a paused signal from the QFutureWatcher. Clicking a second time, I get a resumed signal.
Did I understand something wrong or is my code wrong?
Here some fragments of my code:
@
QString DoWork(const QString& str)
{
QTime timer;
timer.start();
while (timer.elapsed() < 5000) ;
return str + " ready.";
}MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&watcher, SIGNAL(paused()), this, SLOT(paused));
connect(&watcher, SIGNAL(resumed()), this, SLOT(resumed()));
connect(&watcher, SIGNAL(resultReadyAt(int)), this, SLOT(resultReadAt(int)));
finished();
}void MainWindow::start()
{
ui->pushButton->disconnect();
connect(ui->pushButton, SIGNAL(clicked()), &watcher, SLOT(cancel()));
ui->pushButton->setText("Abbruch");ui->pushButton_2->disconnect();
connect(ui->pushButton_2, SIGNAL(clicked()), &watcher, SLOT(togglePaused()));
ui->pushButton_2->setText("Pause");
ui->pushButton_2->setEnabled(true);ToDo.clear();
for (int i=0; i < 10; i++)
ToDo << "Item " + QString::number(i);
watcher.setFuture(QtConcurrent::mapped(ToDo, DoWork));
}void MainWindow::finished()
{
new QListWidgetItem("finished", ui->listWidget);
ui->pushButton->disconnect();
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(start()));
ui->pushButton->setText("Start");ui->pushButton_2->disconnect();
connect(ui->pushButton_2, SIGNAL(clicked()), &watcher, SLOT(togglePause()));
ui->pushButton_2->setText("Pause");
ui->pushButton_2->setEnabled(false);
}void MainWindow::paused()
{
new QListWidgetItem("paused", ui->listWidget);
}void MainWindow::resumed()
{
new QListWidgetItem("resumed", ui->listWidget);
}void MainWindow::resultReadAt(int i)
{
new QListWidgetItem(watcher.resultAt(i), ui->listWidget);
}
@The output from listWidget is e.g.:
Item 0 ready.
Item 1 ready.
resumed
Item 2 ready.
Item 3 ready.and so on, but I miss paused.
Thank you for any hint
Mark
Edit: please use @ tags around your code sections. If you want to modify your post, use the Edit option, don't just re-post the edited version. I have removed a re-post with the code from this topic; Andre
-
UUps,
I wrote some slots not correct, so they didn't exist of course.
Now I fixed it, and I get the paused signal. But now I'm totally confused, because it is very late. My output is now:
Action: Click Start and wait some seconds.
Output: Item 0 ready. (ok), Item 1 ready. (ok)Action: pushbutton_2(Pause) clicked first time
Output: no output????Action: wait some secs.
Output: no output, calculation seems to be stopped (ok)Action: pushbutton_2(Pause) clicked second time
Output: resumed (ok), paused (why now?)Action: wait some secs.
Output: Item 2 ready, Item 3 ready (ok, calculation is running)