I've been working with Qt for around a year now and I still struggle the most with working with custom Models/Views. Especially tree like structures.
I have a class called Account which represents website like account objects:
class Account:
def __init__(website, name, cookies):
self.website = website # string
self.account = account # string
# list of lists:
# [ ['CookieName1', 'CookieValue1', 'Domain1', 'Path1', 'Expiration1'],
# ['CookieName2', 'CookieValue2', 'Domain2', 'Path2', 'Expiration2'] ]
self.cookies = cookies
In reality the cookies will actually be a custom QNetworkCookieJar subclass, but for testing purposes it's in this form.
I want the model to store the accounts in a list and from that list I will be able to expose the data I want to the view/s
I want to expose these Account objects to multiple views in the following form where I have two views. The one on the left shows just the website and account name in 2 columns. The view on the right shows the cookies (5 columns) for the account that is SELECTED in the left view
I'm struggling to figure out the best way to model this, but what I think I need to do is something like the following where there are 7 columns total(in this example I have two Accounts, first having 3 cookies, and second has only a single cookie):
[Website] [Account] [CName] [CValue] [CDomain] [CPath] [CExpires]
- Website1 Account1 (empty) (empty) (empty) (empty) (empty)
(empty) (empty) CName1 CVal1 Dom1 Path1 Exp1
(empty) (empty) CName2 CVal2 Dom2 Path2 Exp2
(empty) (empty) CName3 CVal3 Dom3 Path3 Exp3
- Website2 Account2 (empty) (empty) (empty) (empty) (empty)
(empty) (empty) CName1 CVal1 Dom1 Path1 Exp1
I would hide the last 5 columns in the left view, and hide the first 2 columns in the right view and on each selectionChanged() signal call setRootIndex() on the right view to select the correct index to show the cookies.
The rowCount() implementation would look something like this:
def rowCount(self, parentIdx):
# If no parent index I assume top-level row, so just return the total amount of Accounts we have
if not parentIdx.isValid():
return len(self.accounts)
# Otherwise get the Account object and return how many cookies we have
account = parentIdx.internalPointer()
return len(account.cookies)
But I'm completely lost on how index() and parent() would be implemented. It's the tree like modeling structure that is confusing to me since I'm not actually displaying a tree structure in any view. Is there a better way to go about doing this? A proxy model perhaps?
Any help/guidance in the right direction would be appreciated,
Thanks