Results 1 to 5 of 5

Thread: Errors inserting elements in a QList

  1. #1
    Join Date
    Aug 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Errors inserting elements in a QList

    Hi all,

    I am trying to fill a QList with structs of 2 elements. At first, I am doing it as in the Address Book Example in Qt Examples and Demostrations, like this:

    Qt Code:
    1. QList<QPair<int,int>> listOfPairs;
    2. QPair<int,int> pair(1,1);
    3. listOfPairs.insert(0,pair);
    To copy to clipboard, switch view to plain text mode 
    The code compiles right but when I check the list listOfPairs it has wrong values and not the two ones I have inserted. I have also tried with the method append, and I get the same result. And I've tried also changing the list of QPair's to a list of structs defined as:
    Qt Code:
    1. typedef struct mystruct
    2. {
    3. int m_a;
    4. int m_b;
    5. mystruct(int a, int b)
    6. {
    7. m_a = a;
    8. m_b = b;
    9. }
    10.  
    11. }mystruct;
    12.  
    13. QList<mystruct> listOfMyStructs;
    14. mystruct str(1,1);
    15. listOfMyStructs.insert(0,str);
    To copy to clipboard, switch view to plain text mode 

    But it does not work again.

    What am I doing wrong?

    Thank you!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Errors inserting elements in a QList

    What's the error?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Errors inserting elements in a QList

    First, your Qt code will not compile as posted. When the error is corrected the code compiles and runs just fine:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. // Your snippet v correction
    9. QList<QPair<int,int> > listOfPairs;
    10. QPair<int,int> pair(1,1);
    11. listOfPairs.insert(0,pair);
    12.  
    13. // Put more in the list
    14. listOfPairs.append(QPair<int, int>(2, 3));
    15. listOfPairs.append(QPair<int, int>(4, 5));
    16.  
    17. qDebug() << listOfPairs;
    18.  
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    Qt Code:
    1. (QPair(1,1) , QPair(2,3) , QPair(4,5) )
    To copy to clipboard, switch view to plain text mode 

    So, the thing that does not work and then "does not work again" is probably the method you are using to inspect the list, but we cannot see that.

  4. #4
    Join Date
    Aug 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Errors inserting elements in a QList

    Thank you.

    The code I posted is only a few lines from a more complex code, I know that those lines alone don't compile And you are right and the content of the list is correct, I was checking the content of the variables with the Visual Studio tools and they show me wrong values, but when using QDebug() as you propose it works fine. Do you know why Visual Studio doesn't work for Qt variables or classes?

    Thank you very much.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Errors inserting elements in a QList

    Do you know why Visual Studio doesn't work for Qt variables or classes?
    No, and I cannot make an educated guess because "doesn't work" and "wrong values" is not a good description of what you are seeing.

    The first thing to check is that your project is built with debugging symbols.

    Many (most) Qt classes are built with a private implementation so often the only member variable of a class is a pointer to a private class and that's all you see by default. Debuggers use helper code to present user friendly versions of these objects (like GDB in the Qt SDK does). As a non-Visual Studio user I don't know what helpers are available.

Similar Threads

  1. inserting to Qlist of QSharedPointer
    By jajdoo in forum Newbie
    Replies: 1
    Last Post: 11th August 2011, 00:20
  2. Replies: 6
    Last Post: 29th November 2009, 00:43
  3. deleting all elements in a qlist
    By reshma in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2009, 20:27
  4. Remove first n elements from QList
    By pakulo in forum Qt Programming
    Replies: 8
    Last Post: 4th June 2007, 07:27
  5. Qt 3 to Qt 4 port: QList compile errors
    By Amanda in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2006, 05:45

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.