문제
좋아요, 오늘 밤이 문제가 있습니다.
[...]
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)
{
}
}
[...]
계산 () 프로 시저를 언제든지 멈출 수 있도록하려고하지만 시작 직후에 컨트롤이 느슨해지면 정지를 누를 수 없습니다. 물론 내 향후 계획에서 계산 ()은 실제 무언가 (예 : 열 전달 시뮬레이션)를 "계산"할 것입니다.
제안 해주셔서 감사합니다. 피.
해결책
스레딩을 조사해야합니다. 계산은 UI를 잠급니다.
다른 팁
글쎄, "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()));
}
인수를 계산 ()로 전달하지 않는 한 작동해야합니다.
제휴하지 않습니다 StackOverflow