Results 1 to 4 of 4

Thread: QObject subclass in a QList

  1. #1
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

    Default QObject subclass in a QList

    Hi!

    I recently programmed some qt and stumbled over the problem, that i cannot add my own class derived from QObject into a QList.

    See two examples in attachments!

    This code is not very useful but it was just a test to make sure that i made something wrong!

    Dog is a simple class with 1 function.
    When it is a normal class then i can add it to my QList without a problem.
    But if i extend dog from QObject i cannot add it to a QList!

    Can someone help me?

    I used cmake to compile my examples!
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject subclass in a QList

    Read the docs about QList.

    Somewhere soon you will find:

    "QList's value type must be an assignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *.

    So you need to use the list like this:

    Qt Code:
    1. QList<Dog*> list;
    2. Dog* dog = new Dog();
    3. list.append(dog);
    To copy to clipboard, switch view to plain text mode 
    HIH

    Joh

  3. #3
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QObject subclass in a QList

    Thx for you fast replay.

    And what about memory of the QList<*> ?
    Do i have to manage memory of the QList's data?

    Can someone give me a guide about memory management of Qt? So do i have to delete pointers of QObjects or not?

    Rich

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject subclass in a QList

    You have to delete everything yourself, except that QObject deletes all its children itself.

    So for example when you add a widget to a layout, the layout becomes parent of that widget and deletes the widget when it is destroyed.

    But you need to delete your custom objects yourself.

    Your list will probably be a member variable of some object. in its destructor you call:

    Qt Code:
    1. // iterates through all items of the list and calls delete on them, but does not remove the entry from the list!
    2. qDeleteAll(list);
    3. // removes all entries from the list.
    4. list.clear();
    To copy to clipboard, switch view to plain text mode 
    Joh

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:54
  2. Replies: 8
    Last Post: 12th February 2010, 02:41
  3. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  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. QList<QList > question
    By SubV in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 15:14

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.