From 03411f9954b77fd1f4c919349f28d92853f92f2f Mon Sep 17 00:00:00 2001 From: Yanru Mu Date: Fri, 21 Jun 2024 15:59:09 +0800 Subject: [PATCH] fix: MMessage show wrong position when its parent window in DCC --- dayu_widgets/message.py | 9 ++++++++- examples/message_example.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/dayu_widgets/message.py b/dayu_widgets/message.py index 37dfbd84..ea8e95d7 100644 --- a/dayu_widgets/message.py +++ b/dayu_widgets/message.py @@ -119,7 +119,14 @@ def _fade_int(self): def _set_proper_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, MMessage) and child.isVisible(): diff --git a/examples/message_example.py b/examples/message_example.py index 8a569e6f..10be6004 100644 --- a/examples/message_example.py +++ b/examples/message_example.py @@ -26,6 +26,21 @@ from dayu_widgets.push_button import MPushButton +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): + MMessage.info(parent=self, text="This is a message") + + class MessageExample(QtWidgets.QWidget, MFieldMixin): def __init__(self, parent=None): super(MessageExample, self).__init__(parent) @@ -126,6 +141,12 @@ def _init_ui(self): main_lay.addWidget(MLabel("全局设置默认duration(默认2秒);top(离parent顶端的距离,默认24px)")) 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) @@ -139,6 +160,9 @@ def slot_show_loading(self): msg = MMessage.loading("正在加载中", parent=self) msg.sig_closed.connect(functools.partial(MMessage.success, "加载成功啦!!哈哈哈哈", self)) + def slot_open_dialog(self): + dialog = MDialogExample(self) + dialog.exec_() if __name__ == "__main__": # Import local modules