QComboBox Foreign Key



  • Good day!
    In my application I work with SQLite3 database where 3 tables are stored. One of these table has a foreign key constraints and refers to other 2 tables:

    CREATE TABLE munits
    (
        id INTEGER PRIMARY KEY NOT NULL,
        munit TEXT NOT NULL UNIQUE
    )
    
    CREATE TABLE ctypes
    (
        id INTEGER PRIMARY KEY NOT NULL,
        ctype TEXT NOT NULL UNIQUE
    )
    
    CREATE TABLE sa_kinds
    (
        id INTEGER PRIMARY KEY NOT NULL,
        sa_kind TEXT NOT NULL UNIQUE,
        munit_id  INTEGER NOT NULL,
        ctype_id INTEGER NOT NULL
        FOREIGN KEY(munit_id) REFERENCES munit(id),
        FOREIGN KEY(ctype_id) REFERENCES ctype(id)
    ) 
    

    Tables ctypes and munits are linked with QSqlTableModel, sa_kinds - with QRelationalTableModel.
    Each table is binded with a separate QTableView, but I edit data not in the cells directly - I wrote a separate modal dialog with QLineEdits and QComboBoxes.

    What I want to achieve:

    • Human-readable values in the parent sa_kinds table instead of id codes when it is displayed in QTableView
    • When I edit sa_kinds table's data in the modal dialog, I want QComboBoxes to show human-readable values from munits and ctypes tables in a drop-down lists but, after clicking OK, to update row with corresponding id codes

    Thanks in advance.



  • Can anybody help?


  • Lifetime Qt Champion

    Hi,

    You can set your table model on the combo box so you can choose the text you want and based on that you can retrieve the id you are looking for.



  • @SGaist
    Ok! Huge thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.