質問
わかりました、今夜はこの問題が発生しています:
[...]
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。
解決
スレッド化を調べる必要があります。計算はUIをロックします。
他のヒント
まあ、「Qt4を使用したC ++のデザインパターンの概要」で、彼らは言う
&quot;の使用を避けることが可能です Qtイベントループを支持するスレッド QTimersとの組み合わせ&quot;
しかし、私は試したことがない:)
実際、試したところです-
追加:
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()に引数を渡さない限り、これは機能します。
所属していません StackOverflow