Results 1 to 8 of 8

Thread: Return value to QList

  1. #1
    Join Date
    May 2009
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Return value to QList

    I have a method that returns a QList<int>.

    some possibilities:
    - QList is returned filled with ints. OK.
    - QList returns empty, but because no ints were put in it.

    but the method might result in error, and I have to return it nonetheless.
    right now, I'm returning a empty QList, but its no good, since a empty QList is possible to happen without error.

    I tryed returning 'NULL', but C++ won't let me.
    I thought of returning a QList with the first element set to -1, but that's lame.

    What can I return instead?
    I'd rather not use Exceptions...

    thankyou

  2. #2
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Return value to QList

    think on an impossible value and add it to the list. If the list has only that value, it's like empty.

  3. #3
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Return value to QList

    What about a "bool* ok" function argument?

  4. #4
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Return value to QList

    IF you want to test for NULL then you might try returning a QList<int> *.


    jw
    sw developer

  5. #5
    Join Date
    May 2009
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Return value to QList

    think on an impossible value and add it to the list. If the list has only that value, it's like empty.
    that's what I thought when I said about returning the first element set as -1... but, that's so... not elegant.

    What about a "bool* ok" function argument?
    I can't change any arguments, but maybe I can use that sugestion somewhere else. thanks


    IF you want to test for NULL then you might try returning a QList<int> *.
    if you meant
    Qt Code:
    1. return QList<int> *;
    To copy to clipboard, switch view to plain text mode 
    that doesn't work. I've got nooo idea why.

    thank you all, I think I'll have to rely in 'not elegant' methods XD

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Return value to QList

    The "bool *ok" solution is the best. If you can't alter the arguments but you can alter the returned value you can use
    Qt Code:
    1. QPair<bool, QList<int> > returnListFunction() {}
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: Return value to QList

    Quote Originally Posted by lyucs View Post
    What can I return instead?
    You can return an empty list and besides that have a "lastError" method in your class to determine if the last call was successful or not. Qt uses this approach in many places as an alternative to exceptions.
    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.


  8. #8
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Return value to QList

    I use a public bool variable in the class, than, set it in the beginning of the method and change it if the error occur. After calling the method, I check the flag value.

Similar Threads

  1. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25
  2. QSql*Model + QTreeView : make a tree
    By punkypogo in forum Qt Programming
    Replies: 18
    Last Post: 24th October 2008, 18:14
  3. Once more : QAbstractItemModel woes
    By Valheru in forum Qt Programming
    Replies: 10
    Last Post: 15th January 2008, 10:44
  4. QTableView Repaint/Refresh
    By millsks in forum Newbie
    Replies: 9
    Last Post: 10th January 2008, 17:18
  5. Version 4.1.3 and QTreeView
    By greencastor in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 10:37

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.