QLineEdit Text Use

The am having trouble retrieving the text entered into a QLineEdit widget.

The window displays and allows entry.
When the return key is pressed the signal is generated and the slot executed. The code to assign the value of the text of the QLineEdit appears to work, but when the value is printed the data entered is not there and the database select shows the record which is on the database with the key entered was not found.

Here is the code and the output of the execution.

import os
import sys
import sqlite3 
import PySide6
#QUiLoader allows screen created in QT Designer to be loaded
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import QSize
from PySide6.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
from PySide6.QtWidgets import (
    QApplication,
    QComboBox,
    QDataWidgetMapper,
    QDoubleSpinBox,
    QFormLayout,
    QLabel,
    QLineEdit,
    QMainWindow,
    QSpinBox,
    QWidget,
    )

sys_cust_code = ""
loader = QUiLoader()
basedir = os.path.dirname(__file__)

class ci_widget(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = loader.load(
         os.path.join(basedir, "windows/cust_input.ui"), None
            )
        self.ui.setWindowTitle("Input Customer Code")
        sys_cust_code = self.ui.ci_input.text()
        self.ui.ci_input.returnPressed.connect(check_customer)
        self.ui.show()

def check_customer():
    print("Customer Code Entered")
    print("Sys_cust_code = ", sys_cust_code)
    db = QSqlDatabase("QSQLITE")
    db.setDatabaseName(os.path.join(basedir, "databases/integrity.db"))
    db.open()
    connection_obj = sqlite3.connect('databases/integrity.db')
    cursor_obj = connection_obj.cursor()
    cursor_obj.execute('SELECT * FROM customer WHERE cust_code = ?', (sys_cust_code,))
    customer_rec = cursor_obj.fetchone()  # holds data from customer database
    if customer_rec == None:
        sys_cust_found = "N"
    else:
        sys_cust_found = "Y"
    print("sys_cust_found = ",sys_cust_found)
    print("sys_cust_code = ",sys_cust_code)
    #quit()
app = QApplication(sys.argv)
ui = ci_widget()
app.exec()

Thanks in advance for your assistance.
Here is a screenshot of the output.

Hi @WaltMayhew

There are a few issues here:

Firstly, the sys_cust_code is defined in the global scope. To be able to modify that variable, you need to set it as a global variable in the method scopes. If you don’t do that, assignment to sys_cust_code just creates a local variable. However, global variables are to be avoided where possible, and you don’t need that in this case. Rather than defining the check_customer method in the global scope, you can put it on the class (see below).

Secondly, you assign the value to sys_cust_code = self.ui.ci_input.text() in the __init__. But this only assigns the initial value: the variable won’t be updated if it subsequently changes. To do that, you need to re-assign the value every time the returnPressed signal is fired.

Alternatively, and what I’ve done before, is to just retrieve the current value from the input in the check_customer method. That way you always get the current/latest value when the signal fires.


class ci_widget(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = loader.load(
         os.path.join(basedir, "windows/cust_input.ui"), None
            )
        self.ui.setWindowTitle("Input Customer Code")
        
        self.ui.ci_input.returnPressed.connect(self.check_customer)
        self.ui.show()
  
    def check_customer(self):
        sys_cust_code = self.ui.ci_input.text()
        print("Customer Code Entered")
        print("Sys_cust_code = ", sys_cust_code)
        db = QSqlDatabase("QSQLITE")
        db.setDatabaseName(os.path.join(basedir, "databases/integrity.db"))
        db.open()
        connection_obj = sqlite3.connect('databases/integrity.db')
        cursor_obj = connection_obj.cursor()
        cursor_obj.execute('SELECT * FROM customer WHERE cust_code = ?', (sys_cust_code,))
        customer_rec = cursor_obj.fetchone()  # holds data from customer database
        if customer_rec == None:
            sys_cust_found = "N"
        else:
            sys_cust_found = "Y"
        print("sys_cust_found = ",sys_cust_found)
        print("sys_cust_code = ",sys_cust_code)
        #quit()

Lastly, you might want to use the editingFinished signal instead. This fires when the input box loses focus – both by pressing return, or by clicking out of it and into another box.

Hope that helps!

1 Like

Thanks for your feedback. It is very helpful.

I would be interested in discussing your consulting assistance further. If you are interested in that dialogue please email me privately. Thanks.