Efog
Efog

Reputation: 1179

QInputDialog.getItem() get item index

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.

Upvotes: 4

Views: 3839

Answers (2)

黃振賢
黃振賢

Reputation: 1

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()) {
    int ind=-1;
    for (int i =0;i<dialog->children().count();i++)
    {
        qDebug()<<dialog->children().at(i);
        QComboBox *combox = qobject_cast<QComboBox*>(dialog->children().at(i));
        if (combox)
        {
            ind = combox->currentIndex(); // index of select
        }

    }
}

You can try it. Is work for me.

Upvotes: 0

Jablonski
Jablonski

Reputation: 18524

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);

Upvotes: 4

Related Questions