Jun-19-2024, 02:01 PM
Hello, Forum.
I'm writing a form to insert new records on a relational database with two tables: one with customers data and the other with the contacts data.
This is a mockup of the table schemas:
I'm writing a form to insert new records on a relational database with two tables: one with customers data and the other with the contacts data.
This is a mockup of the table schemas:
# Customers id PK name VARCHAR # Contacts id PK customer_id FK name VARCHARI managed to correctly display a
QTableView
of the Contacts
table using a QSqlRelationalDelegate
. But, in the new contact form, in order to populate the customer QComboBox
with the appropriate text
and userData
I did this:# Customer input. self.customer_input = QComboBox() model = CustomersModel().model model.setTable("Customers") model.setSort(1, Qt.SortOrder.AscendingOrder) model.select() for row_index in range(model.rowCount()): customer_id = model.record(row_index).value("id") customer_name = model.record(row_index).value("nome") self.customer_input.addItem(customer_name, customer_id) self.customer_input.setCurrentIndex(-1) self.customer_input.setObjectName("Cliente")I works. But I feel like there is a more QTonic way to do this. Is there a better approach for this problem?