Results 1 to 5 of 5

Thread: Implementation of setData() with QList

  1. #1
    Join Date
    Feb 2012
    Location
    Germany
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Implementation of setData() with QList

    I want / have to write a setData() function in a class derived from QAbstractListModel because I want to modify the variable QList<ListItem*> m_list

    in the QList I save Parameter-Objects with have a value property which can be written by calling setValue()

    I tried the following lines in setData without success:
    Qt Code:
    1. bool ListModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    2. Parameter item = *m_list[index.row()];
    3. item.setValue(value.toString());
    4. AND
    5. m_list.at(index.row())->setValue(value.toString);
    6. emit dataChanged(index,index);
    7. return true;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Why is it not possible to get a Parameter object by reference and call the function setValue()?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implementation of setData() with QList

    Line #2 makes a copy of the object and you modify the copy and not the original object.

    QList::at() is const and it returns a const object hence you can't call non-const methods on it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Location
    Germany
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Implementation of setData() with QList

    ok thanks.
    I thought line 2 only copies the pointer to that object. ..either don't work

    With the following I add new Parameter objects to my QList
    Qt Code:
    1. ListModel *parameterList
    2. Parameter *parm = new Parameter();
    3. parm->setValue(3);
    4. parameterList->appendRow(parm)
    To copy to clipboard, switch view to plain text mode 
    But with m_list[index.row()]->setValue(value.toString()) I get a ListItem which has not the setValue() function the Parameter object has.

    I'm still not able to get the object reference.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implementation of setData() with QList

    Quote Originally Posted by Rocken View Post
    I thought line 2 only copies the pointer to that object.
    That's not a pointer declaration:
    Qt Code:
    1. Parameter item
    To copy to clipboard, switch view to plain text mode 

    If you wrote the following:
    Qt Code:
    1. Parameter *item = m_list[index.row()]
    To copy to clipboard, switch view to plain text mode 
    then it would have worked.
    I'm still not able to get the object reference.
    Your issue is strictly related to proper use of C++ and not Qt.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    Rocken (15th March 2012)

  6. #5
    Join Date
    Feb 2012
    Location
    Germany
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Implementation of setData() with QList

    Thank you for your help. You're right using pointers is general C++.

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:54
  2. Regarding the implementation of QList
    By rachit.gupta in forum Qt Tools
    Replies: 1
    Last Post: 23rd September 2009, 12:41
  3. QSqlTableModel and possible bug with setData?
    By sylvainb in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2009, 21:43
  4. setData()
    By starcontrol in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2008, 08:54
  5. setData()
    By coderbob in forum Qt Programming
    Replies: 4
    Last Post: 28th February 2008, 12:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.