diff --git a/dayu_widgets/toast.py b/dayu_widgets/toast.py index 11b1cb45..c1b769c4 100644 --- a/dayu_widgets/toast.py +++ b/dayu_widgets/toast.py @@ -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(): diff --git a/examples/toast_example.py b/examples/toast_example.py index f2925b5a..cc96cb95 100644 --- a/examples/toast_example.py +++ b/examples/toast_example.py @@ -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) @@ -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) @@ -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