Results 1 to 5 of 5

Thread: How to Initialise Static QMap?

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default How to Initialise Static QMap?

    Hi All,

    I'm hoping this is not too obvious a question. A static QList can be initialised outside of any method:
    Qt Code:
    1. class A {
    2. public:
    3. A();
    4. ...
    5. private:
    6. static QList<int> m_list;
    7. static QMap<int,QString> m_map;
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    and in the cpp file
    Qt Code:
    1. A::A() { }
    2. ...
    3. QList<int> A::m_list = QList<int>() << 1 << 2 << 4;
    4. ...
    To copy to clipboard, switch view to plain text mode 
    but I cannot see a similar way to initialise the QMap. Can it be done or is it a job for the constructor?

    Regards,
    Chris W

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Initialise Static QMap?

    You have to do something like this:
    Qt Code:
    1. m_map.insert(1, "Yo");
    2. m_map.insert(2, "Yes");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to Initialise Static QMap?

    Thanks boudie. That way of loading the map would have to happen inside the class constructor or a method.

  4. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to Initialise Static QMap?

    @ ChrisW67, Did you figure it out how it's done ? I bumped here while searching for the same.
    Thank you.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to Initialise Static QMap?

    One way is to write a custom class to do so, but yes it is not obvious
    Qt Code:
    1. // file.h
    2. template <typename Key, typename Value>
    3. class Map : public QMap<Key, Value>
    4. {
    5. public:
    6. Map() : QMap<Key, Value>() { }
    7.  
    8. Map<Key, Value>& operator()(const Key & key, const Value & value)
    9. {
    10. this->insert(key, value);
    11. return *this;
    12. }
    13. };
    14.  
    15. class A
    16. {
    17. public:
    18. A()
    19. {
    20. qDebug() << m_list;
    21. qDebug() << m_map;
    22. }
    23. private:
    24. static QList<int> m_list;
    25. static QMap<int, QString> m_map;
    26. };
    27.  
    28. // file.cpp file
    29. QList<int> A::m_list = QList<int>() << 1 << 2 << 4;
    30. QMap<int, QString> A::m_map = Map<int, QString>()(1, QString("1"))(2, QString("2"))(4, QString("4"));
    To copy to clipboard, switch view to plain text mode 

    You could also do it with << operator.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    rawfool (20th June 2013)

Similar Threads

  1. Static Init of a QMap
    By Scorp1us in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2010, 15:08
  2. QMap and static problem
    By longtrue in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 23:14
  3. Qmap problem on a static lib
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2007, 12:11
  4. Initialise toolbar in a different area
    By georgie in forum Qt Programming
    Replies: 4
    Last Post: 4th May 2006, 03:49
  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.