Home>

I am making the following GUI with PySide, but I am stumbling.
・ Multiple QLineEdits are installed
-After entering a number, press Enter to move the focus to the next QLineEdit.

I was able to define a keyPressEvent as shown below and press Enter to move the focus.
I can no longer enter (display) anything in QLineEdit, including numbers.

def keyPressEvent (self, event):
    if event.key () == Qt.Key_Enter:
        print ("Pressed Enter")
        self.input [self.n + 1] .setFocus () # Move focus to next QLineEdit

I tried and errored to accept the input, but it didn't work.
Can anyone give me some advice?

  • Answer # 1

    Create a class that inherits QLineEdit,keyPressEventDid you override the method?
    Well,keyPressEventSince the process is being intercepted by overriding, the original process is not performed.
    Let's call the original processing properly.

    #coding: utf-8
    import sys
    from PySide2.QtWidgets import *
    from PySide2.QtCore import *
    from PySide2.QtGui import *
    class MyLineEdit (QLineEdit):
        def keyPressEvent (self, event):
            print ('keyPressEvent, key =% s.'% Event.key (), flush = True)
            if event.key () == Qt.Key_Return:
                print ('enter.', flush = True)
            else: else:
                # If you do not handle it, execute the process of the override source
                super (). keyPressEvent (event)
    if __name__ =='__main__':
        app = QApplication (sys.argv)
        window = MyLineEdit ()
        window.show ()
        sys.exit (app.exec_ ())