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 http://www.digitalfanatics.org/projects/qt_tutorial/chapter09.html. 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 = m_contacts.at(currentRow());

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

    }
    @

    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->contact.name;@



  • 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.