Results 1 to 4 of 4

Thread: QComboBox setModel - ownership???

  1. #1
    Join Date
    Sep 2010
    Posts
    62
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QComboBox setModel - ownership???

    Hello,

    Does QComboBox own the model after setting it with setModel();

    Manual says no word about it.

    Thx

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QComboBox setModel - ownership???

    Check it for Yourself, i.e.:

    Qt Code:
    1. QComboBox *cb = new QComboBox( this );
    2.  
    3. qDebug() << model->parent();
    4. cb->setModel( model );
    5. qDebug() << model->parent();
    To copy to clipboard, switch view to plain text mode 

    So basically no, views don't take ownership of model, unless explicitly set, because they can be shared by many views.

    Also there is an error in man.:
    The view does not take ownership of the model unless it is the model's parent object because the view (should be model?) may be shared between many different views.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    Join Date
    Sep 2010
    Posts
    62
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QComboBox setModel - ownership???

    Quote Originally Posted by Talei View Post
    Check it for Yourself, i.e.:

    Qt Code:
    1. QComboBox *cb = new QComboBox( this );
    2.  
    3. qDebug() << model->parent();
    4. cb->setModel( model );
    5. qDebug() << model->parent();
    To copy to clipboard, switch view to plain text mode 

    So basically no, views don't take ownership of model, unless explicitly set, because they can be shared by many views.

    Also there is an error in man.:
    Hallo,

    Thanks for the answer.

    For deletion it means: first the view, than the model - correct?
    By the way, is the parenthood this same as ownership? I.e. does a parent widget automatically delete its all children when destroyed?
    Last edited by lotek; 5th August 2011 at 09:02.

  4. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QComboBox setModel - ownership???

    Please RTM
    Look here : http://doc.qt.nokia.com/4.7/objecttrees.html and http://doc.qt.nokia.com/4.7/qt-basic-concepts.html

    Basically any new object that is QObject subclass uses parent/child relationship. This means that if parent is deleted all children are deleted to.
    In above example it you delete "this' then model and view will be deleted to.
    If you write above code like this:

    Qt Code:
    1. QComboBox *cb = new QComboBox( this );
    2.  
    3. qDebug() << model->parent();
    4. cb->setModel( model );
    5. qDebug() << model->parent();
    6.  
    7. delete cb;
    To copy to clipboard, switch view to plain text mode 
    deleting cb will delete model also.
    Althought if You delete model from view then view don't "know" what/how to display. By default views i.e. QComboBox creates QStandardItemModel in constructor so after You delete Your custom model You will have to add new one, something along this line's:
    Qt Code:
    1. QComboBox *cb = new QComboBox( this );
    2. model->setObjectName( "My Old model" );
    3.  
    4. qDebug() << model->parent();
    5. cb->setModel( model );
    6. qDebug() << model->parent();
    7.  
    8. delete model;
    9. cb->setModel( new QStandardItemModel(0, 1, cb) );
    10. cb->model()->setObjectName( "New model" );
    11. cb->addItem( "sample", "Sample" );
    12. qDebug() << "Model name:" << cb->model()->objectName() << "Parent: " << cb->model()->parent();
    To copy to clipboard, switch view to plain text mode 
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

Similar Threads

  1. QTableView and QStyledItemDelegate ownership
    By lxman in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2011, 05:19
  2. Replies: 3
    Last Post: 14th May 2010, 23:00
  3. Thread Ownership Problem
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 00:18
  4. QComboBox -> setModel -> Strange behaviour
    By oscar in forum Qt Programming
    Replies: 2
    Last Post: 13th August 2008, 21:27
  5. QComboBox does not resize with setModel()
    By Wim in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2007, 23:29

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.