Qt消息框 - 显示消息框直到超时
问题描述:
我必须显示保存消息框直到超时。 超时发生后,进入插槽并执行一些功能。Qt消息框 - 显示消息框直到超时
timerToSave=new QTimer(this);
connect(timerToSave,SIGNAL(timeout()),this,SLOT(SavingStatusSlot()));
上面的代码是定时器,当超时移动到savinglot时。
bool PopUpManager::PopUpSaveStaus()
{
timerToSave->start(3000);
saveStatus=false;
if(SetThread::getInstance()->UISaveStatus==ST_PROCESSING)
{
msgBox = new QMessageBox(0);
msgBox->setModal(true);
msgBox->setText("Saving ... ");
msgBox->setIcon(QMessageBox::Information);
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setCursor(Qt::WaitCursor);
msgBox->setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
msgBox->setStyleSheet("background-color:#444;color:#FFF;outline:none;");
msgBox->exec();
}
else
SavingStatusSlot();
return saveStatus;
}
上面的方法调用其他类,当用户点击保存按钮。 一旦调用该方法,启动计时器,然后显示消息框。
如果发生超时调用插槽[如下]
void PopUpManager::SavingStatusSlot()
{
msgBox->button(QMessageBox::Ok)->animateClick();
timerToSave->stop();
if(SetThread::getInstance()->UISaveStatus==ST_OK)
{
saveStatus=true;
}
else
{
PopUpWithOKButton(" Saving Error ");
saveStatus=false;
}
}
这个代码工作,我已经使用了消息框,确定按钮,当超时创建动画点击做一些功能。
现在我想以显示消息框没有按钮,并在超时,然后关闭消息框做了一些功能
但该消息框关闭()不工作。
void PopUpManager::ClosePopUP()
{
if(msgBox->isEnabled())
msgBox->close();
}
如果我调用上面的代码消息框必须关闭,但它正在显示。
任何人都可以帮助我解决这个问题。 在此先感谢。
答
我已经解决了这个问题
used msgBox-> show();而不是msgBox-> exec(); 和msgBox-> hide(); msgBox-> close();
代码如下。
bool PopUpManager::PopUpSaveStaus()
{
timerToSave->start(3000);
saveStatus=false;
if(UISaveStatus==ST_PROCESSING)
{
msgBox = new QMessageBox(QMessageBox::Information,"Error","Processing ... ",0,0,Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
msgBox->setStandardButtons(0);
msgBox->setCursor(Qt::WaitCursor);
msgBox->setStyleSheet("background-color:#444;color:#FFF;outline:none;");
msgBox->show();
}
else
{
SavingStatusSlot();
}
return saveStatus;
}
void PopUpManager::SavingStatusSlot()
{
msgBox->hide();
timerToSave->stop();
if(UISaveStatus==ST_OK)
{
saveStatus=true;
}
else
{
PopUpWithOKButton(" communication Failed ");
saveStatus=false;
}
}
也许这个页面的帮助你:[http://stackoverflow.com/questions/2236800/auto-close-qmessagebox](http://stackoverflow.com/questions/2236800/auto-close-qmessagebox) – aghilpro