kiriloff
kiriloff

Reputation: 26333

QInputDialog action depending on item selection in ComboBox

I have an input dialog (Qt) with three options in a combobox. I would like a different action be launched on the Ok depending on the item which was selected in the combobox. For now, I have

QInputDialog qDialog ;

QStringList items;
items << QString("Choice 1");
items << QString("Choice 2");
items << QString("Choice 3");

qDialog.setOptions(QInputDialog::UseListViewForComboBoxItems);
qDialog.setComboBoxItems(items);
qDialog.setWindowTitle("Choose action");

QObject::connect(&qDialog, SIGNAL(textValueChanged(const QString &)), 
           this, SLOT(onCompute(const QString &)));

qDialog.exec();

The slot oncompute performs a different action depending on the selected item in the combobox... but this is called when the user selects a new item in the box, not on click on ok.

How can i retrieve the item selected on the combo box and perform action on click on Ok ?

Upvotes: 1

Views: 5812

Answers (2)

Ezee
Ezee

Reputation: 4344

Usual way of processing a modal dialog result is this:

QInputDialog qDialog;
...
if (qDialog.exec())
{
   onCompute(qDialog->textValue());
}

You execute a dialog, wait for the result and depending of what butting (ok or cancel) is clicked process the result or not.

Upvotes: 2

kiriloff
kiriloff

Reputation: 26333

Replacing textValueChanged by signal textValueSelected did it.

Upvotes: 2

Related Questions