好的,所以今晚我遇到了这个问题:

[...]   

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 ++设计模式简介”中他们说那个

  

“可以避免使用   线程支持Qt事件循环   与QTimers结合使用“

但我从未尝试过:)

实际上,我刚试过 -

添加:

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(),这就应该有效。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top