Results 1 to 19 of 19

Thread: QList can't be returned

  1. #1
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default QList can't be returned

    I have a problem when returning QList from a fucntion. I have two classes in which one class returns a QList from a fucntion and the other class catches this list. But I get null List returned though list in the other class has values. Below is my classes overview.

    Qt Code:
    1. class A
    2. {
    3. classB *b;
    4. void f()
    5. QList<QString>* list = b->function();
    6. qDebug() << list.at(0); // getting error
    7. }
    8.  
    9. class B
    10. {
    11. QList<QString> Blist;
    12. void list()
    13. {
    14. int i=0;
    15. .....
    16. Blist.insert(i,"value");
    17. qDebug() << Blist.at(i);
    18. i++;
    19. }
    20. QList<QString>* function()
    21. {
    22. qDebug() << Blist.at(0) ; // printing the value at index 0
    23. return &Blist;
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    Can any one please tell me why I am getting null list returned.

    Thank You,
    Baluk

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QList can't be returned

    The problem is most likely not your function but the fact that you do not initialise b!

    Before you use
    Qt Code:
    1. b->function()
    To copy to clipboard, switch view to plain text mode 

    You need to
    Qt Code:
    1. b = new classB;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    list.at is invalid. It should be list->at, as your using a pointer.

    Oh, and ensure you allocate the memory to, or just remove that asterix.

  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: QList can't be returned

    Returning a pointer to an owned list in an object oriented language (and not only there) is probably not the wisest decision.
    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. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QList can't be returned

    Quote Originally Posted by squidge View Post
    list.at is invalid. It should be list->at, as your using a pointer.
    But that should result in a compile error.

  6. #6
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    I have initialized the class. I don't have any compile errors. Only a runtime error. And I fallowed the same procedute to QMap also in my other application. It is working fine.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QList can't be returned

    The following code contains a lot of errors (it does not even compile) can you post the real code please?

    Qt Code:
    1. class A
    2. {
    3. classB *b;
    4.  
    5. void f()
    6.  
    7. QList<QString>* list = b->function();
    8. qDebug() << list.at(0); // getting error
    9. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    My app has 100 lines of code. thats why I have just given the basic view. I will try to write the new app and post it here.

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QList can't be returned

    Not the whole code. Just the parts of class A, and more specifically, your function f.
    Like you wrote it, it does not work.

  10. #10
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    Here is the code sample.
    Qt Code:
    1. #include "xmldata.h"
    2. class calculateHelper : public QMainWindow
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit calculateHelper(QWidget *parent = 0);
    8. ~calculateHelper();
    9. private:
    10. xmlData *xml;
    11. QList<QString>* list1;
    12. }
    13.  
    14. Thank You,
    15. baluk
    16. calculateHelper::calculateHelper(QWidget *parent) :
    17. QMainWindow(parent),
    18. ui(new Ui::calculateHelper)
    19. {
    20. xml = new xmlData();
    21. }
    22. void calculateHelper::insertList()
    23. {
    24.  
    25. ui->tableWidget->clear();
    26.  
    27. this->fDetails = xml->getInfo();
    28. list1 = xml->returnList();
    29. }
    30.  
    31. QList<QString>* xmlData::returnList()
    32. {
    33. &list;
    34. }
    To copy to clipboard, switch view to plain text mode 

    Thank You,
    Baluk

  11. #11
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QList can't be returned

    Ok, I didn't see this before:

    Qt Code:
    1. QList<QString>* xmlData::returnList()
    2. {
    3. &list;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QList<QString>* function()
    2. {
    3. qDebug() << Blist.at(0) ; // printing the value at index 0
    4. return &Blist;
    5. }
    To copy to clipboard, switch view to plain text mode 

    are both wrong. The first more than the second.

    Do you know the meaning of &list? what is the difference with *list?

  12. #12
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    &list means returning the address of list in the above contest but I forgot to write return statement. *list is a pointer variable pointing to some arbitary memory location.

  13. #13
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QList can't be returned

    Quote Originally Posted by baluk View Post
    &list means returning the address of list in the above contest but I forgot to write return statement. *list is a pointer variable pointing to some arbitary memory location.
    No, that is wrong.

    & is the reference operator
    * is the dereference operator.

    Qt Code:
    1. int i = 5;
    2. int *j = &i;
    3. int k = *j;
    To copy to clipboard, switch view to plain text mode 

    In your case your function returns a pointer to a qlist, but you reference the pointer again.

  14. #14
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    My code is now working, I am sorry that I was overlooked about writing "return" statement. As from your example I am doing this
    Qt Code:
    1. int *j = &i;
    To copy to clipboard, switch view to plain text mode 
    in my code. Isn't it.

  15. #15
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QList can't be returned

    Quote Originally Posted by baluk View Post
    My code is now working, I am sorry that I was overlooked about writing "return" statement. As from your example I am doing this
    Qt Code:
    1. int *j = &i;
    To copy to clipboard, switch view to plain text mode 
    in my code. Isn't it.
    You are actually doing:
    Qt Code:
    1. QList<QString> *list;
    2.  
    3. ...
    4.  
    5. QList<QString> *Class::getList()
    6. {
    7. return &list;
    8. // list already is a pointer, so you do not need to reference it.
    9. }
    10.  
    11. // If you have this:
    12. int a = 1;
    13. int b = 2;
    14. int *ap = &a;
    15. int *bp = &b;
    16.  
    17. // You do this:
    18. int *c = &ap;
    19. // While it should be:
    20. int *c = ap;
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    To prevent this from happening in the future, you should try some tools like 'lint' which check your source code for various types of errors (such as not returning a value in a function declared as returning a value)

  17. #17
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    "list" is not a pointer variable but just a variable. "list1" is a pointer variable. In my given code do you see that i used two classes "calculatehelper" and "xmlData". I just showed you the first "class declaration" not the second one.
    Qt Code:
    1. QList<QString>* xmlData::returnList()
    2. {
    3. return &list;
    4. }
    To copy to clipboard, switch view to plain text mode 
    this function belongs to second class in which I declared the list variable as
    Qt Code:
    1. QList<QString> list;
    To copy to clipboard, switch view to plain text mode 
    . Sorry for naming conventions and made it confused.

    Thank You,
    baluk.

  18. #18
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    Hi Can you provide any link to how to use this "lint" tool. I tried goggling but couldn't find any usefull link. I want to use this one in my Qt.

  19. #19
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList can't be returned

    Apologies, I confused the names. 'lint' is more for C code (It will work for C++, but you have to pay), for C++ code you might want to look at cppcheck, which is free and open source: http://en.wikipedia.org/wiki/Cppcheck

    and ensure to add '-Wall' to the command line for the GCC compiler.

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:54
  2. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  3. QDateEdit and date returned
    By cydside in forum Qt Programming
    Replies: 17
    Last Post: 26th May 2009, 21:08
  4. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  5. file name returned by QFileDialog::getSaveFileName()
    By Lexrst in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2008, 16:38

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.