Important: Please read the Qt Code of Conduct -

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

  • Hello,

    This does not work, what am I doing wrong?

    I appreciate the help,


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

        uintStrA = 0;
        qrlStrA  = 0;


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

    QList<StructA> lstStructA;
        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.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