Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23

Thread: How to populate a QTableView using data in a .txt file

  1. #21
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to populate a QTableView using data in a .txt file

    What is the general thumb rule in Qt?
    This is indeed one of the more confusing parts of Qt for someone coming from a traditional C++ background.

    The rule of thumb is that "Anything created (using operator new() with a non-NULL parent object (QObject *, QWidget *) will be deleted when the parent is deleted." What this means in practice is:


    • If you create QMainWindow on the stack (in main()), all of its children will be deleted when the program exits (so you don't need to worry about deleting any child window you create with your QMainWindow as its ancestor).
    • Any time you add a child to another QObject, that transfers ownership and the child will be deleted when the QObject is
    • This rule holds for both QWidget and non-QWidget (eg. QObject) hierarchies
    • QObjects created on the stack are deleted (along with their children) when that part of the stack goes out of scope.


    It is because of these parent-child ownership relationships that copy constructors and assignment operators are disabled for QObject-derived classes.

    These same rules hold for objects in QGraphicsItem hierarchy, even though they are not QObject-based.

    For QStandardItem, QTableWidgetItem, and similar classes that are not QObject-based, when you give them to an instance that *is* QObject-based, you transfer the ownership (and lifetime control) to that instance. This is why calling QStandardItemModel::appendRow() doesn't cause a memory leak.

    Object instances that are designed to be shared (like QAbstractItemModel instances, for which a single instance can be shared by many views), these are usually QObject-based, and are constructed with a QObject instance as their parent. So in the code I posted earlier, the QStandardItemModel instance was created with the QMainWindow as its parent, and will be deleted along with the parent.

    For GUI classes with a .ui file created in Designer, there are at least three conventions: 1) include a "ui" class pointer as a member variable, 2) multiply inherit the GUI class from both a QWidget parent and the UI class, or 3) include a ui class member variable (not a pointer to one).

    In the first case, it is customary to initialize the ui pointer by calling operator new() and then ui->setupUi() in the GUI class constructor. The ui class instance has no "parent", and so it must be explicitly deleted in the GUI class destructor.

    In the second and third cases, there is no operator new() involved, and the ui is initialized by calling either setupUi() (case 2) or ui.setupUi() (case 3).
    Last edited by d_stranz; 23rd September 2015 at 15:48.

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

    newtoQ_s (24th September 2015)

  3. #22
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to populate a QTableView using data in a .txt file

    Since Qt is C++, the general rules can be used.

    However, any QObject derived class that has a parent, will be deleted by its parent if it is not deleted explicitly.

    I.e. QObject based objects can have their life time tied to the live time of another QObject based object.

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    newtoQ_s (24th September 2015)

  5. #23
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Thanks a lot for the detailed response. I am trying my best to understand Qt and to be able to update my knowledge on C++ as well.

Similar Threads

  1. Replies: 0
    Last Post: 28th January 2013, 09:14
  2. how to populate data in QTableWidget
    By gauravg in forum Qt-based Software
    Replies: 1
    Last Post: 25th March 2011, 12:50
  3. Replies: 3
    Last Post: 1st February 2011, 11:57
  4. How to populate Combobox in QTableView
    By ronak.vashi in forum Newbie
    Replies: 8
    Last Post: 4th December 2010, 17:09
  5. How to populate delegate with model data.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 09:31

Tags for this Thread

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.