Maya PySide:当我尝试将自定义信号连接到插槽时,Maya 崩溃

Maya PySide: Maya crashes when I try to connect custom signal to slot

我已经使用 PySide 大约 2 周了,我很喜欢它,但是我在理解一些更中级的概念时遇到了困难。任何帮助将不胜感激。

我正在尝试使用 QLineEdit 和 QCompleter 在 PySide 中使用一些自定义 QEvent。我对 signal/slot 连接使用旧样式,因为我还没有找到真正解释新语法的资源,但我认为这就是我的问题所在。

当我注释掉连接时,Maya 不会崩溃。一旦我重新打开它,每当我点击 Tab 键时 Maya 就会崩溃。

如有任何帮助,我们将不胜感激!

谢谢!

from PySide import QtCore, QtGui
from shiboken import wrapInstance 
import maya.OpenMayaUI as mui

def maya_main_window():
    ptr = mui.MQtUtil.mainWindow()
    return wrapInstance( long( ptr ), QtGui.QWidget )   

####################################################################
class MyWindow(QtGui.QDialog): 
    def __init__( self, parent=maya_main_window() ):
        super( MyWindow, self ).__init__( parent )

        # create objects
        self.la = QtGui.QLabel("Press tab in this box:")
        self.le = MyLineEdit()
        self.wordList = ["hi", "bye", "yes", "lane"]
        self.completer = QtGui.QCompleter( self.wordList, self )
        self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
        self.la2 = QtGui.QLabel("\nLook here:")
        self.le2 = QtGui.QLineEdit()
        self.le.setCompleter(self.completer)

        # layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.la)
        layout.addWidget(self.le)
        layout.addWidget(self.la2)
        layout.addWidget(self.le2)
        self.setLayout(layout)

        #####################
        # connections
        #####################
        self.connect(self.le, QtCore.SIGNAL("tabPressed"), self.on_tab)
        self.connect(self.le, QtCore.SIGNAL("escPressed"), self.on_esc)

        #####################
        # proper new style?
        #####################
        #self.le.tab_event.connect(self.on_tab)
        #self.le.esc_event.connect(self.on_tab)

    ######################
    # Slots
    ######################
    def on_tab(self):
        # I'd like tab to have the same function as arrow down
        print "tabbed"

    def on_esc(self):
        self.close()


####################################################################
class MyLineEdit( QtGui.QLineEdit):

    def __init__(self, parent=maya_main_window()):
        super( MyLineEdit, self ).__init__( parent  )

    ########################
    # Custom Signals
    ########################
    def tab_event(self, event):
        if (event.type()==QtCore.QEvent.KeyPress) and (event.key()==QtCore.Qt.Key_Tab):
            self.emit(QtCore.SIGNAL("tabPressed"))
            return True

        return QtGui.QLineEdit.event(self, event)

    def esc_event(self, event):
        if (event.type()==QtCore.QEvent.KeyPress) and (event.key()==QtCore.Qt.Key_Escape):
            self.emit(QtCore.SIGNAL("escPressed"))
            return True



####################################################################
if __name__ == "__main__": 
    # Development stuff
    try:
        myWindow_ui.close()
        myWindow_ui.deleteLater()
    except:
        pass


    myWindow_ui = MyWindow()
    myWindow_ui.show()


    # Development stuff
    try:
        myWindow_ui.show()
    except:
        myWindow_ui.close()
        myWindow_ui.deleteLater()

我认为我的问题与连接信号槽的方式有关。我发现这个很棒的 post 可以带您了解新样式。

我在 MyLineEdit 的构造函数之前创建了一些变量。然后我在 tab_event 函数中使用了这些变量。然后我用新的连接语法将信号连接到插槽。

这是带有注释的更新代码:

from PySide import QtCore, QtGui
from shiboken import wrapInstance 
import maya.OpenMayaUI as mui

def maya_main_window():
    ptr = mui.MQtUtil.mainWindow()
    return wrapInstance( long( ptr ), QtGui.QWidget )   

####################################################################
class MyWindow(QtGui.QDialog): 
    def __init__( self, parent=maya_main_window() ):
        super( MyWindow, self ).__init__(  )

        # create objects
        self.la = QtGui.QLabel("Press tab in this box:")
        self.le = MyLineEdit()
        self.la2 = QtGui.QLabel("\nLook here:")
        self.le2 = QtGui.QLineEdit()

        # layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.la)
        layout.addWidget(self.le)
        layout.addWidget(self.la2)
        layout.addWidget(self.le2)
        self.setLayout(layout)

        # connections
        # Bad syntax
        #self.connect(self.le, QtCore.SIGNAL("tabPressed"), self.update)
        # Correct syntax
        self.le.tab_pressed.connect(self.update)

    # Slot   
    def update(self):
        newtext = str(self.le2.text()) + "tab pressed "
        self.le2.setText(newtext)

####################################################################
class MyLineEdit( QtGui.QLineEdit):

    # Create variables before the constructor
    tab_pressed = QtCore.Signal(str)
    signal_str = "tabPressed"

    def __init__(self, parent=None):
        super( MyLineEdit, self ).__init__(   )

    # Signal
    def event(self, event):

        # Variables inserted
        if (event.type()==QtCore.QEvent.KeyPress) and (event.key()==QtCore.Qt.Key_Tab):
            self.tab_pressed.emit(self.signal_str)
            return True

        return QtGui.QLineEdit.event(self, event)

####################################################################
if __name__ == "__main__": 
# Development stuff
    try:
        myWindow_ui.close()
        myWindow_ui.deleteLater()
    except:
        pass


    myWindow_ui = MyWindow()
    myWindow_ui.show()


    # Development stuff
    try:
        myWindow_ui.show()
    except:
        myWindow_ui.close()
        myWindow_ui.deleteLater()