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
4.99k stars 464 forks source link

[Bug]: ElevatedCardWidget添加右键菜单后,连续点击右键会错乱 #891

Open wanghaiqing2015 opened 1 month ago

wanghaiqing2015 commented 1 month ago

What happened?

在examples\examples\view\card_widget\demo.py 的EmojiCard中添加contextMenuEvent方法后,

运行demo,右键单击EmojiCard,

注意的是右键第一个,右键第二个,右键第三个。。。就会出现EmojiCard跑到第一个位置上去了。

Operation System

Windows11 and Windows10

Python Version

3.8 64位

PyQt/PySide Version

PySide 6.6.3.1

PyQt/PySide-Fluent-Widgets Version

1.5.6

How to Reproduce?

在examples\examples\view\card_widget\demo.py 的EmojiCard中添加contextMenuEvent方法后,

运行demo,右键单击EmojiCard,

注意的是右键第一个,右键第二个,右键第三个。。。就会出现EmojiCard跑到第一个位置上去了。 11111111111

https://github.com/zhiyiYo/PyQt-Fluent-Widgets/assets/11815186/f4619d1c-6802-4ada-830a-d02a4e37f5df

Minimum code

# coding:utf-8
import sys
from pathlib import Path

from PySide6.QtCore import Qt, QPoint, QSize, QUrl, QRect, QPropertyAnimation
from PySide6.QtGui import QIcon, QFont, QColor, QPainter
from PySide6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QGraphicsOpacityEffect

from qfluentwidgets import (CardWidget, setTheme, Theme, IconWidget, BodyLabel, CaptionLabel, PushButton,
                            TransparentToolButton, FluentIcon, RoundMenu, Action, ElevatedCardWidget,
                            ImageLabel, isDarkTheme, FlowLayout, MSFluentTitleBar, SimpleCardWidget,
                            HeaderCardWidget, InfoBarIcon, HyperlinkLabel, HorizontalFlipView,
                            PrimaryPushButton, TitleLabel, PillPushButton, setFont, SingleDirectionScrollArea,
                            VerticalSeparator, MSFluentWindow, NavigationItemPosition)

from qfluentwidgets.components.widgets.acrylic_label import AcrylicBrush
from qfluentwidgets import FluentIcon as FIF
from PySide6.QtGui import QAction
from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget
from qfluentwidgets import CaptionLabel, ElevatedCardWidget, ImageLabel, FlowLayout, ScrollArea
from qfluentwidgets import RoundMenu, Action, MenuAnimationType, MenuItemDelegate
from qfluentwidgets import FluentIcon as FIF

def isWin11():
    return sys.platform == 'win32' and sys.getwindowsversion().build >= 22000

if isWin11():
    from qframelesswindow import AcrylicWindow as Window
else:
    from qframelesswindow import FramelessWindow as Window

class EmojiCard(ElevatedCardWidget):
    """ Emoji card """

    def __init__(self, iconPath: str, parent=None):
        super().__init__(parent)
        self.iconWidget = ImageLabel(iconPath, self)
        self.label = CaptionLabel(Path(iconPath).stem, self)

        self.iconWidget.scaledToHeight(68)

        self.vBoxLayout = QVBoxLayout(self)
        self.vBoxLayout.setAlignment(Qt.AlignCenter)
        self.vBoxLayout.addStretch(1)
        self.vBoxLayout.addWidget(self.iconWidget, 0, Qt.AlignCenter)
        self.vBoxLayout.addStretch(1)
        self.vBoxLayout.addWidget(
            self.label, 0, Qt.AlignHCenter | Qt.AlignBottom)

        self.setFixedSize(168, 176)
    def contextMenuEvent(self, e):
        menu = RoundMenu(parent=self)
        # menu = CheckableMenu(parent=self, indicatorType=MenuIndicatorType.RADIO)

        # NOTE: hide the shortcut key
        # menu.view.setItemDelegate(MenuItemDelegate())

        # add actions
        menu.addAction(Action(FIF.COPY, 'Copy'))
        menu.addAction(Action(FIF.CUT, 'Cut'))
        menu.actions()[0].setCheckable(True)
        menu.actions()[0].setChecked(True)

        # add sub menu
        submenu = RoundMenu("Add to", self)
        submenu.setIcon(FIF.ADD)
        submenu.addActions([
            Action(FIF.VIDEO, 'Video'),
            Action(FIF.MUSIC, 'Music'),
        ])
        menu.addMenu(submenu)

        # add actions
        menu.addActions([
            Action(FIF.PASTE, 'Paste'),
            Action(FIF.CANCEL, 'Undo')
        ])

        # add separator
        menu.addSeparator()
        menu.addAction(QAction(f'Select all', shortcut='Ctrl+A'))

        # insert actions
        menu.insertAction(
            menu.actions()[-1], Action(FIF.SETTING, 'Settings', shortcut='Ctrl+S'))
        menu.insertActions(
            menu.actions()[-1],
            [Action(FIF.HELP, 'Help', shortcut='Ctrl+H'),
             Action(FIF.FEEDBACK, 'Feedback', shortcut='Ctrl+F')]
        )
        menu.actions()[-2].setCheckable(True)
        menu.actions()[-2].setChecked(True)

        # show menu
        menu.exec(e.globalPos(), aniType=MenuAnimationType.DROP_DOWN)

class MicaWindow(Window):

    def __init__(self):
        super().__init__()
        self.setTitleBar(MSFluentTitleBar(self))
        if isWin11():
            self.windowEffect.setMicaEffect(self.winId(), isDarkTheme())

class Demo1(MicaWindow):

    def __init__(self):
        super().__init__()
        self.setWindowIcon(QIcon(':/qfluentwidgets/images/logo.png'))
        self.setWindowTitle('Fluent Emoji gallery')

        self.flowLayout = FlowLayout(self)

        self.resize(580, 680)
        self.flowLayout.setSpacing(6)
        self.flowLayout.setContentsMargins(30, 60, 30, 30)
        self.flowLayout.setAlignment(Qt.AlignVCenter)

        for path in Path('./resource').glob('*.png'):
            self.addCard(str(path))

    def addCard(self, iconPath: str):
        card = EmojiCard(iconPath, self)
        self.flowLayout.addWidget(card)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w1 = Demo1()
    w1.show()
    app.exec()
Liruochen1207 commented 2 weeks ago

好家伙你这个代码我这边显示没QApplication直接死掉 image

wanghaiqing2015 commented 2 days ago

代码同级目录下,需要有resource文件夹,需要在样例中执行这段代码的