Results 1 to 4 of 4

Thread: Uses on Qvariant in the address book example

  1. #1
    Join Date
    Apr 2011
    Posts
    67
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    22
    Thanked 5 Times in 2 Posts

    Default Uses on Qvariant in the address book example

    Hello,i am studying the address book example on Qt Assistant and on the address book example i have a come across usages of Qvariant which i don't understand.

    Here is the code:

    Qt Code:
    1. QVariant TableModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();//what does the use of Qvariant here mean - 1
    5.  
    6. if (index.row() >= listOfPairs.size() || index.row() < 0)
    7. return QVariant();//what does the use of Qvariant here mean - 2
    8.  
    9. if (role == Qt::DisplayRole) {
    10. QPair<QString, QString> pair = listOfPairs.at(index.row());
    11.  
    12. if (index.column() == 0)
    13. return pair.first;
    14. else if (index.column() == 1)
    15. return pair.second;
    16. }
    17. return QVariant();//what does the use of Qvariant here mean - 3
    18. }
    To copy to clipboard, switch view to plain text mode 

    I have commented on the code next to Qvariant usages.Someone help me understand what it means.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: Uses on Qvariant in the address book example

    The default QVariant::QVariant() constructor creates an invalid object.

    You can test for valid QVariant with code

    Qt Code:
    1. QVariant obj = model->data(index, role);
    2. if (obj.isValid()) {
    3. // Do Something with valid data
    4. }
    5. else {
    6. // Handle invalid data
    7. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Apr 2011
    Posts
    67
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    22
    Thanked 5 Times in 2 Posts

    Default Re: Uses on Qvariant in the address book example

    I don't understand,could you explain in relation to the code i've given?.

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    Default Re: Uses on Qvariant in the address book example

    The code you posted check if there are the condition to return a "valid" result.
    If don't it returs an invalid QVariant, in this way the caller may say if the result is valid.
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 4
    Last Post: 4th January 2011, 12:07
  2. Costomize the Address Book Example.
    By HeX0R in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2010, 00:30
  3. Replies: 1
    Last Post: 4th December 2009, 17:03
  4. Replies: 4
    Last Post: 22nd September 2009, 09:36
  5. qt book
    By doss in forum Newbie
    Replies: 1
    Last Post: 8th April 2009, 09:24

Tags for this Thread

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.