QT 中 界面中消息的停留時間解決方案
要實現的理想效果是:
程序運行開始時界面中沒有文字顯示,然后有文字顯示,文字停留幾秒鍾,然后文字消失。
首先在UI中拖入一個 frame ,在frame 中拖入一個 label。
========================================================================
在類的聲明中聲明
QTimer *timer;
========================================================================
可以在構造函數中寫如下代碼
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(hideMsg()));
ui.frame->hide(); //初始將frame隱藏,就將frame中的控件隱藏了
========================================================================
在相應函數中顯示frame內容,同時出發計時器
ui.frame->show();
QFont font;
font.setPointSize(12);
ui.label->setFont(font); //設置label中字體大小
ui.label->setWordWrap(true); //設置label中字符換行
ui.label->setText(returnMsg);
timer->start(3000); //出發計時器,3秒過后將會觸發hideMsg()槽
========================================================================
void A::hideMsg()
{//槽函數--隱藏frame中的內容並停止計時器
ui.frame->hide();
timer->stop(); //停止計時器
}
========================================================================
最后別忘記在析構函數中釋放相應的內存
if (NULL != timer)
{
delete timer;
timer = NULL;
}
========================================================================