Populating fields in .ui file from QT Designer

Joined
Apr 7, 2025
Messages
1
Reaction score
0
While I have many years of programming experience, I am new to Python.

I have designed a .ui file using QT Designer which I want to use to maintain a database. My Python program loads and displays the .ui file. Now I am looking to load the data from the database into the fields in the .ui and present them but I can’t for the life of me figure out how to populate the methods in the .ui file with the data from the database. Any help would be greatly appreciated.

Here’s a sample of the code in the .ui file for one of the fields


Code:
<widget class="QLineEdit" name="code">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>50</y>
      <width>60</width>
      <height>20</height>
     </rect>
    </property>
    <property name="sizePolicy">
     <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
    <property name="minimumSize">
     <size>
      <width>60</width>
      <height>20</height>
     </size>
    </property>
    <property name="maximumSize">
     <size>
      <width>60</width>
      <height>20</height>
     </size>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="maxLength">
     <number>6</number>
    </property>
    <property name="alignment">
     <set>Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter</set>
    </property>
   </widget>

Here’s the Python code to display the .ui file and read the database.

Code:
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,
    )

loader = QUiLoader()
basedir = os.path.dirname(__file__)
    
class MainUI:
    def __init__(self):
        super().__init__()
        self.ui = loader.load(
           os.path.join(basedir, "customer.ui"), None
            )
        self.ui.setWindowTitle("Integrity Customer Maintenance")
        self.db = QSqlDatabase("QSQLITE")
        self.db.setDatabaseName(os.path.join(basedir, "integrity.db"))
        self.db.open()
        self.connection_obj = sqlite3.connect('integrity.db')
        self.cursor_obj = self.connection_obj.cursor()
        self.sys_cust_code = "mayhew"
        #self.sys_cust_code = "333333"
        self.cursor_obj.execute('SELECT * FROM customer WHERE cust_code = ?', (self.sys_cust_code,))
        self.customer_rec = self.cursor_obj.fetchone()  # holds data from customer database
        self.code = self.customer_rec[0]
        self.name = self.customer_rec[1]
        print ("Key = ",self.sys_cust_code)
        print(self.customer_rec)
        print("")
        print("Printing Individual Fields")
        print("Code = ", self.customer_rec[0])
        print("Name = ", self.customer_rec[1])
        self.ui.code
        self.ui.show()
        # self.model = QSqlTableModel(db=self.db)
        self.mapper = QDataWidgetMapper() # One for all widgets
app = QApplication(sys.argv)
ui = MainUI()
app.exec()
 
Joined
Apr 8, 2025
Messages
2
Reaction score
0
The missing piece is connecting your Python code to the UI elements loaded from the .ui file and then using that connection to populate the fields with data from your database.

Python:
import os
import sys
import sqlite3
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtCore import QFile

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        ui_file = QFile("your_ui_file.ui")  # Replace with your .ui file name
        ui_file.open(QFile.ReadOnly)
        loader = QUiLoader()
        self.ui = loader.load(ui_file)
        ui_file.close()

        self.setCentralWidget(self.ui)       
        self.codeLineEdit = self.ui.findChild(QLineEdit, "code") #Access the QLineEdit widget with the name "code"
        # Access other widgets similarly:
        # self.anotherWidget = self.ui.findChild(WidgetType, "widgetName")

        # Connect to the database and load data (see next step)
        self.loadData()

    def loadData(self):
        # ... (database connection and data retrieval logic - see next step)
        pass

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

For more complex database interactions, consider using Qt's Model-View architecture with QTableView or QTreeView widgets. This approach provides a more structured and efficient way to manage and display large datasets. QSqlQueryModel or QSqlTableModel would be excellent choices for handling the data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top