Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QInputDialog.getItem() get item index

Tags:

c++

dialog

qt

I have some list and QInputDialog. There may be same strings in my list, so i want to get not the string result but item index. Is it real?

QStringList list;
for (Serial serial: serialList->vector) {
    list.append(serial.name);
}

QInputDialog *dialog = new QInputDialog();
bool accepted;
QString item = dialog->getItem(0, "Title", "Label:", list, 0, false, &accepted);
if (accepted && !item.isEmpty()) {
    qDebug() << dialog->?????; //here i want to see index of choosen item
}

I've tried to use result() but it is not working. Help, please.

like image 513
Efog Avatar asked Nov 01 '25 05:11

Efog


1 Answers

No, QInputDialog has no such method. But of course this information has combobox inside dialog.

Can you access this combobox?

I think that it is not good idea. Look at the source code of QInputDialog:

void QInputDialog::setComboBoxItems(const QStringList &items)
{
    Q_D(QInputDialog);
    d->ensureComboBox();
    d->comboBox->blockSignals(true);
    d->comboBox->clear();
    d->comboBox->addItems(items);
    d->comboBox->blockSignals(false);
    if (inputMode() == TextInput)
        d->chooseRightTextInputWidget();
}

As you can see your combobox is hidden by d-pointer and it is normal practice in Qt(hide implementation details). More information here.

Probably the best solution:

Use indexOf() method from QStringList. For example:

int index = list.indexOf(item);
like image 136
Kosovan Avatar answered Nov 03 '25 20:11

Kosovan