Skip to content

Commit

Permalink
fix: MToast show wrong position when its parent window in DCC
Browse files Browse the repository at this point in the history
  • Loading branch information
muyr authored and loonghao committed Jun 21, 2024
1 parent a13a4d3 commit a4a909c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion dayu_widgets/toast.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ def _fade_int(self):

def _get_center_position(self, parent):
parent_geo = parent.geometry()
pos = parent_geo.topLeft() if parent.parent() is None else parent.mapToGlobal(parent_geo.topLeft())
if parent.isWindowType():
# 其parent没有parent了,就是个完全独立的窗口
pos = parent_geo.topLeft()
elif parent in QtWidgets.QApplication.topLevelWidgets():
# 其parent虽然是独立窗口,但却还有parent,常见情况,DCC中比如Maya我们开发的工具窗口,会将maya主窗口作为工具节目的parent
pos = parent_geo.topLeft()
else:
pos = parent.mapToGlobal(parent_geo.topLeft())
offset = 0
for child in parent.children():
if isinstance(child, MToast) and child.isVisible():
Expand Down
24 changes: 24 additions & 0 deletions examples/toast_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def run(self):
time.sleep(3)


class MDialogExample(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MDialogExample, self).__init__(parent=parent)
main_lay = QtWidgets.QVBoxLayout()
button = MPushButton("Show Message")
button.clicked.connect(self.slot_show_message)

main_lay.addWidget(MLabel("This is a dialog in DCC. Click the button to show a message."))
main_lay.addWidget(button)
self.setLayout(main_lay)

def slot_show_message(self):
MToast.info(parent=self, text="This is a message")


class ToastExample(QtWidgets.QWidget, MFieldMixin):
def __init__(self, parent=None):
super(ToastExample, self).__init__(parent)
Expand Down Expand Up @@ -78,6 +93,12 @@ def _init_ui(self):
main_lay.addWidget(MLabel("不同的提示状态:成功、失败、加载中。默认2秒后消失"))
main_lay.addWidget(loading_button)

main_lay.addWidget(MDivider("Open Dialog"))
button_open_dialog = MPushButton("Open Dialog")
button_open_dialog.clicked.connect(self.slot_open_dialog)
main_lay.addWidget(button_open_dialog)
main_lay.addWidget(MLabel("模拟在DCC 里面弹出我们开发的工具窗口"))

main_lay.addStretch()
self.setLayout(main_lay)

Expand All @@ -97,6 +118,9 @@ def slot_finished(self):
self.msg.close()
MToast.success("加载成功", self)

def slot_open_dialog(self):
dialog = MDialogExample(self)
dialog.exec_()

if __name__ == "__main__":
# Import local modules
Expand Down

0 comments on commit a4a909c

Please sign in to comment.