Skip to content

Commit

Permalink
fix: MMessage 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 8c6ce2c commit 03411f9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion dayu_widgets/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
24 changes: 24 additions & 0 deletions examples/message_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 03411f9

Please sign in to comment.