Search code examples
pythonpysideqlineeditqpushbutton

Use one QPushButton with two QLineEdits, depending on last focus


I have a window, which has two QLineEdits in it. They are Line 1 and Line 2. I have a Backspace QPushButton which is to be pressed. I want some code which, when the backspace is pressed, will delete the text from the desired QLineEdit. This is to be done based on which one is focused at the time.

I understand that currently my code will backspace line1, however I want it to delete whichever line edit most recently had focus (i.e. if line1 was selected before backspace, it will get backspaced, if line 2 was the last in focus, then it will be backspaced).

I'm thinking it requires an if statement or 2, not sure though. How do I choose which line edit is deleted based on which one last had focus?

from PySide import QtGui, QtCore
from PySide.QtCore import*
from PySide.QtGui import*

class MainWindow(QtGui.QMainWindow):                                                                                    #The Main Window Class Maker

    def __init__(self,):
        QtGui.QMainWindow.__init__(self)
        QtGui.QApplication.setStyle(('cleanlooks'))

        mfont = QFont()
        mfont.setFamily("BankGothic LT")
        mfont.setPointSize(40)
        mfont.setBold(True)
        xfont = QFont()
        xfont.setFamily("BankGothic LT")
        xfont.setPointSize(40)
        xfont.setLetterSpacing(QFont.AbsoluteSpacing, 15)

        self.line1 = QLineEdit("Line 1", self)
        self.line1.setFixedSize(460, 65)
        self.line1.setFont(xfont)
        self.line1.move(10,10)

        self.line2 = QLineEdit("Line 2", self)
        self.line2.setFixedSize(460, 65)
        self.line2.setFont(xfont)
        self.line2.move(10,200)


        #BackSpace button
        back = QPushButton("BackSpace", self)
        back.move(100,100)
        back.setFixedSize(300,75)
        back.setFont(mfont)
        back.clicked.connect(self.line1.backspace)


if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.setWindowTitle("BackSpace")
    window.resize(480, 400)
    window.setMaximumSize(480,400)
    window.setMinimumSize(480,400)
    window.show()
    sys.exit(app.exec_())

Solution

  • Ok guys, so I kept working on this in order to find a useful situation, where one would use this sort of functionality. Say you were trying to create a login form for a touch screen, but you had no onscreen keyboard installed, you can 'create' one. This is what the intended use was for.

    I've sort of tested this, and fixed any bugs I saw, but hey, feel free to use it. I noticed that heaps of examples existed for calculators, but no real examples existed for Keypad or Numberpad entry. Enjoy!

        from PySide import QtGui, QtCore
        from PySide.QtCore import*
        from PySide.QtGui import*
    
        class MainWindow(QtGui.QMainWindow):                                                                                    #The Main Window Class Maker
    
        def __init__(self,):
            QtGui.QMainWindow.__init__(self)
            QtGui.QApplication.setStyle(('cleanlooks'))
    
            U = QLabel("U:", self)
            U.move(10,10)
            P = QLabel("P:", self)
            P.move(10,50)
    
            self.line1 = QLineEdit("", self)
            self.line1.move(20,10)
            self.line1.setReadOnly(True)
    
            self.line2 = QLineEdit("", self)
            self.line2.move(20,50)
            self.line2.setReadOnly(True)
            self.line2.setEchoMode(QLineEdit.Password)
    
            #PushButtons
            back = QPushButton("<", self)
            back.move(100,80)
            back.setFocusPolicy(QtCore.Qt.NoFocus)
            back.setFixedSize(20,20)
            one = QPushButton('1', self)
            one.move(10,80)
            one.setFocusPolicy(QtCore.Qt.NoFocus)
            one.setText("1")
            one.setFixedSize(20,20)
            two = QPushButton('2', self)
            two.move(40,80)
            two.setFocusPolicy(QtCore.Qt.NoFocus)
            two.setFixedSize(20,20)
            three = QPushButton('3', self)
            three.move(70,80)
            three.setFocusPolicy(QtCore.Qt.NoFocus)
            three.setFixedSize(20,20)
            four = QPushButton('4', self)
            four.move(10,110)
            four.setFocusPolicy(QtCore.Qt.NoFocus)
            four.setFixedSize(20,20)
            five = QPushButton('5', self)
            five.move(40,110)
            five.setFocusPolicy(QtCore.Qt.NoFocus)
            five.setFixedSize(20,20)
            six = QPushButton('6', self)
            six.move(70,110)
            six.setFocusPolicy(QtCore.Qt.NoFocus)
            six.setFixedSize(20,20)
            seven = QPushButton('7', self)
            seven.move(10,140)
            seven.setFocusPolicy(QtCore.Qt.NoFocus)
            seven.setFixedSize(20,20)
            eight = QPushButton('8', self)
            eight.move(40,140)
            eight.setFocusPolicy(QtCore.Qt.NoFocus)
            eight.setFixedSize(20,20)
            nine = QPushButton('9', self)
            nine.move(70,140)
            nine.setFocusPolicy(QtCore.Qt.NoFocus)
            nine.setFixedSize(20,20)
            zero = QPushButton('0', self)
            zero.move(100,140)
            zero.setFocusPolicy(QtCore.Qt.NoFocus)
            zero.setFixedSize(20,20)
            enter = QPushButton("E", self)
            enter.move(100,110)
            enter.setFixedSize(20,20)
            enter.setFocusPolicy(QtCore.Qt.NoFocus)
    
    
                    #click Handles
            def handleBackspace():
                backh = QtGui.qApp.focusWidget()
                if backh is self.line1 or backh is self.line2:
                    backh.backspace()
            def handleZero():
                zeroh = QtGui.qApp.focusWidget()
                if zeroh is self.line1:
                    zeroh.setText((self.line1.text()+str('0')))
                else:
                    zeroh.setText(self.line2.text()+str('0'))
            def handleOne():
                oneh = QtGui.qApp.focusWidget()
                if oneh is self.line1:
                    oneh.setText(self.line1.text()+str('1'))
                else:
                    oneh.setText(self.line2.text()+str('1'))
            def handleTwo():
                twoh = QtGui.qApp.focusWidget()
                if twoh is self.line1:
                    twoh.setText(self.line1.text()+str('2'))
                else:
                    twoh.setText(self.line2.text()+str('2'))
            def handleThree():
                threeh = QtGui.qApp.focusWidget()
                if threeh is self.line1:
                    threeh.setText(self.line1.text()+str('3'))
                else:
                    threeh.setText(self.line2.text()+str('3'))
            def handleFour():
                fourh = QtGui.qApp.focusWidget()
                if fourh is self.line1:
                    fourh.setText(self.line1.text()+str('4'))
                else:
                     fourh.setText(self.line2.text()+str('4'))
            def handleFive():
                fiveh = QtGui.qApp.focusWidget()
                if fiveh is self.line1:
                    fiveh.setText(self.line1.text()+str('5'))
                else:
                    fiveh.setText(self.line2.text()+str('5'))
            def handleSix():
                sixh = QtGui.qApp.focusWidget()
                if sixh is self.line1:
                    sixh.setText(self.line1.text()+str('6'))
                else:
                    sixh.setText(self.line2.text()+str('6'))
            def handleSeven():
                sevenh = QtGui.qApp.focusWidget()
                if sevenh is self.line1:
                    sevenh.setText(self.line1.text()+str('7'))
                else:
                    sevenh.setText(self.line2.text()+str('7'))
            def handleEight():
                eighth = QtGui.qApp.focusWidget()
                if eighth is self.line1:
                    eighth.setText(self.line1.text()+str('8'))
                else:
                    eighth.setText(self.line2.text()+str('8'))
            def handleNine():
                nineh = QtGui.qApp.focusWidget()
                if nineh is self.line1:
                    nineh.setText(self.line1.text()+str('9'))
                else:
                    nineh.setText(self.line2.text()+str('9'))
    
            #Click Conditions
            self.connect(enter, SIGNAL("clicked()"), self.close)
            zero.clicked.connect(handleZero)
            nine.clicked.connect(handleNine)
            eight.clicked.connect(handleEight)
            seven.clicked.connect(handleSeven)
            six.clicked.connect(handleSix)
            five.clicked.connect(handleFive)
            four.clicked.connect(handleFour)
            three.clicked.connect(handleThree)
            two.clicked.connect(handleTwo)
            one.clicked.connect(handleOne)
            back.clicked.connect(handleBackspace)
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = MainWindow()
        window.setWindowTitle("LoginWindow")
        window.resize(130, 180)
        window.setMaximumSize(130, 180)
        window.setMinimumSize(130, 180)
        window.show()
        sys.exit(app.exec_())