0

さて、今夜はこの問題に直面しています:

[...]   

connect(startButton, SIGNAL(clicked()), this, SLOT(startCalculation()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stopCalculation()));

[...]

void MainWindow::startCalculation()
{
   qDebug() << "hello";
   this->startButton->setDisabled(true);
   this->stopButton->setEnabled(true);
   this->calcStatus = true;
   this->calculate();
}

void MainWindow::stopCalculation()
{
    this->startButton->setEnabled(true);
    this->stopButton->setDisabled(true);
    this->calcStatus = false;
}


void MainWindow::calculate()
{
   qDebug() << "hello";
   while(this->calcStatus)
   {
   }
}
[...]

calculate() プロシージャをいつでも停止できるようにしようとしていますが、開始直後に制御できなくなり、STOP を押すことができません。もちろん、私の将来の計画では、calculate() は実際のもの (熱伝達シミュレーションなど) を「計算」します。

提案をありがとう。P.

4

2 に答える 2

0

スレッドを調べる必要があります。計算はUIをロックします。

于 2009-10-27T00:14:39.117 に答える
0

「Qt4 を使用した C++ でのデザイン パターンの紹介」では、次のように述べています。

「QTimersと組み合わせたQtイベントループを優先して、スレッドの使用を避けることができます」

しかし、私はそれを試したことはありません:)

実際、私はちょうど試してみました -

追加:

QTimer      *Timer;

MainWindow クラス ヘッダーと MainWindow コンストラクターに次を追加します。

Timer = new QTimer(this);

次に、calculate() を関数からシグナルに変更し、次のように変更します。

void MainWindow::startCalculation()
{
    qDebug() << "hello";
    this->startButton->setDisabled(true);
    this->stopButton->setEnabled(true);
    this->calcStatus = true;
    connect(Timer, SIGNAL(timeout()), this, SLOT(calculate()));
    Timer->start(0);
}

void MainWindow::stopCalculation()
{
    this->startButton->setEnabled(true);
    this->stopButton->setDisabled(true);
    this->calcStatus = false;
    Timer->stop();
    Timer->disconnect(this,SLOT(calculate()));
} 

これは、calculate() に引数を渡さない限り機能するはずです。

于 2009-10-27T00:36:46.643 に答える