PDA

View Full Version : QComboBox values?



dbrmik
7th January 2009, 11:52
Hi

Using a QComboBox to control values in my application. I have a number of these to set various parameters. For examples 1 have a combobox with values 7,9,11,3,15. I want to send these values to my app class. However only the index is sent, i.e for the first value 7 then value 0 . What is the best way of sending the actual comboxbox value instead of the index to my app class

Best Regards

jpn
7th January 2009, 12:22
I suppose you are using a signal with "int index" parameter. Use one with "QString text" parameter instead and convert it to an integer.

dbrmik
7th January 2009, 13:27
Thanks

Yes I am using signal currentIndexChanged. I could use the overloaded currentIndexChanged(QString) but this sends the QString. I want to pass an integer to my applications member function

Regards

rexi
7th January 2009, 21:11
That's the way it works. The signal currentIndexChanged(int) passes the index to the slot, currentIndexChanged(QString) passes the contents of the combobox at that index as a string. If you want to call a method that takes an int, convert the string with QString::toInt first.

Lesiok
8th January 2009, 07:43
In slot connected to QComboBox::currentIndexChanged(int) call QComboBox::itemData method.

dbrmik
8th January 2009, 12:10
Thanks everyone.

But I want seperation between my application class and gui class, so I dont want to call any Qt functions from my application class. The bottom line is I dont know how to intercept the signal currentIndexChanged in order to convert my Qstring to an integer. Do I need to implement a new signal?

Thanks

rexi
8th January 2009, 16:51
In that case you could connect the currentIndexChanged signal to a slot in your GUI class, convert the QString to an int and call the method from your application class. Of course, instead of calling the method directly you could also define and emit a custom signal which you connect to the slot from your application class. You can find an example in the Qt documentation (http://doc.trolltech.com/4.4/signalsandslots.html#a-small-example).

The question is, why don't you want any Qt code in your application class? If that class has a slot, you have Qt code already in there.

aamer4yu
8th January 2009, 17:33
Why dont u try as rexi said - convert QString to int in your slot ?

Still if u want a slot in ur application class with int parameter, then have a look at using QSignalMapper, though this will be lengthy way !

dbrmik
9th January 2009, 08:32
Thanks Rexi, a custom slot in my GUI class to perform the conversion QString to int , then emit the signal to my application class

Thanks