我在PySide工作了大约2周,我很喜欢它,但我很难理解一些更为中间的概念。任何帮助将不胜感激。
我试图通过QLineEdit和QCompleter在PySide中使用一些自定义QEvent。我使用旧样式进行信号/插槽连接,因为我还没有找到真正解释新语法的资源,但我认为这就是我的问题所在。
当我评论连接时,Maya不会崩溃。一旦我重新打开它,只要我点击标签,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()
答案 0 :(得分:0)
我认为我的问题与我连接信号和插槽的风格有关。我发现这个伟大的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()