[SOLVED] QMAP



  • Hi,
    I have an application with an object that has as member a QMap<QString,int>
    I have defined basic functions to add values/keys and I have defined a function print to qDebug() the QMap.

    The problem is that when I call Object.print(); which is a simple qDebug() << map
    I see that instead to print the map I print "QMap()". If I call the same function Object.print() just after I set the key/Values....I see the full map with the right data. I don't see any errors...or warnings.


  • Moderators

    Are you sure that the map is not empty when print the first time?



  • @koahnig
    yes you are write the MAP is empty.
    Let me explain better the issue. I have an object that contains a list of objects2 that contains a list of object3. The objects3 has as member the Map.

    In my algorithm I need to find a specific Object3 and initialize the map.
    The object/objects2/objects3 are already created (maps are empty).

    so...I am able to find the Object3 and the pointer to the Object3. I do update the Object3 with the map, then I use a function cal Object3.print()...to verify the contents is correct(It Is). I do that in a for(for(for))) loop...for all the maps/Objects3. When I finish I ask to print all the Object3 and the result is empty...

    for(int j=0;j<(Object.getDbcMessages(i))->msgSignals.size();j++){
    	if((Object.getDbcMessages(i))->getSignalsRef(j)->getSigName() == sigName){
    		QMap<quint64,QString> myMap;
    		CANSignal *s = (Object.getDbcMessages(i))->getSignalsRef(j);
    		for(int k=0; k<(stringL.length()-1);k=k+2){
    			myMap.insert(stringL.at(k).toLongLong(),stringL.at(k+1));
    		}
    		s->mappedValues = myMap;
    		s->printMap();   // this prints the correct Map
    		break;
    	}
    }
    

    ....
    // if I print the map here...it is empty.

    [edit, koahnig] Markdown tags added for code


  • Moderators

    I have added markdown tags to your code to make it more readable. Please check out the markdown tags at the end of the thread.
    You pull a pointer from somewhere with CANSignal *s = (Object.getDbcMessages(i))->getSignalsRef(j);
    You create a map and fill it with information :

            QMap<quint64,QString> myMap;
            for(int k=0; k<(stringL.length()-1);k=k+2){
                myMap.insert(stringL.at(k).toLongLong(),stringL.at(k+1));
            }
    

    You assign the map to an instance of a structure you have a pointer to s->mappedValues = myMap;
    Since you can print immediately after the assignment, so far all is most likely ok.

    The question is what the pointer is?
    My guess is that you are pulling a new pointer when you print again. Obviously you obtain the pointer for information from a DB. However, most likely your pointer is a simple pointer for an assembly of the information, but not to the content of the DB.
    Therefore, you assign the information to the structure, but this is immediately destroyed. Probably you need to have an update to the DB for storing the information also in the DB.

    Note: This is a bit crystal ball reading and may not be vary accurate.



  • Thanks, I will add markdown tags next time, sorry about that.

    I understand it is difficult without the complete code, but there are too many lines...

    I am trying to debug more in deep, there is something basic I am missing for sure...I will post updates, thanks for now.


  • Moderators

    Try this one

    for(int j=0;j<(Object.getDbcMessages(i))->msgSignals.size();j++){
        if((Object.getDbcMessages(i))->getSignalsRef(j)->getSigName() == sigName){
            QMap<quint64,QString> myMap;
            for(int k=0; k<(stringL.length()-1);k=k+2){
                myMap.insert(stringL.at(k).toLongLong(),stringL.at(k+1));
            }
            CANSignal *s = (Object.getDbcMessages(i))->getSignalsRef(j);
            s->mappedValues = myMap;
            s->printMap();   // this prints the correct Map
            CANSignal *s_another = (Object.getDbcMessages(i))->getSignalsRef(j);
            s_another->printMap();   // this prints the correct Map
            break;
        }
    }
    

    when my assumption is correct, the first printMap will work, but the second will not.



  • @koahnig

    Thanks for taking the time to look into this.
    I tried your code, both prints work fine.
    Now, I use this functions to get the pointer...

    CANSignal* CANMsg::getSignalsRef(int i) {
        return &(msgSignals[i]);
    }
    

    msgSignals is a QList<CANSignal>

    Is there anything wrong to get the pointer to a QList element in this way? I do not have compilation errors.

    Thanks


  • Moderators

    I think there is the issue.
    First question why are you using a pointer there? You could do the same with a reference.
    However, also the reference may fail later on. This depends completely on what has been implemented in order obtain the pointer to the list. When the list respectively its location is different from call to call. For whatever reason it seems to be identical when you call it immediately twice.

    I am still thinking that you do not update the original structure, but only a copy somewhere. However, it is hard for me to prove without seeing the whole.

    Another possible check for you might be:

    for(int j=0;j<(Object.getDbcMessages(i))->msgSignals.size();j++){
        if((Object.getDbcMessages(i))->getSignalsRef(j)->getSigName() == sigName){
            QMap<quint64,QString> myMap;
            for(int k=0; k<(stringL.length()-1);k=k+2){
                myMap.insert(stringL.at(k).toLongLong(),stringL.at(k+1));
            }
            CANSignal *s = (Object.getDbcMessages(i))->getSignalsRef(j);
            s->mappedValues = myMap;
            s->printMap();   // this prints the correct Map
        }
    }`
    for(int j=0;j<(Object.getDbcMessages(i))->msgSignals.size();j++){
        if((Object.getDbcMessages(i))->getSignalsRef(j)->getSigName() == sigName){
            CANSignal *s_another = (Object.getDbcMessages(i))->getSignalsRef(j);
            s_another->printMap();   // this may not print the correct Map
            break;
        }
    }
    


  • I am totally with you, I am sure I do not update the original structure. Originally I was not using the pointers...I changed the code just because I had this problem and I wanted to make sure I was pointing to the right structure...which seems not to be the case even using the pointers..

    I think there is something more fundamental that I am missing...and understand you can't help more without the project...I will try other solutions and update this post...

    thanks for now.


  • Moderators

    You are the only one who should be in the perfect position to understand your code.

    You probably have to go through your code line by line with the debugger to get hold of the issue. Or you have to comment out things between working and not working sections. Unfortunately, both methods are rather painful.

    Good luck anyway.



  • @koahnig

    Ok , sometimes I feel really stupid...all the functions implemented were working correctly. The map has always been stored into the structure but...the second print function was not working in the right way...Still trying to understand why...


  • Moderators

    Unfortunately such things are happening to everyone too often ;)

    Obviously, you do the access to the second print not correctly at all respectively the framework has changed when you arrive there.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.