[Python] Populating field in .ui file from QT Designer

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    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

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


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

    import os
    import sys
    import sqlite3
    import PySide6

    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.mapper = QDataWidgetMapper()
    app = QApplication(sys.argv)
    ui = MainUI()
    app.exec()

    Continue reading...

Compartilhe esta Página