Search code examples
c++pythonqtpyqtqcombobox

how to send data as hidden to QComboBOX in pyqt


I need to send name and id to QComboBox, But i want to show name and because id is id in dB table, i want to keep as hidden id, but i don’t know how to keep as hidden id. I can name such as:

self.comboBox_2.addItems(list1)

Bu i don't have any idea on hidden data.

My question is , how send data as hidden to QComboBox?


Solution

  • If you have a data model based on QAbstractItemModel which has to columns 'id' and 'name', you can do this:

    enum Columns
    {
        COL_ID,
        COL_NAME
    }
    
    QComboBox *combo = new QComboBox;
    combo->setModel(model);
    combo->setModelColumn(COL_NAME)
    

    If you want to add items to a combobox one by one, you do this:

    QComboBox *combo = new QComboBox;
    combo->addItem(name, id);
    

    Then you can retrieve id value from combobox item using:

    QString id = combo->itemData(index, Qt::UserRole).toString();