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

[Bug]: RoundMenu中创建的子菜单, 在每次重新打开RoundMenu时子菜单都会右移 #792

Closed
HydeYYHH opened this issue Mar 14, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@HydeYYHH
Copy link

What happened?

动画
发现在 components.widgets.menu SubMenuItemWidget类的 paintEvent方法中 self.width()每次调用会增加20, 添加self.setFixedWidth(self.width())可以解决bug

Operation System

Windows11

Python Version

3.10.7

PyQt/PySide Version

PyQt5.15.10

PyQt/PySide-Fluent-Widgets Version

1.5.2

How to Reproduce?

self.commandBar.addWidget(self.createDropDownButton())

    def createSubMenu(self):
        sub_menu = RoundMenu("Font style", self)
        sub_menu.setIcon(QIcon(':/res/font_style.png'))
        sub_menu.setMaxVisibleItems(10)
        sub_menu.addActions([
            Action(FontStyles[i], triggered=lambda _: self.fontStyle(i)) for i in range(len(FontStyles))
        ])
        return sub_menu
       
    def createDropDownButton(self):
        button = DropDownToolButton(FluentIcon.FONT, self)
        button.setFixedHeight(20)

        menu = RoundMenu(parent=self)
        menu.addMenu(self.createSubMenu())

        button.setMenu(menu)
        return button

Minimum code

self.commandBar.addWidget(self.createDropDownButton())

    def createSubMenu(self):
        sub_menu = RoundMenu("Font style", self)
        sub_menu.setIcon(QIcon(':/res/font_style.png'))
        sub_menu.setMaxVisibleItems(10)
        sub_menu.addActions([
            Action(FontStyles[i], triggered=lambda _: self.fontStyle(i)) for i in range(len(FontStyles))
        ])
        return sub_menu
       
    def createDropDownButton(self):
        button = DropDownToolButton(FluentIcon.FONT, self)
        button.setFixedHeight(20)

        menu = RoundMenu(parent=self)
        menu.addMenu(self.createSubMenu())

        button.setMenu(menu)
        return button
@HydeYYHH HydeYYHH added the bug Something isn't working label Mar 14, 2024
@HydeYYHH HydeYYHH changed the title [Bug]: RoundMenuROun中创建的子菜单, 在每次重新打开RoundMenu时子菜单都会右移 [Bug]: RoundMenu中创建的子菜单, 在每次重新打开RoundMenu时子菜单都会右移 Mar 14, 2024
@AlexZhu2001
Copy link
Contributor

是由与RoundMenu::adjustSize的self.viewport().adjustSize()这一句导致的。

看了一下qt源码,adjustSize这个语句做了以下几件事:

  1. s=sizeHint()取一下viewport的sizeHint
  2. 如果是窗口会额外计算一下窗口能不能在当前屏幕放得下,viewport不是窗口这里不会执行
  3. 如果此时s不是有效的值,r=childrenRect(),然后设定大小为QSize(r.width()+2*r.x(), r.height + 2*r.y())

不知道为什么,在菜单显示之后,childrenRect的x始终是36,所以viewport每次会增大72,而内部控件会在viewport增大后自动扩大,接下来就是重复套娃。
没有很好的解决方案,如果确定RoundMenu不允许内部控件动态改变大小,这里可以替换成

r = self.viewport().childrenRect()
self.viewport().resize(self.viewport().width(), r.height() + 2 * r.y())

现在的RoundMenu其实也无法容纳动态的Widget,因为QListsWidgetItem的sizeHint是在插入时设置的,而RoundMenu依靠item的sizeHint宽度来计算自己的宽度,如果要支持动态宽度的Widget,可能需要继承QListsWidgetItem重写一个item

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants