Results 1 to 16 of 16

Thread: Best approach with model/view implementation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Best approach with model/view implementation

    I removed the beginInsertRows() and endInsertRows. I replaced it with an 'emit layoutChanged()' and now it seems to work. Is thier any downside to doing it this way? The insertRows() function I posted above was for test purposes only. Now that I can insert a row, would it be best to put a generic or blank record in and then go back with a setData() or just put the valid data in immediately? I'll be using a dialog to enter the new row.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best approach with model/view implementation

    Quote Originally Posted by awhite1159 View Post
    I removed the beginInsertRows() and endInsertRows. I replaced it with an 'emit layoutChanged()' and now it seems to work. Is thier any downside to doing it this way?
    Yes, the whole model needs to be reread by the view.

    would it be best to put a generic or blank record in and then go back with a setData() or just put the valid data in immediately? I'll be using a dialog to enter the new row.
    Put the valid data immediately, for instance like this:

    Qt Code:
    1. void myModel::appendNewRow(.... someData){
    2. beginInsertRows(QModelIndex(), rowCount(), rowCount());
    3. MyStruct struct = someData;
    4. m_list << struct; // m_list is the container the model operates on
    5. endInsertRows();
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Best approach with model/view implementation

    Thank you Wysota.

    The only thing I need to be clear on is whether or not the model requires the custom data be in a central repository or container. With the current code since I am still in the process of learning Qt and how it works, I chose to populate the data prior to instantiating the model. I noticed while tracing there are no calls to setData(). All the calls are to the models data() function. So this would imply to me that the current implementation of my model interface to my custom data only works correctly for existing data in the custom data structure as a whole. Hence, that is why the layoutChanged() works and beginInsertRows/endInsertRows does not.

    My model is not aware of a central repository. I simply provide pointers during the intitalization of the model as to where the data resides on the heap via the parent/child members of my base class.

  4. #4
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Best approach with model/view implementation

    I know that you assume that I have a good working knowledge of C++ and I believe I do. But it is always a learning process. I am no where being an expert as I only do this in my recreation. It seems that a large number of the problems are not with Qt but instead with the poster's knowledge of C++. I am probably one of those and I thank you for supporting me.

    I have no copy constructor for my custom base class. I utilize the default implicit copy constructor. Could that be the problem? Does the model need more than a shallow copy of the custom data structure?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best approach with model/view implementation

    Quote Originally Posted by awhite1159 View Post
    The only thing I need to be clear on is whether or not the model requires the custom data be in a central repository or container.
    No, it doesn't. There can even be no real data at all, it can all be generated on the fly (for example the multiplication table).

    I chose to populate the data prior to instantiating the model.
    This is fine as long as the data doesn't change behind the model's back.

    Hence, that is why the layoutChanged() works and beginInsertRows/endInsertRows does not.
    No, this is wrong. beginInsertRows()/endInsertRows() doesn't modify the data in any way, it just informs the views that rows are being added in a particular place so that they know they might need to update their viewports.

    I simply provide pointers during the intitalization of the model as to where the data resides on the heap via the parent/child members of my base class.
    This is fine.

    Quote Originally Posted by awhite1159 View Post
    I have no copy constructor for my custom base class. I utilize the default implicit copy constructor. Could that be the problem?
    No, as long as you know what you are doing with the pointers inside the structure.

    Does the model need more than a shallow copy of the custom data structure?
    The model doesn't need anything. All access to the data is performed through methods you implement, so you have total control over what is going on.

  6. #6
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Best approach with model/view implementation

    Thanks Wysota.

    I understand your responses and thank you.

    Well, I am completely at a loss and there must be a problem with how I have implemented this. I will examine all my code and report back on my findings.

  7. #7
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Best approach with model/view implementation

    Silly problem corrected. My insertRows() function had parameters which passed the starting row, the count, and the parent. I was using the value for the count in the beginInsertRows() as it's last parameter. So the call to beginInsertRows() ended up being invalid:

    beginInsertRows(startingrow, count, QModelIndex());

    This would not work since it had a negative range since the actual call I was making had the strting row as 10 and the ending row as 1 when I attempted to insert 1 row at the end of the list.

    Thank you Wysota for all your help.

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.