Nominate our 2022 Qt Champions!

Editing items of a QList

  • I am working on module to support adding, editing, and removing data to an xml file using Chapter 9 as an example I have made a change to the code by replacing QValueList with QList. A problem I am running into is that I am getting an error indicating

    bq. /mnt/hgfs/work/Projects/ART-build-desktop/../ART/snippetbrowser.cpp:122: error: passing ‘const QString’ as ‘this’ argument of ‘QString& QString::operator=(const QString&)’ discards qualifiers

    My editing code snippet is as follow:
    void snippetBrowser::editSnippet()
    Contact c =;

    editor-> =;
    editor->contact.eMail = c.eMail;
    editor-> =;
    if (editor->exec() == QDialog::Accepted)
    { = editor->; = editor->contact.eMail; = editor->;
        setItem(currentRow(), 0, new QTableWidgetItem(;
        setItem(currentRow(), 1, new QTableWidgetItem(;


    There error seem to be @ line 11-13. How can edit the item of a QList???
    Thank you for your time and consideration.

  • You should use operator [] instead of at() in this case as it returns constant reference to the item.

    @m_contacts [currentRow()].name = editor->;@

  • On non-const lists, operator returns a reference to the item and can be used on the left side of an assignment.

    For read-only access, an alternative syntax is to use at() which is faster than operator, because it never causes a deep copy to occur.

    const T & at ( int i ) const
    T & operator[] ( int i )
    const T & operator[] ( int i ) const

  • thank you to both responses!!!

Log in to reply