Qt实现窗体在显示屏旁边自动隐藏(类似QQ)

Qt实现窗体在显示屏旁边自动隐藏(类似QQ)

看群里有人问这个东西,本人闲来无事便依照自己的想法实现了下:

Qt实现窗体在显示屏旁边自动隐藏(类似QQ)_第1张图片
其实实现的点子很简单:

void AutoHideWidget::leaveEvent(QEvent *event)
{
    isAutoHide();
    if (m_bIsAutoHide)
    {
        hideWidget();
    }
}

void AutoHideWidget::enterEvent(QEvent *event)
{
    if (m_bIsAutoHide)
    {
        showWidget();
    }
}

在鼠标离开窗体的时候去判断窗体是否满足自动隐藏的条件,以及自动隐藏的方向,如果满足则自动隐藏,否则不操作。
鼠标再次进入窗体的时候,判断是否自动隐藏,隐藏则自动显示出来。

重点:

Qt实现窗体在显示屏旁边自动隐藏(类似QQ)_第2张图片
其实窗体隐藏后还是有很小的一部分显示的,因为就是依靠这点显示区感应鼠标再次进入的事件。

眼里的隐藏和显示

看看代码大家就很清楚了

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();
}

其实就是将窗体移动到屏幕外了,在移动的时候加上动画就搞定了。

源代码下载:源码下载

你可能感兴趣的:(Qt)