Important: Please read the Qt Code of Conduct -

[SOLVED] QList::operator<< or QList::append()?

  • Greetings.

    Similar to my previous post, now I'm undecided about which is more convenient to use to add items to a constant QList (more precisely, a const QList< cv::Mat >& ):

    (1) QList::operator <<


    (2) QList::append()

    In both cases, considering the version that adds only one element at a time.

    Do they have the same performance? or one is better than the other?

    Thanks in advance for any help and/or suggestions.

  • Moderators

    You can also use QList::operator+= :-)

    I think they are all the same, choose the one you like best. You can, of course, ask Thiago (the maintainer of QtCore) about this, he may know a lot about performance considerations, but I doubt this is really that important. Remember the famous quote about premature optimisation ;-)

  • Thank you.

    As in my previous post, I guess that being a constant QList, the various options will have a similar performance. However, no hurts to ask.

  • In the QList header file you'll find:
    @inline QList<T> &operator+=(const T &t) {
    append(t); return *this;
    inline QList<T> &operator<< (const T &t) {
    append(t); return *this;

    So using append() directly saves one indirection, but due to the inlining I'd doubt it makes a real difference on performance.


    When adding multiple elements, using << has the advantage you can do:
    @myList << a << b << c << d;@

    Probably not faster than multiple append()'s, but clearer code ;-)

  • Thanks for the information MuldeR, now the matter is clarified.

Log in to reply