在PyQt4中使用uic.loadUI()时无法获取密钥事件
我有以下无法捕获密钥事件的代码。 我使用uic.loadUi()加载GUI。 但我似乎无法捕捉键盘事件 请帮忙在PyQt4中使用uic.loadUI()时无法获取密钥事件,pyqt4,Pyqt4,我有以下无法捕获密钥事件的代码。 我使用uic.loadUi()加载GUI。 但我似乎无法捕捉键盘事件 请帮忙 class cMyApp(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self) self.ui = uic.loadUi("myApp.ui") #~ self.ui.show() # Show myApp
class cMyApp(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self)
self.ui = uic.loadUi("myApp.ui")
#~ self.ui.show() # Show myApp UI but key event Doesn't Work :(
self.show() # Show a small window but key event works.
def keyPressEvent(self, event):
if type(event)==QtGui.QKeyEvent:
print ("type(event) = ",type(event))
if event.key()==QtCore.Qt.Key_Escape:
print("Esc pressed!!!")
self.close()
event.accept()
else:
event.ignore()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
myApp = cMyApp()
sys.exit(app.exec_())
发现了问题;P
使用uic.loadUI()加载时,必须提供“self”作为baseinstance的另一个参数;否则默认为无
更正代码部分应为:
self.ui = uic.loadUi("myApp.ui", self) # Must supply 'self' as baseinstance.
self.ui.show() # Show myApp UI can work with key event now! :)