Results 1 to 5 of 5

Thread: static QMap doesn't seem to remain static (or at least keeps becoming empty)

  1. #1
    Join Date
    Feb 2006
    Location
    Enschede, NL, EU
    Posts
    19
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default static QMap doesn't seem to remain static (or at least keeps becoming empty)

    I'm trying to get my app in development to load a bunch of static data on startup, for which i'm using a namespaced static QMap, but every time I call on the contents of it it turns out to be empty.

    This is in the main file:
    Qt Code:
    1. void kreator::loadData() {
    2. QString filename;
    3. //QFile xmlFile;
    4.  
    5. // load natures;
    6. ExaltedData::NatureParser parsenat = ExaltedData::NatureParser();
    7. //filename = locate("data", "natures.xml");
    8. filename="/home/shadow/project/kreator/src/data/natures.xml";
    9. QFile xmlFile(filename);
    10. QXmlInputSource source(&xmlFile);
    11.  
    12. reader.setContentHandler( &parsenat );
    13. if (!reader.parse(source)) {
    14. KMessageBox::error(this,("Parse error in natures.xml\n"+parsenat.errorString() + "\nElement Count=")+QString::number(parsenat.count));
    15. };
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    and in the header of the data namespace I have
    Qt Code:
    1. namespace ExaltedData {
    2.  
    3. static QMap<QString,NatureData> natures;
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    I've verified that the parse works correctly and during the parse natures has 21 elements, but when I reference it again later it's got 0 elements again.

    Anyone able to tell me what I'm doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static QMap doesn't seem to remain static (or at least keeps becoming empty)

    Quote Originally Posted by ReilenBlaeyze
    and in the header of the data namespace I have
    If you have this in the header, then you probably have a different natures variable in every compilation unit.
    Last edited by jacek; 19th February 2006 at 01:32. Reason: fixed typos

  3. #3
    Join Date
    Feb 2006
    Location
    Enschede, NL, EU
    Posts
    19
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: static QMap doesn't seem to remain static (or at least keeps becoming empty)

    okay, that now makes sense... how do I make it static across all my units?

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

    Default Re: static QMap doesn't seem to remain static (or at least keeps becoming empty)

    Why make it static? Make it global but non-static. If you make it static, it'll only be available in one compilation unit and you'll need to create two global functions to access it from other compilation units. Try this instead:

    In one of implementation files:
    Qt Code:
    1. namespace ExaltedData {
    2. QMap<QString,NatureData> natures;
    3. }
    To copy to clipboard, switch view to plain text mode 

    and in every file which needs to use it add:
    Qt Code:
    1. namespace ExaltedData {
    2. extern QMap<QString,NatureData> natures;
    3. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2006
    Location
    Enschede, NL, EU
    Posts
    19
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: static QMap doesn't seem to remain static (or at least keeps becoming empty)

    That worked like a charm, although it does seem a bit counterintuitive.

    Thanks

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12

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.