Search code examples
pythonpyqt5qlineedit

Issues with QLineEdit


In this piece of code, I have two line edits and a push button in the middle. In the beginning, the cursor is on the first line edit. I want to write a function (switchLineEdit) that would switch between the two meaning that once you would click the button after starting the program, it would move the cursor to the second line edit (the second line edit would be clicked simply) and vice versa. Here's the code:

import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWidget

# ----------------------------------------------------

def switchLineEdit():
    pass

# ----------------------------------------------------

app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Line Edit")
window.setFixedWidth(280)
window.setFixedHeight(260)
window.move(100, 50)

window.setStyleSheet(
    "background-color: rgb(208, 208, 208);"
)

lineEdit1 = QtWidgets.QLineEdit(window)
lineEdit1.setGeometry(40, 30, 200, 40)
lineEdit1.setStyleSheet(
    "background-color: rgb(255, 255, 255);"
    "font: 11pt \'Trebuchet MS\';"
    "border: 0px solid;"
    "border-radius: 20px;"
    "padding-left: 7px;"
    "padding-right: 7px;"
)
lineEdit1.setMaxLength(30)

button = QtWidgets.QPushButton(window)
button.setGeometry(110, 100, 60, 60)
button.setStyleSheet(
    "border: 0px solid;"
    "background-color: rgb(24, 92, 36);"
    "border-radius: 30px;"
)

lineEdit2 = QtWidgets.QLineEdit(window)
lineEdit2.setGeometry(40, 190, 200, 40)
lineEdit2.setStyleSheet(
    "background-color: rgb(255, 255, 255);"
    "font: 11pt \'Trebuchet MS\';"
    "border: 0px solid;"
    "border-radius: 20px;"
    "padding-left: 7px;"
    "padding-right: 7px;"
)
lineEdit2.setMaxLength(30)

lineEdit1.returnPressed.connect(lambda: print(lineEdit1.text()))
button.clicked.connect(switchLineEdit)

window.show()
sys.exit(app.exec_())

Can anyone help me write the function?


Solution

  • The logic is to verify that QLineEdit had the focus using hasFocus() and set it to the other using setFocus(). The QPushButton can add complexity since when it is clicked it will have the focus making it difficult to recognize who had the focus so a possible solution is to set Qt::NoFocus as focusPolicy so that it does not acquire the focus.

    import sys
    
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication, QLineEdit, QPushButton, QVBoxLayout, QWidget
    
    
    class Widget(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setAttribute(Qt.WA_StyledBackground)
            self.setStyleSheet(
                """
            QWidget{
                background-color: rgb(208, 208, 208);
            }
            QLineEdit{
                background-color: rgb(255, 255, 255);
                font: 11pt 'Trebuchet MS';
                border: 0px solid;
                border-radius: 20px;
                padding-left: 7px;
                padding-right: 7px;
            }
            QPushButton{
                border: 0px solid;
                background-color: rgb(24, 92, 36);
                border-radius: 30px;
            }
                """
            )
    
            self.lineEdit1 = QLineEdit(maxLength=30)
            self.lineEdit1.setFixedSize(200, 40)
    
            self.lineEdit2 = QLineEdit(maxLength=30)
            self.lineEdit2.setFixedSize(200, 40)
    
            button = QPushButton(focusPolicy=Qt.NoFocus)
            button.setFixedSize(60, 60)
    
            lay = QVBoxLayout(self)
            lay.addWidget(self.lineEdit1, alignment=Qt.AlignCenter)
            lay.addWidget(button, alignment=Qt.AlignCenter)
            lay.addWidget(self.lineEdit2, alignment=Qt.AlignCenter)
    
            self.setFixedSize(280, 260)
    
            button.clicked.connect(self.handle_clicked)
    
        def handle_clicked(self):
            if self.lineEdit1.hasFocus():
                self.lineEdit2.setFocus()
            elif self.lineEdit2.hasFocus():
                self.lineEdit1.setFocus()
    
    
    def main():
        app = QApplication(sys.argv)
        window = Widget()
        window.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()