View Full Version : converting of string to enum

24th October 2008, 11:15
I am using QextSerialPort class.with my application i am going to configure the port settings of my pc.
to achieve the above tasks i collect the port setting parameters from my gui combo boxes(which are string type) and i converted "string" parameters to "enum" type using below expression

QString portdbs=ui.comboBox_3->currentText();
DataBitsType dbs=(DataBitsType)Enum.Parse(typeof(DataBitsType), portdbs,true);

after this i compiled my application,but am getting below error:

main.cpp:2381: error: `Enum' undeclared (first use this function)
main.cpp:2381: error: (Each undeclared identifier is reported only once for each
function it appears in.)
main.cpp:2381: error: expected primary-expression before ')' token
main.cpp:2381: error: expected `,' or `;' before "Enum"

For avoiding that error i added "using System;" to my application,but surprisingly my edit plus editor is not detecting the "using"keyword(i.e the color is not changing)

please help me on this issue.

Thanks alot to all,

24th October 2008, 13:34
Are these your own enums? Or enums in QextSerialPort? In any case there are different ways you can approach this.

For one, you could use setItemData() on your QComboBox items, and store the int value of the enum there, then call itemData() to retrieve it.

Or if you didn't want to do that, you could create a map with the string as the key and the enum int as the value...etc.

24th October 2008, 16:31
DataBitsType dbs=(DataBitsType)Enum.Parse(typeof(DataBitsType), portdbs,true);

What? Are you trying to mix C# and C++?

See Q_ENUMS() (http://doc.trolltech.com/4.4/qobject.html#Q_ENUMS) and QMetaEnum.