Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed and optimized OpacityAniStackedWidget (PyQt6) #812

Open
wants to merge 4 commits into
base: PyQt6
Choose a base branch
from

Conversation

3rdPix
Copy link

@3rdPix 3rdPix commented Mar 21, 2024

Class OpacityAniStackedWidget presented a couple issues because it was not able to transition between widgets that had an index difference greater than 2 in the stack. Also, when transition was made, sometimes, older widgets would remain visible in the frame. Here is an example of this behavior, in which I linked key presses to setCurrentIndex method.

Vídeo sin título

Little explanation: I navigate to the "Cursos" subinterface from the window, and right below the commandBar that is shown in the frame, there is the stacked widget.

In this scenario, the book image is linked in a widget to index 0, in index 1 there is the CardWidget (with the orange square), and in index 2 there is an empty widget, it may be difficult to appreciate in this demonstration because of the incorrect behavior of the current class.

Another issue that former implementation presented was the increasing amount of memory used by the class as it creates new instances of QGraphicsOpacityEffect and QPropertyAnimation for each widget in the stacked widget. Finally, stack would break the program if there was an attempt to change to an index that did not exist.

I fixed all the issues described above as follows:

  • Transition not working: Created two animations, one that handles the "fade away" of the widget, and the other that handles the "fade in" of the new widget.
  • Too many instances: Only two animations and effects are preserved at all times, when it is time to change shown widget, those animations are then linked to the respective widgets, then deleted, and two new ones are created.
  • OutOfIndex: added small step to check if index exists in the stack.

Here is a presentation of the new, expected, behavior:

good

Note that this implementation works well in PyQt6, which I am most familiar with. It would be needed that someone with experience in older versions implements this in such versions.


OpacityAniStackedWidget 类出现了一些问题,因为它无法在堆栈中索引差值大于 2 的小部件之间进行转换。 此外,当进行转换时,有时旧的小部件将在框架中保持可见。 下面是此行为的一个示例,其中我将按键链接到 setCurrentIndex 方法。

Vídeo sin título

小解释:我从窗口导航到“Cursos”子界面,在框架中显示的命令栏的正下方,有堆叠的小部件。

在这种情况下,书籍图像在小部件中链接到索引 0,索引 1 中有 CardWidget(带有橙色方块),索引 2 中有一个空小部件,在本演示中可能很难理解,因为 当前类的不正确行为。

以前的实现提出的另一个问题是类使用的内存量不断增加,因为它为堆叠小部件中的每个小部件创建了“QGraphicsOpacityEffect”和“QPropertyAnimation”的新实例。 最后,如果尝试更改不存在的索引,堆栈将破坏程序。

我修复了上述所有问题,如下所示:

  • 过渡不起作用:创建了两个动画,一个处理小部件的“淡出”,另一个处理新小部件的“淡入”。
  • 太多实例:始终只保留两个动画和效果,当需要更改显示的小部件时,这些动画会链接到相应的小部件,然后删除,并创建两个新的。
  • OutOfIndex:添加了小步骤来检查堆栈中是否存在索引。

以下是新的预期行为的演示:

good

请注意,这个实现在我最熟悉的 PyQt6 中运行良好。 需要有旧版本经验的人在此类版本中实现这一点。


self._opacity1 = QGraphicsOpacityEffect(self)
self._opacity2 = QGraphicsOpacityEffect(self)
self._opacity2.setOpacity(0.0)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_opacity1 is assigned to former widget whilst _opacity2 is assigned to widget that is to be shown

def setDuration(self, ms: int) -> None:
"""Sets the duration of the transition between widgets"""
self._opacityUp.setDuration(ms)
self._opacityDown.setDuration(ms)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added method to customize duration of the transition.

@3rdPix 3rdPix changed the title Fixed and optimized OpacityAniStackedWidget Fixed and optimized OpacityAniStackedWidget (PyQt6) Mar 22, 2024
@3rdPix
Copy link
Author

3rdPix commented Mar 25, 2024

@zhiyiYo What do you think about this? Is there a reason you haven't looked into this? 你怎么看待这件事? 您还有什么理由没有研究过这个问题吗?

@zhiyiYo
Copy link
Owner

zhiyiYo commented Mar 25, 2024

I've been a bit busy lately, so I'll have to wait a little longer to verify this PR

@3rdPix
Copy link
Author

3rdPix commented Nov 27, 2024

Added a small fix to avoid a problem that arises when multiple objects try to paint a view at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants