0

I'm about 2 weeks into PySide and I'm loving it, but I'm having trouble understanding some of the more intermediate concepts. Any help would be greatly appreciated.

I'm trying to get a few custom QEvents working in PySide with QLineEdit and QCompleter. I'm using the old style for signal/slot connections because I haven't found a resource yet that really explains the new syntax, but I think this is where my problem lies.

When I comment out the connection, Maya won't crash. Once I turn it back on, Maya crashes whenever I hit tab.

Any help would be greatly appreciated!

Thanks!

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()
Mike Bourbeau
  • 481
  • 11
  • 29
  • This question is actually 4 questions. Try and keep the questions focussed on one problem at a time, so answerers and site users can benefit better from it. – kartikg3 Jun 11 '15 at 22:45
  • Also in the whole post you havent mentioned Maya crashing. You just mention you get the error, in the title though you say Maya crashes. What is actually happening? And again, try and keep the question focused to one issue. – kartikg3 Jun 11 '15 at 22:47
  • Sorry, it's my first post :/ I actually answered my question last night, so I'll post the answer in a second. I'll make some edits to my original post so everything is relevant. – Mike Bourbeau Jun 12 '15 at 13:29
  • Great! Would be great if you can post the answer. – kartikg3 Jun 12 '15 at 13:30

1 Answers1

0

I think my problem was related to the style in which I was connecting the signal and slot. I found this great post that walks you through the new style.

I created a few variables before the MyLineEdit's constructor. Then I used those variables in the tab_event function. Then I connected the signal to the slot with the new connection syntax.

Here is the updated code with comments:

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()
Community
  • 1
  • 1
Mike Bourbeau
  • 481
  • 11
  • 29