Skip to content

Commit a4a909c

Browse files
muyrloonghao
authored andcommitted
fix: MToast show wrong position when its parent window in DCC
1 parent a13a4d3 commit a4a909c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

dayu_widgets/toast.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,14 @@ def _fade_int(self):
123123

124124
def _get_center_position(self, parent):
125125
parent_geo = parent.geometry()
126-
pos = parent_geo.topLeft() if parent.parent() is None else parent.mapToGlobal(parent_geo.topLeft())
126+
if parent.isWindowType():
127+
# 其parent没有parent了,就是个完全独立的窗口
128+
pos = parent_geo.topLeft()
129+
elif parent in QtWidgets.QApplication.topLevelWidgets():
130+
# 其parent虽然是独立窗口,但却还有parent,常见情况,DCC中比如Maya我们开发的工具窗口,会将maya主窗口作为工具节目的parent
131+
pos = parent_geo.topLeft()
132+
else:
133+
pos = parent.mapToGlobal(parent_geo.topLeft())
127134
offset = 0
128135
for child in parent.children():
129136
if isinstance(child, MToast) and child.isVisible():

examples/toast_example.py

+24
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ def run(self):
3737
time.sleep(3)
3838

3939

40+
class MDialogExample(QtWidgets.QDialog):
41+
def __init__(self, parent=None):
42+
super(MDialogExample, self).__init__(parent=parent)
43+
main_lay = QtWidgets.QVBoxLayout()
44+
button = MPushButton("Show Message")
45+
button.clicked.connect(self.slot_show_message)
46+
47+
main_lay.addWidget(MLabel("This is a dialog in DCC. Click the button to show a message."))
48+
main_lay.addWidget(button)
49+
self.setLayout(main_lay)
50+
51+
def slot_show_message(self):
52+
MToast.info(parent=self, text="This is a message")
53+
54+
4055
class ToastExample(QtWidgets.QWidget, MFieldMixin):
4156
def __init__(self, parent=None):
4257
super(ToastExample, self).__init__(parent)
@@ -78,6 +93,12 @@ def _init_ui(self):
7893
main_lay.addWidget(MLabel("不同的提示状态:成功、失败、加载中。默认2秒后消失"))
7994
main_lay.addWidget(loading_button)
8095

96+
main_lay.addWidget(MDivider("Open Dialog"))
97+
button_open_dialog = MPushButton("Open Dialog")
98+
button_open_dialog.clicked.connect(self.slot_open_dialog)
99+
main_lay.addWidget(button_open_dialog)
100+
main_lay.addWidget(MLabel("模拟在DCC 里面弹出我们开发的工具窗口"))
101+
81102
main_lay.addStretch()
82103
self.setLayout(main_lay)
83104

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

121+
def slot_open_dialog(self):
122+
dialog = MDialogExample(self)
123+
dialog.exec_()
100124

101125
if __name__ == "__main__":
102126
# Import local modules

0 commit comments

Comments
 (0)