Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFutureWatcher, signal paused()
Qt 6.11 is out! See what's new in the release blog

QFutureWatcher, signal paused()

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark2
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HuXiKa
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mark2
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved