PDA

View Full Version : How to store current selection from QcomboBox in a Variable



Angel_Snypz
25th September 2012, 23:34
Hi,

I'm using a UI i created using the QtDesigner with several Comboboxes in it and am wondering how to get the selected item in the combobox to store in a variable.

by this i mean the user selects an option in one of the comboboxes, i need to take the users selection (in my case a few of the options are Chiron,Cyclops,aesir) and then store the options they selected somewhere for me to use for calculations later with data i have read from a .xls file using xlrd.

looking around i have seen currentIndexChanged(int) to send a signal to a slot with the index of the option the user selected, however i am unsure of how to implement this to store into a variable or other container.

Thanks in Advance
-Angel

ChrisW67
26th September 2012, 05:20
QComboBox::currentIndex() and QComboBox::currentText() give you the entry number and text respectively of the currently selected item in the combo box (if there is one). The signal you named tells you when that changes. So you want something like:


private:
int m_diety;

...
m_deity = ui->deityCombo->currentIndex();
// or
m_deity = ui.deityCombo->currentIndex();
// or
m_deity = deityCombo->currentIndex();
// depending on how you have incorporated the designer UI

BalaQT
26th September 2012, 08:26
as Chrish said,
use QComboBox::currentText() to get the stored text in QComboBox.


by this i mean the user selects an option in one of the comboboxes, i need to take the users selection (in my case a few of the options are Chiron,Cyclops,aesir) and then store the options they selected somewhere for me to use for calculations later with data i have read from a .xls file using xlrd.


If u want to do some actions based on user selection,use activated(QString) signal


void QComboBox::activated ( const QString & text ) [signal]
This signal is sent when the user chooses an item in the combobox. The item's text is passed. Note that this signal is sent even when the choice is not changed. If you need to know when the choice actually changes, use signal currentIndexChanged().


hope it helps
Bala