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

“可以避免使用线程来支持 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(),这应该可以工作。

于 2009-10-27T00:36:46.643 回答