Qt實現窗體在顯示屏旁邊自動隱藏(類似QQ)
看羣裏有人問這個東西,本人閒來無事便依照自己的想法實現了下:
其實實現的點子很簡單:
void AutoHideWidget::leaveEvent(QEvent *event)
{
isAutoHide();
if (m_bIsAutoHide)
{
hideWidget();
}
}
void AutoHideWidget::enterEvent(QEvent *event)
{
if (m_bIsAutoHide)
{
showWidget();
}
}
在鼠標離開窗體的時候去判斷窗體是否滿足自動隱藏的條件,以及自動隱藏的方向,如果滿足則自動隱藏,否則不操作。
鼠標再次進入窗體的時候,判斷是否自動隱藏,隱藏則自動顯示出來。
重點:
其實窗體隱藏後還是有很小的一部分顯示的,因爲就是依靠這點顯示區感應鼠標再次進入的事件。
眼裏的隱藏和顯示
看看代碼大家就很清楚了
void AutoHideWidget::hideWidget()
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(100);
animation->setStartValue(QRect(this->pos(), this->size()));
QRect rcEnd;
if (m_enDriection & Up)
{
rcEnd = QRect(this->x(), -this->height() + 2, this->size().width(), this->rect().height());
}
else if (m_enDriection & Left)
{
rcEnd = QRect(-this->width() + 2, this->y(), this->size().width(), this->rect().height());
}
else if (m_enDriection & Right)
{
rcEnd = QRect(m_nDesktopWidth - 2, this->y(), this->size().width(), this->rect().height());
}
animation->setEndValue(rcEnd);
animation->start();
}
void AutoHideWidget::showWidget()
{
QPoint pos = this->pos();
QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(100);
animation->setStartValue(QRect(pos, this->size()));
QRect rcEnd;
if (m_enDriection & Up)
{
rcEnd = QRect(this->x(), 0, this->size().width(), this->rect().height());
}
else if (m_enDriection & Left)
{
rcEnd = QRect(0, this->y(), this->size().width(), this->rect().height());
}
else if (m_enDriection & Right)
{
rcEnd = QRect(m_nDesktopWidth - this->width(), this->y(), this->size().width(), this->rect().height());
}
animation->setEndValue(rcEnd);
animation->start();
}
其實就是將窗體移動到屏幕外了,在移動的時候加上動畫就搞定了。
源代碼下載:源碼下載