Results 1 to 2 of 2

Thread: XML, read file, string to enum.....

  1. #1
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default XML, read file, string to enum.....

    Hello everybody,
    i have an xml file which i read during start-up and write when the programm is quit.
    The dataset is read into a qmap during parsing. Two parameters i read through the process are form typ enum.


    Qt Code:
    1. ///*! Enum for xxx names*/
    2. typedef enum {
    3. x1 = 0, /***/
    4. x3, /***/
    5. x8, /***/
    6. undefined_xxNames /***/
    7. }EnumxxNames;
    8. QString getEnumxxNames(EnumxxNames element);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QString graphicsEnum::getEnumxxNames(EnumxxNames element)
    2. {
    3. switch(element)
    4. {
    5. case x1:
    6. return QString("x1");
    7. case x3:
    8. return QString("x3");
    9. case x8:
    10. return QString("x8");
    11. default:
    12. return QString::number(element);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    in xml parser

    Qt Code:
    1. qDebug()<<readItem(xx,getStringXMLItem(Properties),getStringXMLItemProperties(xxName));
    To copy to clipboard, switch view to plain text mode 
    Note readItem gives back a Qstring -->
    reads out x1, x3,x8

    if i want to conert that back to the enum using static cast

    Qt Code:
    1. qDebug()<<getEnumxxNames(static_cast<EnumxxNames>(readItem(xx,getStringXMLItem(Properties),getStringXMLItemProperties(xxName)));
    To copy to clipboard, switch view to plain text mode 
    reads out x8

    why is that...what is wrong here....
    Is there a other way to get this done ?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: XML, read file, string to enum.....

    You cannot cast a string to an integer or enum.
    Just like for the conversion in the other direction you'll need a function that understands the enum and its string representation.
    Something like

    Qt Code:
    1. EnumxxNames getEnumValue(const QString &element)
    2. {
    3. if (element == "x1") return x1;
    4. if (element == "x3") return x3;
    5.  
    6. return static_cast<EnumxxNames>(element.toInt()); // actually should check the result of toInt before casting
    7. }
    To copy to clipboard, switch view to plain text mode 

    Alternatively you can do the string -> value mapping with a map or hash that uses the string representation as the keys and the respective enum value as the values.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Speerfish (14th April 2014)

Similar Threads

  1. Replies: 3
    Last Post: 8th June 2011, 07:36
  2. setText using a string read from file
    By Splatify in forum Newbie
    Replies: 3
    Last Post: 28th October 2010, 16:39
  3. converting of string to enum
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2008, 17:31
  4. read input string --> text mode
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2007, 19:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.