Results 1 to 3 of 3

Thread: 2 ways to get data from model

  1. #1
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question 2 ways to get data from model

    Hi all.
    I'm learning from Johan Thelin's book(based on Qt 4.x) and there is written that,to get data from model we use:
    Qt Code:
    1. int value2 = index.model()->data(index,Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 

    But in documentation(Qt 5.1.1) there is another way:
    Qt Code:
    1. int value = index.data().toInt();
    To copy to clipboard, switch view to plain text mode 

    Both work for me very well. But what is the difference? Which one I should use?

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: 2 ways to get data from model

    I would use the second version because it is shorter. You don't need to use the index twice. Apart from that there's not much difference.
    There are also a lot of other convenience functions all over the Qt framework.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 2 ways to get data from model

    Ultimately all calls for data end up in the model's data() method.

    As Infinity wrote, QModelIndex::data() is just a short hand for getting the pointer to the model and calling its data() method.

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 21st October 2013, 21:24
  2. Replies: 9
    Last Post: 14th February 2013, 19:39
  3. Replies: 0
    Last Post: 2nd November 2011, 11:13
  4. Ways to share data between threads
    By AndresBarbaRoja in forum Qt Programming
    Replies: 8
    Last Post: 11th March 2011, 16:47
  5. Model-view: Display items in different ways
    By taboom in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2007, 19:05

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
  •  
Qt is a trademark of The Qt Company.