QInputDialog.getItem()获取项索引

时间:2015-01-03 21:51:47

标签: c++ qt dialog

我有一些列表和QInputDialog。我的列表中可能有相同的字符串,所以我想得到的不是字符串结果而是项目索引。这是真的吗?

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
}

我已尝试使用result(),但它无效。请帮忙。

1 个答案:

答案 0 :(得分:1)

不,QInputDialog没有这样的方法。但是当然这个信息在对话框中有组合框。

您可以访问此组合框吗?

我认为这不是个好主意。查看QInputDialog的{​​{3}}:

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

您可以看到d-pointer隐藏了您的组合框,这是Qt中的常规做法(隐藏实施细节)。 source code

可能是最佳解决方案:

使用QStringList中的More information here.方法。例如:

int index = list.indexOf(item);