Maya 等待 Qt 窗口关闭 [英] Maya wait for Qt window to close

查看:72
本文介绍了Maya 等待 Qt 窗口关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在 Qt 应用程序中,您会在代码的开头和结尾使用它:

Normally in Qt applications you would use this at the start and end of your code:

app = QtWidgets.QApplication(sys.argv)
...
app.exec_()

但是在 Maya 中,您不使用它,因为 Qt 本身在 Maya 应用程序上运行.如果您不知道 Maya 是什么,我相信这也适用于许多其他应用程序.也就是说,我的代码如下所示:

But in Maya, you don't use this because Qt runs on the Maya application it self. I'm sure this works the same for many other applications as well if you don't know what Maya is. That said, my code looks like this:

import sys
from PySide2 import QtWidgets, QtGui, QtCore

class Test():
    def __init__(self):
        self.open_qt()

    def open_qt(self):
        # app = QtWidgets.QApplication(sys.argv) # Don't need this in Maya
        self.window = QtWidgets.QWidget() # I tried QDialog also

        btn = QtWidgets.QPushButton("press me")
        btn.clicked.connect(self.login)

        lay = QtWidgets.QVBoxLayout()
        lay.addWidget(btn)

        self.window.setLayout(lay)
        self.window.show()

        # app.exec_() # Don't need this in Maya

    def login(self):
        print("logged in!")

print("before")
temp = Test()
print("after")

但是在 Maya 中运行它我得到了这个结果:

But running this in Maya I get this result:

before
after
logged in!

但我需要它:

before
logged in!
after

如果您在 Maya 之外运行此代码(并且您使用了这两行注释掉的行),那么您会得到正确的结果(此处的块).

If you run this code outside of Maya (and you use those two commented out lines) then you get the correct result (block above here).

我怎样才能让 Maya Qt 也像使用 QtWidgets.QApplication(sys.argv) 一样正确等待?

How can I get the Maya Qt to also wait correctly like it would if you used QtWidgets.QApplication(sys.argv)?

推荐答案

QDialog 可能更适合您的需求,因为它运行自己的事件循环,不会阻塞程序,同时等待对话框出现完成.

A QDialog might be more well suited for your needs, as it runs its own event loop that won't block the program, while waiting for the dialog to be completed.

重要的是调用它的exec_() 并调用 accept() 需要时.

The important thing to do is to call of its exec_() and call accept() when needed.

from PySide2 import QtWidgets, QtGui, QtCore

class Test(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Test, self).__init__(parent)
        layout = QtWidgets.QVBoxLayout(self)

        self.spinBox = QtWidgets.QSpinBox()
        layout.addWidget(self.spinBox)

        btn = QtWidgets.QPushButton("press me")
        layout.addWidget(btn)
        btn.clicked.connect(self.login)

    def login(self):
        print("logged in!")
        self.accept()


dialog = Test()
if dialog.exec_():
    print(dialog.spinBox.value())

我没有 Maya,但根据 这个答案 您可以使用 maya.OpenMayaUI 模块和 shiboken 的 wrapInstance() 获取其主窗口.

I don't have Maya, but according to this answer you can get its main window using the maya.OpenMayaUI module and shiboken's wrapInstance().

这篇关于Maya 等待 Qt 窗口关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆