zhiyiYo / PyQt-Fluent-Widgets

A fluent design widgets library based on C++ Qt/PyQt/PySide. Make Qt Great Again.
https://qfluentwidgets.com
GNU General Public License v3.0
5.61k stars 541 forks source link

[Bug]: Round Menu 由于分割线的存在导致无法正常删除Action #676

Closed qiauil closed 10 months ago

qiauil commented 10 months ago

What happened?

在RoundMenu的删除Action的代码中

    def removeAction(self, action: Union[QAction, Action]):
        """ remove action from menu """
        if action not in self._actions:
            return

        index = self._actions.index(action)
        self._actions.remove(action)
        action.setProperty('item', None)
        item = self.view.takeItem(index)
        item.setData(Qt.ItemDataRole.UserRole, None)
        super().removeAction(action)

        # delete widget
        widget = self.view.itemWidget(item)
        if widget:
            widget.deleteLater()

self.view.takeitem(index)中的index与_actions中的index一致。如果此时菜单中包含分割线(Separator), 则分割线也是一个item,也占用一个index位置。这将导致删除某些Action中实际上在view中删除的是分割线而不是action对应的item。

Operation System

Windows 11

Python Version

3.11.5

PyQt/PySide Version

PyQt6

PyQt/PySide-Fluent-Widgets Version

1.2.0

How to Reproduce?

运行以下代码将会发现action5-9还出现在菜单中:

from qfluentwidgets import RoundMenu,Action
from PyQt6.QtWidgets import QApplication
import sys

app = QApplication(sys.argv)
my_Menu=RoundMenu()
actions=[Action("action{}".format(i)) for i in range(10)]
for action in actions:
    my_Menu.addAction(action)
    my_Menu.addSeparator()
my_Menu.show()
for action in actions:
    my_Menu.removeAction(action)
sys.exit(app.exec())

Minimum code

from qfluentwidgets import RoundMenu,Action
from PyQt6.QtWidgets import QApplication
import sys

app = QApplication(sys.argv)
my_Menu=RoundMenu()
actions=[Action("action{}".format(i)) for i in range(10)]
for action in actions:
    my_Menu.addAction(action)
    my_Menu.addSeparator()
my_Menu.show()
for action in actions:
    my_Menu.removeAction(action)
sys.exit(app.exec())
qiauil commented 10 months ago
image
zhiyiYo commented 10 months ago

版本太低了换成1.4.3