Hi!

I have been using Qt the last couple of months for a class project and I am really loving it especially the QML part of it. It really is exactly what I would like to work with. However, I have gotten stuck at a certain obsticle the last couple of weeks and I would really appreciate some help with it.

The application that I am building is basically an interface to manage, sort and filter through Pokémon cards in the users collection. The user creates a new Pokémon cards album and populates it with Pokémon Cards.

Here is how the UI looks like:


For example:
The user creates two albums, one called "Favorites" and another called "Wishlist". The user can then add cards to those Albums. Once that is done, the user will be able to view the available albums in a tab or a page and clicking on an album will open up a view that display the cards in that album.

I have found different ways to go about this implementation. One of them uses a TreeItem class and TreeModel class to create this nested QList of objects. The problem is that I will need to have different classes with different properties and children depending on where they are in the Tree. Here is a diagram I made to show what I mean:


Since the Card objects won't exist outside of Albums, I have decided to pass them to the Albums directly rather than using pointers. However, this means that appending a card to an Album will demand a copy constructor if I use the following method:
Qt Code:
  1. avoid Album::addCard(const Card &scarf)
  2. {
  3. BeginInsertRows();
  4. m_cards << card;
  5. EndInsertRows();
  6. }
To copy to clipboard, switch view to plain text mode 

The problem here is that I keep running into problems with the creating the Copy constructor and any methods that I need to add to the class to make the constructor work.
So here is how the approach that I took looks like:

main.cpp:
I included the three header files for the classes App, Album and Card and registered them using qmlregistertype. I also created an instance of the App class and set it as root context property to use the models later on.

App:
This class basically fulfills the uppermost parent in the tree. It has no properties such as name and so on. Just the minimal tree root data methods to add and remove Albums to and from it on run time in it's QList<Album> m_album private member. It also has a NameRole to get the name of the Albums it contains.

Album:
It is added to an App object instance. It points to it's parent through a private App* m_parentApp variable. It has a QString m_name and it can add cards to it's QList<Card>m_cards private list. It also has several Roles used to retrieve information about cards in it using the data method.

Card:
It is added to Albums, has no list of children. Points to it's parent Album. Has many variables such as Name, ID, ImageURL and so on.

All the classes inherit QAbstractListModel publicly. Not sure if they really should but that's what I have found people recommending on threads.

The problem with creating the copy constructor (Album(const Album &source) is that when I am adding an Album to an App (m_albums << album) is this:
I am not really sure how to go about iteratating through the children appended to the object being copied.

I created a for() loop that ends at m_cards.count(). Then I noticed that I needed a way to access the card data in the Album being copied for each iteration but I have found no way to make it work. Using the source.data(index, role) expects a QmlIndex rather than an int. I tried dynamic_casting, not sure if I should but that didn't work either so I thought that I should create a new method that retrieves the Card from the Album by value or reference so that my copy constructor works but haven't had any success there.
I tried:
Qt Code:
  1. Card getCard(int row)
  2. {
  3. Return m_cards.at(row);
To copy to clipboard, switch view to plain text mode 
}
But that didn't work, I tried using Card* and Card& as well as m_cards[index.row()] but that didn't work.
The only reason I am doing this is so that my copy constructor which is automatically called when appending an album to and App or a Card to an Album can do something like this:
Qt Code:
  1. For all the children in the objects QList:
  2. This->name = source.m_cards.name();
To copy to clipboard, switch view to plain text mode 

I would appreciate some help with this. Feel free to give any feedback about the approach or suggest a different too if you think that something else would be better.

Thanks.