Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Editing items of a QList
Forum Updated to NodeBB v4.3 + New Features

Editing items of a QList

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 12.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dakron
      wrote on last edited by
      #2

      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;@

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        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
        @

        1 Reply Last reply
        0
        • P Offline
          P Offline
          phamtv
          wrote on last edited by
          #4

          thank you to both responses!!!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved