C++ Qt C++;限制应用程序可用的系统资源
我正在开发一个GUI应用程序,它使用一个选项卡小部件,并且有几个选项卡。我有一个标签,上面有一张桌子。我创建了一个方法,每5秒刷新一次表。这是我的密码:C++ Qt C++;限制应用程序可用的系统资源,c++,qt,resources,C++,Qt,Resources,我正在开发一个GUI应用程序,它使用一个选项卡小部件,并且有几个选项卡。我有一个标签,上面有一张桌子。我创建了一个方法,每5秒刷新一次表。这是我的密码: void MainWindow::delay(int seconds) { QTime dieTime = QTime::currentTime().addSecs(seconds); while( QTime::currentTime() < dieTime ) QCoreApplication::pro
void MainWindow::delay(int seconds)
{
QTime dieTime = QTime::currentTime().addSecs(seconds);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void MainWindow::on_tabWidget_currentChanged(int inx)
{
if (inx == 3)
{
while (ui->tabWidget->currentIndex() == 3)
{
delay(5);
refreshTable();
}
}
}
void主窗口::延迟(整数秒)
{
QTime dieTime=QTime::currentTime().addSecs(秒);
while(QTime::currentTime()tabWidget->currentIndex()==3)
{
延误(5);
刷新表();
}
}
}
我遇到的问题是,每当while循环运行时,我大约30%的CPU都会被耗尽。基本上,应用程序会说“我们到了吗?我们到了吗?我们到了吗?”这似乎占用了CPU
有没有办法限制系统资源,或者有没有办法阻止它占用大量CPU?感谢bluebob为我指明了正确的方向。以下是我的解决方案:
QTimer *timer;
void MainWindow::handleTableRefresh()
{
if (ui->tabWidget->currentIndex() == 3)
{
refreshTable();
}
else
{
disconnect(timer, SIGNAL(timeout()), this, SLOT(handleTableRefresh()));
timer->stop();
}
}
void MainWindow::on_tabWidget_currentChanged(int inx)
{
if (inx == 3)
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(handleTableRefresh()));
timer->start(5000);
}
}
感谢蓝铃草为我指明了正确的方向。以下是我的解决方案:
QTimer *timer;
void MainWindow::handleTableRefresh()
{
if (ui->tabWidget->currentIndex() == 3)
{
refreshTable();
}
else
{
disconnect(timer, SIGNAL(timeout()), this, SLOT(handleTableRefresh()));
timer->stop();
}
}
void MainWindow::on_tabWidget_currentChanged(int inx)
{
if (inx == 3)
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(handleTableRefresh()));
timer->start(5000);
}
}
你所做的就是所谓的投票。您正在寻找一种可以避免轮询的方法。看起来您可以使用作为插槽连接到refreshTable函数。太好了!这正是我所需要的。你所做的就是所谓的投票。您正在寻找一种可以避免轮询的方法。看起来您可以使用作为插槽连接到refreshTable函数。太好了!这正是我需要的。