June 27, 2011

Mark2 Mark2
Lab Rat
1 posts

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:

  1. QString DoWork(const QString& str)
  2. {
  3.   QTime timer;
  4.   timer.start();
  5.   while (timer.elapsed() < 5000) ;
  6.   return str + " ready.";
  7. }
  8.  
  9.  
  10. MainWindow::MainWindow(QWidget *parent) :
  11.     QMainWindow(parent),
  12.     ui(new Ui::MainWindow)
  13. {
  14.     ui->setupUi(this);
  15.     connect(&watcher, SIGNAL(paused()), this, SLOT(paused));
  16.     connect(&watcher, SIGNAL(resumed()), this, SLOT(resumed()));
  17.     connect(&watcher, SIGNAL(resultReadyAt(int)), this, SLOT(resultReadAt(int)));
  18.     finished();
  19. }
  20.  
  21.  
  22. void MainWindow::start()
  23. {
  24.   ui->pushButton->disconnect();
  25.   connect(ui->pushButton, SIGNAL(clicked()), &watcher, SLOT(cancel()));
  26.   ui->pushButton->setText("Abbruch");
  27.  
  28.   ui->pushButton_2->disconnect();
  29.   connect(ui->pushButton_2, SIGNAL(clicked()), &watcher, SLOT(togglePaused()));
  30.     ui->pushButton_2->setText("Pause");
  31.   ui->pushButton_2->setEnabled(true);
  32.  
  33.   ToDo.clear();
  34.   for (int i=0; i < 10; i++)
  35.     ToDo << "Item " + QString::number(i);
  36.   watcher.setFuture(QtConcurrent::mapped(ToDo, DoWork));
  37. }
  38.  
  39. void MainWindow::finished()
  40. {
  41.   new QListWidgetItem("finished", ui->listWidget);
  42.   ui->pushButton->disconnect();
  43.   connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(start()));
  44.   ui->pushButton->setText("Start");
  45.  
  46.   ui->pushButton_2->disconnect();
  47.   connect(ui->pushButton_2, SIGNAL(clicked()), &watcher, SLOT(togglePause()));
  48.   ui->pushButton_2->setText("Pause");
  49.   ui->pushButton_2->setEnabled(false);
  50. }
  51.  
  52.  
  53. void MainWindow::paused()
  54. {
  55.   new QListWidgetItem("paused", ui->listWidget);
  56. }
  57.  
  58.  
  59. void MainWindow::resumed()
  60. {
  61.   new QListWidgetItem("resumed", ui->listWidget);
  62. }
  63.  
  64. void MainWindow::resultReadAt(int i)
  65. {
  66.   new QListWidgetItem(watcher.resultAt(i), ui->listWidget);
  67. }

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

2 replies

June 27, 2011

HuXiKa HuXiKa
Lab Rat
79 posts

Please put your code between ‘@’ tags, it’s unreadable.

 Signature 

If you can find faults of spelling in the text above, you can keep them.

June 27, 2011

Mark2 Mark2
Lab Rat
1 posts

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)

 
  ‹‹ Memory scanner      Convert from int to hex ››

You must log in to post a reply. Not a member yet? Register here!