python 2.7 - in pyqt how to print "Ctrl+key" in QLineEdit when pressed Ctrl + anyKey -


here kode genereted event qlineedit put pressed key combination in textline ok shift key , alt key not ctrl key why ?

#!/usr/bin/python  import sys pyqt4.qtcore import * pyqt4.qtgui import *  def main():     app = qapplication(sys.argv)     w = mywindow()     w.show()     sys.exit(app.exec_()) class mywindow(qwidget):     def __init__(self, *args):         qwidget.__init__(self, *args)          self.la = qlabel("press tab in box:")         self.le = mylineedit()           layout = qvboxlayout()         layout.addwidget(self.la)         layout.addwidget(self.le)         self.setlayout(layout)           self.connect(self.le, signal("press"),                  self.update)      def update(self):         oldtext = str(self.le.text())         self.le.settext(self.le.mytext)  class mylineedit(qlineedit):     def __init__(self, *args):         qlineedit.__init__(self, *args)          self.mytext = ""     def event(self, event):         if event.type() == qevent.keypress:             if  event.modifiers() & qt.controlmodifier  : 

in textline ok shift key , alt key can put variable self.mytext value in textline, not ctrl key why ?

                print event.text()                 self.mytext = "ctrl+"+ event.text()                 self.emit(signal("press"))             return true         return qlineedit.event(self, event)  if __name__ == "__main__":     main() 

the reason why original example doesn't work because many of ctrl+key combinations produce control characters - e.g. ctrl+j produces newline.

the correct way capture keyboard combinations using qkeysequence, work all keys, including function keys, arrow keys, page up/down keys, etc. can translated shortcuts using qkeysequence.portabletext.

here's demo based on original example:

import sys pyqt4.qtcore import * pyqt4.qtgui import *  def main():     app = qapplication(sys.argv)     w = mywindow()     w.show()     sys.exit(app.exec_())  class mywindow(qwidget):     def __init__(self, *args):         qwidget.__init__(self, *args)          self.la = qlabel("press tab in box:")         self.le = mylineedit()          layout = qvboxlayout()         layout.addwidget(self.la)         layout.addwidget(self.le)         self.setlayout(layout)          self.le.keypressed.connect(self.update)      def update(self, text):         self.le.settext(text)  mod_mask = (qt.ctrl | qt.alt | qt.shift | qt.meta)  class mylineedit(qlineedit):     keypressed = pyqtsignal(str)      def keypressevent(self, event):         keyname = ''         key = event.key()         modifiers = int(event.modifiers())         if (modifiers , modifiers & mod_mask == modifiers ,             key > 0 , key != qt.key_shift , key != qt.key_alt ,             key != qt.key_control , key != qt.key_meta):              keyname = qkeysequence(modifiers + key).tostring()              print('event.text(): %r' % event.text())             print('event.key(): %d, %#x, %s' % (key, key, keyname))          self.keypressed.emit(keyname)  if __name__ == "__main__":     main() 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -