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;
    while (timer.elapsed() < 5000) ;
    return str + " ready.";

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    connect(&watcher, SIGNAL(paused()), this, SLOT(paused));
    connect(&watcher, SIGNAL(resumed()), this, SLOT(resumed()));
    connect(&watcher, SIGNAL(resultReadyAt(int)), this, SLOT(resultReadAt(int)));

    void MainWindow::start()
    connect(ui->pushButton, SIGNAL(clicked()), &watcher, SLOT(cancel()));

    connect(ui->pushButton_2, SIGNAL(clicked()), &watcher, SLOT(togglePaused()));

    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);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(start()));

    connect(ui->pushButton_2, SIGNAL(clicked()), &watcher, SLOT(togglePause()));

    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.
    Item 2 ready.
    Item 3 ready.

    and so on, but I miss paused.

    Thank you for any hint


    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

  • Please put your code between '@' tags, it's unreadable.

  • 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)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.