Results 1 to 11 of 11

Thread: using Qlist with a class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using Qlist with a class

    Quote Originally Posted by Havard View Post
    How do I then use the .append() since it needs an argument, and my class does not take arguments?
    Qt Code:
    1. QList< Something * > list;
    2.  
    3. Something * something = new Something();
    4. list.append( something );
    5. // or simply:
    6. list.append( new Something() );
    To copy to clipboard, switch view to plain text mode 
    Don't forget to delete the items when you remove them from the list (qDeleteAll( list ) might be helpful).

  2. The following user says thank you to jacek for this useful post:

    Havard (17th February 2007)

  3. #2
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using Qlist with a class

    Thank you so much for your reply.

    Unfortunately there is a problem with using pointers.
    This little program illustrates this:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <emmintrin.h>
    3.  
    4. class myClass {
    5.  
    6. public:
    7. myClass();
    8. ~myClass();
    9. __m128i O;
    10.  
    11. };
    12.  
    13. myClass::myClass()
    14. {
    15. //sets the O to 0 bits.
    16. O = _mm_set_epi32 (0x00000000,0x00000000,0x00000000,0x00000000);
    17. }
    18.  
    19. myClass::~myClass()
    20. {
    21. }
    22.  
    23. int main(int argc, char *argv[])
    24. {
    25. QCoreApplication a(argc, argv);
    26. QList<myClass *> list;
    27. list.append(new myClass());
    28.  
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    Because I am using SSE2 instructions, the program crashes... However, if instead use: list[20]; there is no problem. But using *list[20]; the program crashes in the same manner.
    If I replace: list.append(new myClass()); with list.append(&myClass()); it does not crash, but I don't think this is any good?

    My question? How would I rewrite the program to allow for QList<myClass> list; instead of QList<myClass *> list;

    Thank you!

    Havard
    Last edited by Havard; 17th February 2007 at 16:35.

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using Qlist with a class

    Quote Originally Posted by Havard View Post
    Because I am using SSE2 instructions, the program crashes...
    It doesn't crash on my system. Are there any error messages? What CPU do you have?

    Quote Originally Posted by Havard View Post
    My question? How would I rewrite the program to allow for QList<myClass> list; instead of QList<myClass *> list;
    Just change:
    Qt Code:
    1. QList<myClass *> list;
    2. list.append(new myClass());
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. QList<myClass> list;
    2. list.append( myClass() );
    To copy to clipboard, switch view to plain text mode 
    The default implementations of operator= and copy constructor will be enough for myClass.

  5. The following user says thank you to jacek for this useful post:

    Havard (17th February 2007)

  6. #4
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using Qlist with a class

    I just wanted to say that the problem was finally solved, but turned out to be a lot more complex than I had expected.

    Without going into too much detail, it turns out that there is a problem with aligning the 16 byte sse2 variables inside a c++ class with a microsoft compiler.

    So jacek, I am guessing that you are not using a microsoft-compiler since you did not get any errors with the little test-program I posted. I am even guessing you are not using anything microsoft (like Visual Studio) at all...?

    The solution was to overload the "new" operator to make it align them properly.

  7. #5
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using Qlist with a class

    Here is a link with the fix, if anyone would be interested in using sse2 with classes: http://softwarecommunity.intel.com/i.../30231301.aspx

  8. The following user says thank you to Havard for this useful post:

    jacek (24th February 2007)

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using Qlist with a class

    Quote Originally Posted by Havard View Post
    So jacek, I am guessing that you are not using a microsoft-compiler since you did not get any errors with the little test-program I posted. I am even guessing you are not using anything microsoft (like Visual Studio) at all...?
    Yes, you are absolutely right.

Similar Threads

  1. QList usage in place QPtrList
    By darpan in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 15:41
  2. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43
  3. Replies: 2
    Last Post: 4th May 2006, 19:17
  4. How to propagate from one class to another
    By mahe2310 in forum Qt Programming
    Replies: 15
    Last Post: 20th March 2006, 01:27
  5. Replies: 5
    Last Post: 15th March 2006, 07:33

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.