Important: Please read the Qt Code of Conduct -

QList with custom class - accessing elements' methods

  • Greetings,

    It maybe that I just can't formulate the right question to google it, but I've tried several times, could't find my exact problem though. So I thought, maybe you guys could help me out:

    The question is very simple:

    I have a custom class MyClass, I make a *QList<MyClass *> objList, I create a new instance of my class *MyClass obj, then I put it in the list: objList->append(obj).

    But when I try to call a member function of an element of the list
    AutoComplete shows nothing (no members of the object), as if it doesn't recognize that an object of MyClass is returned.
    When I manually type it: objList->at(0)->someFunction() it is working without a problem.

    So what troubles me is that QtCreator doesn't recognize the object returned from at(...) function, so that I can further call member functions. I am pretty sure I have misunderstood something along the way, so it would be kind if you would help me to clear this up.

    Thanks in advance, Kris.

  • Hello,

    I tested what you described in my QtCreator and it works for me. Meaning I get all methods of the MyClass as suggestions from the Autocompletion.

    Could you provide more details, i.e. Qt Version, Creator Version and the actual code (in increasing importance ;-)).



  • Oh sorry, I totally forgot how important is that

    Qt Creator 2.7.2,
    Qt 5.1.0 (32 bit),

    @//in header
    QList<Waypoint *> *Waypoints;

    //in constructor
    Waypoints = new QList<Waypoint *>();

    //in a function
    Waypoint *newWaypoint = new Waypoint();
    newWaypoint->sequenceId = 1;
    qDebug() << Waypoints->at(0)
    // then when I press "." nothing happens,
    //and I must manually add "->sequenceId";

  • At first glance nothing strikes me as really odd. Let's clear some things I'm not 100% sure about:

    'In header' means that the Waypoints list is a class variable of a class A.

    In the corresponding cpp file you initialise that pointer with a new QList-object within class A's constructor.

    Within a method of class A then occurs the last piece of code.

    Are those assumptions correct?

    Then I have two additional questions:

    When you press '.' and QtCreator recognises its a pointer it is usually translated to '->'. Does this translation happen or does a '.' occur?

    In your initial posting you wanted to access a function. Now it seems to be a member-variable or property or something?!? it should make no difference in my opinion but this way I can reproduce your problem more accurately.

    Best Soraltan

  • Thank you for your reply.

    Your three assumptions are all correct, all is done within a class.

    As for the additional questions:

    1. No, it doesn't translate to ->, it remains as a point
    2. AFAIK it doesn't matter whether it's a member-variable or method/function

  • Lifetime Qt Champion


    It might be an old Qt Creator problem (your version is pretty old), I would recommend updating to the latest version (currently 3.1) and try again.

  • Yes I agree it should not matter. It is only that I can reproduce it better. Regardless it works in both cases for me: I can get code-completion for methods as well as for member-variables. I need to give an index to the at() function of course. There was a short moment when I thought I could reproduce your behaviour until I noted that I forgot to provide an index (e.g. at(0) instead of at()).

    Weird. Could you provide the class?!? Just so that I can test it with the class in question. But I must admit that I'm grasping for straws here. I have no real clue on what is going on.

    Btw: I have 2.4.1 32bit and I'm using Qt 4.8 (the latter should have no impact on code-completion though).

  • Uff, I was not aware of how far QtCreator already got. A Bug that occurred somewhere between version 2.4 and 2.7 and was fixed later?!? Definitely worth a shot.

    Regardless I will update mine, too! Let's see what has changed :-)

  • Somewhat unrelated to the question, but related to the code: Why do you use a pointer to a QList object (as opposed to a plain QList object)?

Log in to reply