Append struct into nested QList don't work [SOLVED]



  • Hello,

    This does not work, what am I doing wrong?

    I appreciate the help,

    regards,

    @
    struct StructA
    {
    QString strStrA;
    unsigned int uintStrA;
    qreal qrlStrA;

    StructA(void)
    {
        uintStrA = 0;
        qrlStrA  = 0;
    }
    

    }

    struct StructB
    {
    QString strStrB;
    unsigned int uintStrB;
    qreal qrlStrB;

    QList<StructA> lstStructA;
    
    StructB(void)
    {
        uintStrB = 0;
        qrlStrB  = 0;
    }
    

    }

    void procedure(void)
    {
    QList<StructB> lstStructB;

    StructB strb;
    StructA stra;
    
    strb.strStrB  = "abc";
    strb.uintStrB = 1;
    strb.qrlStrB  = 2;
    
    stra.strStrA  = "def";
    stra.uintStrA = 3;
    stra.qrlStrA  = 4;
    
    strb.lstStructA.append( stra ); // this works
    lstStructB.append( strb );
    
    StructA structa;
    
    structa.strStrA  = "ghi";
    structa.uintStrA = 5;
    structa.qrlStrA  = 6;
    
    // this does not work, compiling reports
    // "error: C2663: 'QList <T> :: append': 2 overloads have no meaningful
    // conversion to pointer 'this' with [T = StructA]"
    lstStructB.at( lstStructB.size()-1 ).lstStructA.append( structa );
    

    }
    @


  • Moderators

    QList returns const objects. You can't modiy the objects by calling "append" on them. Consider using takeAt() method instead.



  • Hello sierdzio, thanks for reply,

    I apologize, the QList man says:

    bq. QList uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator [] (). On non-const lists, operator [] () returns a reference to the item and can be used on the left side of an assignment:

    in the following line:

    bq. Because QList is implemented as an array of pointers, this operation is very fast (constant time). For read-only access, an alternative syntax is to use at()

    a mistake of mine,

    I ask apologies and thank you for the reply,

    Best regards


  • Moderators

    Hey, no problem. That is one of the reasons this forum exists: to solve issues like this one :)


Log in to reply
 

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