Qt 显示/隐藏小部件动画

发布于 2024-08-19 14:03:56 字数 752 浏览 9 评论 0原文

我正在尝试实现显示/隐藏小部件动画。该小部件是一个 QDockWidget,因此位于 QMainWindowLayout 内部。

使用 QPropertyAnimation 似乎不起作用,我得到的东西看起来像这样:

m_listViewDock->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QPropertyAnimation* animation = new QPropertyAnimation(m_listViewDock, "geometry", m_listViewDock);

animation->setDuration(1000);

QRect g = m_listViewDock->geometry();
animation->setStartState(g);
g.setHeight(80);
animation->setEndState(g);
animation->start(QAbstractAnimation::DeleteWhenStopped);

不幸的是它没有做任何事情。我尝试使用其他属性(minimumHeight、fixedHeight),但出现同样的问题。

我以为我没有使用设计器正确设置小部件布局,但即使我使用最小尺寸,我仍然没有任何结果。如果我想玩转尺寸,我应该使用什么样的尺寸策略?

我被困住了,如果有人能澄清我的问题那就太好了。我不确定我做错了什么...

提前感谢您的帮助, 鲍里斯-

I'm trying to implement a show/hide widget animation. The widget is a QDockWidget and therefore is inside the QMainWindowLayout.

Using QPropertyAnimation doens't seem to work, I got something looking like that :

m_listViewDock->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QPropertyAnimation* animation = new QPropertyAnimation(m_listViewDock, "geometry", m_listViewDock);

animation->setDuration(1000);

QRect g = m_listViewDock->geometry();
animation->setStartState(g);
g.setHeight(80);
animation->setEndState(g);
animation->start(QAbstractAnimation::DeleteWhenStopped);

Unfortunately it doesn't do anything. I tried with other properties (minimumHeight, fixedHeight), but same issue.

I thought I didn't setup my widget layout correctly using the designer but even if I play with minimum sizes I still don't have any result. What kind of size policy should I use if I want to play with the size?

I'm stuck, it would be so great if someone could clarify my issue. I'm not sure I'm doing anything wrong...

Thanks in advance for your help,
Boris -

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-08-26 14:03:56

顺便说一句,这是 Qt 程序员如何在 QWidgetAnimator 中使用它,主要用于停靠小部件的动画,我正在做完全相同的事情......:

  const QRect final_geometry = _final_geometry.isValid() || widget->isWindow() ? _final_geometry :
        QRect(QPoint(-500 - widget->width(), -500 - widget->height()), widget->size());

#ifndef QT_NO_ANIMATION
    AnimationMap::const_iterator it = m_animation_map.constFind(widget);
    if (it != m_animation_map.constEnd() && (*it)->endValue().toRect() == final_geometry)
        return;

    QPropertyAnimation *anim = new QPropertyAnimation(widget, "geometry", widget);
    anim->setDuration(animate ? 200 : 0);
    anim->setEasingCurve(QEasingCurve::InOutQuad);
    anim->setEndValue(final_geometry);
    m_animation_map[widget] = anim;
    connect(anim, SIGNAL(finished()), SLOT(animationFinished()));
    anim->start(QPropertyAnimation::DeleteWhenStopped); 

By the way, here is how Qt programmers used it in the QWidgetAnimator, mainly used for animations of dock widgets, I'm doing exactly the same... :

  const QRect final_geometry = _final_geometry.isValid() || widget->isWindow() ? _final_geometry :
        QRect(QPoint(-500 - widget->width(), -500 - widget->height()), widget->size());

#ifndef QT_NO_ANIMATION
    AnimationMap::const_iterator it = m_animation_map.constFind(widget);
    if (it != m_animation_map.constEnd() && (*it)->endValue().toRect() == final_geometry)
        return;

    QPropertyAnimation *anim = new QPropertyAnimation(widget, "geometry", widget);
    anim->setDuration(animate ? 200 : 0);
    anim->setEasingCurve(QEasingCurve::InOutQuad);
    anim->setEndValue(final_geometry);
    m_animation_map[widget] = anim;
    connect(anim, SIGNAL(finished()), SLOT(animationFinished()));
    anim->start(QPropertyAnimation::DeleteWhenStopped); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文