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()
 

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,341
Messages
2,571,807
Members
48,646
Latest member
WyattIrvin

Latest Threads

Top