Question

I want to keep a static location to write to, due to multiple instantiations. I want to be able to add to the list from each instantiation. But only the first one is kept.
Not sure what to do?

Works for pointer of type char. But when I tried converting QStringList to pointer I just kept getting an error: Segmentation Fault.

*.h


QStringList msgList;

*.cpp


fncInit(){
    static QStringList MessageList;
    msgList = MessageList;//keep the location constant for all new instantiations
}

fncBuild(QString strMessage){
    MessageList.append(strMessage); //if I use a pointer QStringList through out, I get Segmentation Fault.
}

fncPrintf(){
    for(int i; i < msgList.count(); i++){
        printf("%d)    %s", i, msgList.at(i).toStdString().c_str());
    } 
}
Was it helpful?

Solution

You should declare your msgList static in the header. Don't forget to add QStringList CLASS_NAME::msgList; in your .cpp file.

And in your fncInit you assign empty QStringList to msgList, but anyway correct way is above.

OTHER TIPS

In your header file, you need the following code:

static QStringList msgList;

Which will make this static variable available to all functions including that header file.

When you're declaring the msgList static within a function, you're creating a local instance of it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top