简体   繁体   中英

Changing parameters based on qt combo box

I have a combo box called x_axis_unit in qt with two options; metres and ms. When I change the options, the combo box doesn't take note of the change and stays stuck on metres after I changed the option to ms once I exit the gui. I typed in

 ui->plot_type->setCurrentIndex(1);

but that doesn't set it. So what argument is needed to set the current index to its current value? Also based on the current option I'd like to run a loop that changes another parameter. So if the current text in the combo box is metres then I set a variable called axis to 0 and if it is in ms then I set the variable to 1

// Combo box code

ui->setupUi(this);
ui->x_axis_unit->addItem("metres");
ui->x_axis_unit->addItem("ms");

So how can I set the combo box to ms, it is always on metres.

You could use an enum to store the index for the combo box.

If the enum has class scope you can then change the combo box from any function in the class with "comboBox.setCurrentIndex(enum entry)" like this:

enum comboBoxSelection
{
    eMetres = 0,
    eMS
};

x_axis_unit = new QComboBox(parent);

x_axis_unit->insertItem(eMetres, "Metres");
x_axis_unit->insertItem(eMS, "ms");

x_axis_unit->setCurrentIndex(eMS);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM