Results 1 to 11 of 11

Thread: using Qlist with a class

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

    Default using Qlist with a class

    Hi.

    I have a class without any constructors that I want to use Qlist with.
    Since .append() does not take zero arguments, how do I add one more class to the Qlist?

    Is Qlist the best container for me? The class contains all the info about the current Tab the user have selected using QTab, so I can use a MyClass[MyTab->currentIndex()] to access the current info. When the user adds another tab, I will need another instance of MyClass, and thought that Qlist and .append() would be the way to go, but I am having troubles with this since my class does not contain any constructors.

    Any advice?

    thanks!

    Havard

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: using Qlist with a class

    Hi

    The values stored in the various Qt containers can be of any assignable data type.
    To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator.
    J-P Nurmi

  3. #3
    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

    thanks, does this mean I have to equip my class with a default constructor, a copy constructor, and an assignment operator, even though it does not have (or need) any of this at the present?

    Reading about "Generic Containers" in the QtAssistant, it says: "If we don't provide a copy constructor or an assignment operator, C++ provides a default implementation that performs a member-by-member copy. "

    How come?

    Havard

  4. #4
    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
    does this mean I have to equip my class with a default constructor, a copy constructor, and an assignment operator, even though it does not have (or need) any of this at the present?
    You can avoid this by creating a list of pointers.

    Quote Originally Posted by Havard View Post
    it says: "If we don't provide a copy constructor or an assignment operator, C++ provides a default implementation that performs a member-by-member copy. "

    How come?
    That's how C++ works. You can disable this by delaring those methods as private.

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

    Havard (17th February 2007)

  6. #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

    Quote Originally Posted by jacek View Post
    Originally Posted by Havard :
    does this mean I have to equip my class with a default constructor, a copy constructor, and an assignment operator, even though it does not have (or need) any of this at the present?

    You can avoid this by creating a list of pointers.
    Ok, thanks! How do I then use the .append() since it needs an argument, and my class does not take arguments?

    Havard

  7. #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
    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).

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

    Havard (17th February 2007)

  9. #7
    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.

  10. #8
    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.

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

    Havard (17th February 2007)

  12. #9
    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.

  13. #10
    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

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

    jacek (24th February 2007)

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