1

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

Model/View Multiple Views based on selection

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

Wallboy
  • 154
  • 5
  • 14
  • 2
    I would do it with two models: one represents the websites and another - cookies of a website. On changing selection in left view I would update the model of the right view according to selected object. – vahancho Apr 09 '18 at 06:45
  • Should the cookies model contain ALL the cookies for every website/account and then filtered? Or should the model just be reset on each selection change to only the cookies for a particular website? – Wallboy Apr 09 '18 at 23:51
  • It shouldn't handle all cookies all together - you simply don't show them in that way. I guess your website object contains a list of the corresponding cookies. If so, you just pass this list to the cookies model which will provide this data to the view. I am just guessing now, because I don't know how your data is organized internally. – vahancho Apr 10 '18 at 07:12
  • I provided an example of how the data is organized. I showed the cookies as a list of lists for brevity of the example. In reality it will be a custom QNetworkCookieJar. I take it you mean I would reset the model on each selectionChanged with the correct cookies for the account? – Wallboy Apr 11 '18 at 04:56
  • Yes. Let's assume we have view1 and view2 with model1 and model2 correspondingly. When an item of view1 selected you set the list of cookies to the model2 and reset it so that it will show selected website's cookies only. Your models will be simple ones - each should just handle a list accounts or cookies. – vahancho Apr 11 '18 at 06:34

0 Answers0