Python Forum
[PyQt] Populate QComboBox with "text" and "userData" from database.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Populate QComboBox with "text" and "userData" from database.
#1
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:
# Customers
id PK
name VARCHAR

# Contacts
id PK
customer_id FK
name VARCHAR
I 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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] populate a QListWidget devilonline 1 1,792 Apr-10-2023, 02:52 AM
Last Post: deanhystad
  [PyQt] [Solved]Populate ComboBox with for loop? Extra 3 2,411 Jul-31-2022, 09:01 PM
Last Post: Extra
  Problems getting tk Combobox contents to populate properly dford 4 4,190 Jan-08-2022, 02:39 PM
Last Post: dford
  [PyQt] How do I display the DB table I will choose from the QComboBox in QTableWidget JokerSob 2 2,451 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Auto populate dictionary with names/values of QT widgets cjh 1 3,147 Mar-23-2021, 02:56 PM
Last Post: deanhystad
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 2,448 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  QComboBox doesn't display selection as long as it has not lost the focus Meaulnes 3 3,464 May-07-2020, 03:42 PM
Last Post: Meaulnes
  QComboBox for PyQt code gvin47 3 2,239 Apr-22-2020, 04:01 PM
Last Post: gvin47
  [PyQt] How to populate a treeview on a GUI with a dictionary mart79 1 8,518 Aug-05-2019, 01:30 PM
Last Post: Denni
  populate list with images and be able to select them ricardons 0 2,208 Jan-11-2019, 03:45 PM
Last Post: ricardons

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020