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()
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: