Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QList<QObject*> testList, how to access individual QObject.at() values?



  • Hello, I'm new here, wondering if anyone knows how to do this?
    I got a QList <QObject*> list and I'm trying to qDebug print the 1st value? All I'm getting is the pointer address (0z12e3c983820) with this code. Is there no way to retrieve the actual value associated with this address?
    Thank for all the help in advance.

    QList<QObject*> list() const;
    for (int index=0; index < list.size(); index++) {
         QObject *test01 = list.value(0);
         qDebug() << test01;
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What exact value do you want to get ?


  • Lifetime Qt Champion

    Hi
    Normally you would do like
    qDebug() << *test01;

    But qDebug() cannot output the properties of a QObject so it will just
    not compile.

    so you can do

    qDebug() << test01->SomeVar << " " << test01->SomeOtherVar;

    But as @SGaist asks:
    what did you expect it to output ?



  • Hello, thanks for the quick replies. I'm trying to access the text string for each element. So if I'm understanding correctly, QObject will always show address data? Does list.value(0) for example not contain data at that location element? Not pointer address data, actual text data?


  • Lifetime Qt Champion

    @BoGut
    Hi
    Its a live binary object so you store a pointer to it in the list.
    So when QDebug see that pointer, it will just print out the address as
    not much else it can do.
    You can also print out elements of the QObject but you have to tell
    qDebug() which ones.


  • Lifetime Qt Champion

    What text string ?
    QObject cannot be copied hence you have a list of pointers to QObject instances.



  • Ok this is starting to make sense now, somewhat :-)
    When I do a print out of the QList<QObject*> list, I get now this: membersList(0x282a4d000b0). I checked and membersList is defined as MembersList* members() const. So can I just do a qDebug() << members()[list.value(index)] to get the value stored?


  • Lifetime Qt Champion

    @BoGut
    Hi
    Im not sure what MemeberList is..
    In any case, you can print out Properties via the meta system
    see here how
    https://stackoverflow.com/questions/36518686/dump-all-properties-of-a-qobject-derived-object



  • @BoGut
    None of us are sure what you're trying to do or expect :) I will offer this, as a possibility; do you know what kind of object --- derived from QObject --- you expect the objects in the list to be? Such as your Member objects? You might want to look at qobject_cast if you want your code to try casting the generic QObject *s to a specific QObject derived-type, and on that you can call its dedicated methods if it is that type of object.



  • I want to thank you all for the help, I'll look into using the qobject_cast code and I'll post an update when I finally fix it. Stay tuned.



  • So it looks like I was missing the variable statement after this. It works now. Thanks everyone for the help.

    QList<QObject*> list() const;
    for (int index=0; index < list.size(); index++) {
         QObject *test01 = list.value(0)->name();
         qDebug() << test01;
    }