簡述
網頁右下角上經常會出現一些提示性的信息,桌面軟件中也比較常見,類似360新聞、QQ消息提示一樣!
這種功能用動畫實現起來很簡單,這節我們暫時使用定時器來實現,后面章節會對動畫框架進行詳細講解。
下面我們來實現一個右下角冒泡的功能。
效果
實現原理
連接信號與槽
m_pShowTimer = new QTimer(this);
m_pStayTimer = new QTimer(this);
m_pCloseTimer = new QTimer(this);
connect(m_pShowTimer, SIGNAL(timeout()), this, SLOT(onMove()));
connect(m_pStayTimer, SIGNAL(timeout()), this, SLOT(onStay()));
connect(m_pCloseTimer, SIGNAL(timeout()), this, SLOT(onClose()));
實現
界面現實的時候調用showMessage(),然后啟動定時器開始顯示、駐留、關閉。
void MainWindow::showMessage()
{
QRect rect = QApplication::desktop()->availableGeometry();
m_point.setX(rect.width() - width());
m_point.setY(rect.height() - height());
move(m_point.x(), m_point.y());
m_pShowTimer->start(5);
} void MainWindow::onMove()
{
m_nDesktopHeight--;
move(m_point.x(), m_nDesktopHeight); if (m_nDesktopHeight <= m_point.y())
{
m_pShowTimer->stop();
m_pStayTimer->start(5000);
}
} void MainWindow::onStay()
{
m_pStayTimer->stop();
m_pCloseTimer->start(100);
} void MainWindow::onClose()
{
m_dTransparent -= 0.1; if (m_dTransparent <= 0.0)
{
m_pCloseTimer->stop();
close();
} else {
setWindowOpacity(m_dTransparent);
}
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43