Results 1 to 6 of 6

Thread: Comparing a Qstring with a value defined in an enum

  1. #1
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Comparing a Qstring with a value defined in an enum

    Hi, I am recieving a string that defines the function i have to call. To do this I wrote this class:
    Qt Code:
    1. class MiTableClass:public QObject
    2. {
    3. Q_OBJECT
    4. Q_ENUMS(MiTableElements);
    5.  
    6. //This is a singleton class but i deleted the extra code lines for this post.
    7.  
    8. public:
    9. int indexOf(QString);
    10. QString commandNumber(int);
    11.  
    12. enum MiTableElements
    13. {
    14. //Eth commands
    15. AUTOTEST,//0
    16. HALT,//1
    17. STANDBY,//2
    18. SSNORMAL,//3
    19. };//The enum is way longer
    20. };
    21.  
    22. int MiTableClass::indexOf(QString command)
    23. {
    24. const QMetaObject metaObject=m_Instance->staticMetaObject;
    25. int index = metaObject.indexOfEnumerator("MiTableElements");
    26. QMetaEnum metaEnum = metaObject.enumerator(index);
    27. int i=metaEnum.keyToValue(command.toLatin1());
    28. return i;
    29. }
    30.  
    31. QString MiTableClass::commandNumber(int nCmd)
    32. {
    33. const QMetaObject metaObject=m_Instance->staticMetaObject;
    34. int index = metaObject.indexOfEnumerator("MiTableElements");
    35. QMetaEnum metaEnum = metaObject.enumerator(index);
    36. QString str=metaEnum.valueToKey(nCmd);
    37. return str;
    38. }
    To copy to clipboard, switch view to plain text mode 

    and the to use it I do this
    Qt Code:
    1. void HLP_MessageProcessor::run()
    2. {
    3. MiTableClass * mi =MiTableClass::Instance();
    4. lst=message.split(",");
    5.  
    6. //Figuring out the command code
    7. int i=mi->indexOf(lst[0]);
    8. switch(i)
    9. {
    10. case 0://AUTOTEST
    11. AUTOTEST_Function();
    12. break;
    13. case 1://HALT
    14. HALT_Function();
    15. //The lack of break here is on purpouse
    16. case 2://STANDBY
    17. STANDBY_Function();
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    Now the thing is that I will have to add cases inbetween the ones i already have and I would like to be able to write
    Qt Code:
    1. switch(i)
    2. {
    3. case AUTOTEST:
    4. ...
    To copy to clipboard, switch view to plain text mode 
    And i do not know where to declare the enum so I can use it in this way for the switch.

    So far i tried to put it as a global enum, but then the class members that compare the string with the enum value stopped to work.
    Any ideas?
    How to register the global enum inside my class? i'm lost in this topic

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Comparing a Qstring with a value defined in an enum

    enum is a public member of MiTableClass, so this will work:
    Qt Code:
    1. switch(i)
    2. {
    3. case MiTableClass::AUTOTEST:
    4. ...
    To copy to clipboard, switch view to plain text mode 

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

    AndresBarbaRoja (24th March 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Comparing a Qstring with a value defined in an enum

    Furthermore if you declare AUTOTEST_Function() (and all the others) as Q_INVOKABLE (or as slots) you can do:
    Qt Code:
    1. void HLP_MessageProcessor::run()
    2. {
    3. MiTableClass * mi =MiTableClass::Instance();
    4. lst=message.split(",");
    5. QMetaObject::invokeMethod(qPrintable(QString("%1_Function").arg(lst[0])));
    6. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    AndresBarbaRoja (24th March 2011)

  6. #4
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Comparing a Qstring with a value defined in an enum

    Thanks, to both of you. Right now stampede sugestion works fine. And i'll start declaring slots because i like the way the wysota code looks

  7. #5
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Comparing a Qstring with a value defined in an enum

    Just an observation in case someone else reads this.
    The invokeMethod requires the object from which you want to execute the member, so i added "this" to my code so it would compile:

    Qt Code:
    1. void HLP_MessageProcessor::run()
    2. {
    3. MiTableClass * mi =MiTableClass::Instance();
    4. lst=message.split(",");
    5. QMetaObject::invokeMethod(qPrintable(this,QString("%1_Function").arg(lst[0])));
    6. }
    To copy to clipboard, switch view to plain text mode 

    In any case, I declared all functions as private slots, the invokeMethod seems to be calling them (it returns true) but the debugger never stops in the breakpoint inside the called function. Any ideas why?

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Comparing a Qstring with a value defined in an enum

    This works fine for me:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. class Caller : public QObject {
    4. Q_OBJECT
    5. public:
    6. Caller(){}
    7.  
    8. protected:
    9. Q_INVOKABLE void func1() {
    10. qDebug() << Q_FUNC_INFO;
    11. }
    12.  
    13. Q_INVOKABLE void func2() {
    14. qDebug() << Q_FUNC_INFO;
    15. }
    16. };
    17.  
    18. #include "main.moc"
    19.  
    20. int main(int argc, char **argv){
    21. Caller c;
    22. QMetaObject::invokeMethod(&c, argv[1]);
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Called as:
    $ ./invokable func1
    void Caller::func1()
    $ ./invokable falafel
    QMetaObject::invokeMethod: No such method Caller::falafel()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Comparing QString causes the program to crash .
    By ladiesfinger in forum Qt Programming
    Replies: 9
    Last Post: 6th November 2010, 18:17
  2. Best performance when comparing two QTextEdits
    By leenxkewl in forum Qt Programming
    Replies: 3
    Last Post: 16th October 2010, 20:07
  3. Comparing the |(shift+\) char
    By warry in forum Newbie
    Replies: 1
    Last Post: 8th September 2008, 08:05
  4. Problem with comparing two QDateTime objects
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2008, 12:27
  5. Comparing Items In List Box
    By kenny_isles in forum Qt Programming
    Replies: 9
    Last Post: 21st February 2007, 14:06

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.