Results 1 to 3 of 3

Thread: Static Init of a QMap

  1. #1
    Join Date
    Feb 2007
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Static Init of a QMap

    In python, which I know is 100% runtime, I can initialize a dictionary as:

    Qt Code:
    1. d={'a':1, 'b':2}
    To copy to clipboard, switch view to plain text mode 

    so

    d['a']=1
    I have a need to have a constant QMap, and I'd rather not have to call a function to initialize it, since that will break the const protection. I'd like the compiler to make it for me.

    Is there any way to do that qith a QMap? (Or other similar class)

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Static Init of a QMap

    Qt Code:
    1. // i guess with QMap you have to workaround it like this:
    2. static QMap<char, int> myValues() {
    3. QMap<char, int>map;
    4. map.insert('a', 1);
    5. map.insert('b', 2);
    6. return map;
    7. }
    8. static const QMap<char, int> MY_MAP = myValues();
    9.  
    10. // with QList it would be this simple:
    11. static const QList<int> MY_LIST = QList<int>() << 1 << 2;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jun 2010
    Posts
    2

    Default How about this?

    Qt Code:
    1. #include <QMap>
    2. #include <QString>
    3. #include <QPair>
    4.  
    5. template <class Key, class T> class InitializableQMap : public QMap<Key,T>
    6. {
    7. public:
    8. inline InitializableQMap<Key,T> &operator<< (const QPair<Key,T> &t)
    9. { insert(t.first,t.second); return *this; }
    10. };
    11.  
    12. class MyClass
    13. {
    14. public:
    15. static const InitializableQMap<QString, int> MY_STATIC_CONST_MAP;
    16. };
    17.  
    18. const InitializableQMap<QString,int> MyClass::MY_STATIC_CONST_MAP = InitializableQMap<QString,int>()
    19. << QPair<QString,int>("key1",3)
    20. << QPair<QString,int>("key2",2)
    21. ;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10:50
  3. QMap Problem with arguments.
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 12:12
  4. QMap destructor bug
    By Ruud in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2006, 09:08
  5. Replies: 4
    Last Post: 14th February 2006, 21:35

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.