L
L
LexArd2015-04-28 11:41:17
Qt
LexArd, 2015-04-28 11:41:17

QComboBox and DB, how to get ID?

I get the ID like this:

....
Region= new QComboBox;   
qry.exec("SELECT * FROM region");
model->setQuery(qry);
Region->setModel(model);
Region->setModelColumn(3);

connect(Region, SIGNAL(currentTextChanged(QString)), SLOT(comboChange(QString)));

void MyWidget::comboChange(QString str)
{
    QSqlQuery qry1;
    qry1.prepare("SELECT region_id FROM region WHERE name=?");
    qry1.addBindValue(str);
    qry1.exec();
    int reg;
    while(qry1.next())
        reg = qry1.value(0).toInt();
    //qDebug() << reg;
    emit toRegion(reg);
}
.......

With my zero experience, it seems to me that this is nonsense, albeit somehow working. Tell me the right way.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Martyanov, 2015-04-28
@LexArd

Use QComboBox::setItemData() when filling the combobox: you can add data invisible to the user to the items. In comboChange QComboBox::itemData() will allow them to be received.

K
kuzia6146, 2015-05-12
@kuzia6146

I would use the QComboBox::currentIndexChanged(int index) signal, and in the slot I would refer to the model in the combobox.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question