QTreeWidget expand animation on double click

╄→гoц情女王★ 提交于 2020-01-11 09:18:25

问题


I have created a QTreeWidget and set animation to true (setAnimated(true)). When I'm clicking on a mark (triangle) at the left of item it expands smoothly, but when I'm double clicking on the item it expands too fast (almost like there is no "animated" flag set). I want smooth animation on double click too. How can I solve this problem?

QTreeView calls QTreeViewPrivate::expandOrCollapseItemAtPos on mark click and QTreeViewPrivate::expand on double click, so I have no access to these methods.

I'm using PySide for creating Qt application (but I've tried C++ and the problem is the same).


回答1:


You need to override the click behaviour. Check the event if it's a double click or not and then you can redirect the event to the appropriate call. You should check the state if it's already clicked or not to prevent a second animation which might happen.




回答2:


The trick is, double click also rise a single click in default implementation. Before expand animation, the default implementation call QAbstractItemView::setState(AnimatingState) to set internal state for animation.

And in QAbstractItemView::mouseReleaseEvent(), QAbstractItemView::setState(NoState) is called before animation finished. After this, animation terminated immediately.

Use following code to work around:

void YourSubTreeView::mouseReleaseEvent(QMouseEvent* event)
{
    QAbstractItemView::State preState = state();
    QTreeView::mouseReleaseEvent(event);
    if (preState == QAbstractItemView::AnimatingState)
        setState(preState);
}


来源:https://stackoverflow.com/questions/13304136/qtreewidget-expand-animation-on-double-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!