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

14th February 2006, 12:38
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:

void kreator::loadData() {
QString filename;
//QFile xmlFile;

// load natures;
ExaltedData::NatureParser parsenat = ExaltedData::NatureParser();
//filename = locate("data", "natures.xml");
QFile xmlFile(filename);
QXmlInputSource source(&xmlFile);
QXmlSimpleReader reader;

reader.setContentHandler( &parsenat );
if (!reader.parse(source)) {
KMessageBox::error(this,("Parse error in natures.xml\n"+parsenat.errorString() + "\nElement Count=")+QString::number(parsenat.count));


and in the header of the data namespace I have

namespace ExaltedData {

static QMap<QString,NatureData> natures;


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?

14th February 2006, 14:12
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.

14th February 2006, 14:28
okay, that now makes sense... how do I make it static across all my units?

14th February 2006, 14:40
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:

namespace ExaltedData {
QMap<QString,NatureData> natures;

and in every file which needs to use it add:

namespace ExaltedData {
extern QMap<QString,NatureData> natures;

14th February 2006, 21:35
That worked like a charm, although it does seem a bit counterintuitive.

Thanks :)