Nominate our 2022 Qt Champions!

Can "item" be added on top of the "list" ?

  • I am test adding items to the "list" .
    When I add several "item"line by line it gets sorted alphabetically.
    When I add same series of "item" it gets resorted .

    By default "list sorting" is disabled and by definition last item added should be at the end of the list.

    I am not in favor to discus Qt interpretation of "sorting" , " last item in the end of the list" and "insert" or "add" either.

    Perhaps verbal description is "last (item) in , first (item ) out" which in the days of decent terminology was "LIFO" .

    In short -how can I have list of "item," displayed in LIFO fashion ?

    item ADDED on top of the list and displayed as such

    void QListWidget::addItem(const QString &label)
    Inserts an item with the text label at the end of the list widget.

  • Moderators

    To insert item at the top of the list use insertItem(int row, const QString &label) with row set to 0. addItem(label) is basically a shorthand for insertItem(count(), label).

    As for sorting - if sortingEnabled is false (the default as you said) there should be no sorting whatsoever, so maybe double check if it was enabled somewhere accidentally? You could query with isSortingEnabled() just before or after you insert an item.

  • @Chris-Kawa On my original post I was trying to avoid semantics , but
    there are differences in English even non English native likes me can observe.
    add - means just that - AT THE END
    append - is more precise , again at the end
    insert - means at selected place

    It is obvious, and I have been there before, that Qt has few options to manipulate entries into widget.
    Depending on widget.
    AND they are NOT same in each widget - for example suggested "is sorting enabled " is NOT a feature of TextEdit.
    Which is OK - I can bypass that.

    On the other hand - append - does not automatically advance line / index. Result is - "stuff "
    is added "in line " after each other. Unusable for me.

    To make sure - this reply is not complaint, I am just noticing stuff , irregardless if it is obvious to some.

    BTW this "new Coke" form and its inability to check what I am replying to is "expletive deleted ".

  • Moderators

    add - means just that - AT THE END

    Add means add. Doesn't really specify where. You can add to list, map, set, dictionary or sorted collection and it will have different placement in each. If you add to sorted collection it won't add at the end but at the place its sorting order indicates. For unsorted collections it just adds at the end because it happens to be the easiest and most performant operation. English word "add" doesn't have placement connotations, just expanding some collection to include given element.

    append - is more precise , again at the end

    Yes, but append is not used here for exactly that reason - it suggests position, which you can't guarantee if sorting is enabled. What should append b do to a sorted collection like this a,d,g? a,b,d,g? It breaks the meaning of append. a,d,g,b? It breaks sorting order. Both are wrong answers so if something can be sorted append operation is not included. On unsorted collections like QList or QString append is included because its meaning is clear and doesn't break container's assumptions.

    insert - means at selected place

    Agreed and it does just that. It could be more clear as insertAt, but the parameter clearly suggests that anyway.

    for example suggested "is sorting enabled " is NOT a feature of TextEdit.

    I mentioned it in context of QListWidget, which is a collection (list) of entries so it's obvious that it would sort those entries.
    It's not clear what would it sort in a QTextEdit - letters, words, sentences, paragraphs, text blocks, lines, something else? What if text has markup? What if there's block formatting? Should it break that, split blocks or reinterpret before sorting? Not clear at all. Different people different needs, so again - it doesn't try to guess for everyone.

    append - does not automatically advance line / index

    Why would it? Advancing a line would mean adding data to the collection that you didn't put there yourself. What if you wanted to append contents of a streaming text buffer (eg. from a real time chat) and it kept inserting line breaks whenever new data chunk arrived? It would totally destroy the data and be kinda excruciating to workaround (inserting artificial backspaces?). If you want a line break just append a line break. It's up to you as a user to implement behavior you need.

Log in to reply