Results 1 to 9 of 9

Thread: Sharing a QTreeWidget item across two tabs in Pyside2

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sharing a QTreeWidget item across two tabs in Pyside2

    How do I add tree widget pointers in Python ?
    The same way you probably already did it: self.ReqTree = QtWidgets.QTreeWidget, except that now you need two of them, one for each tab.

    Make a method that returns you the correct part of the data structure based on the combo box selection.
    I don't know what the data is that you are displaying in your tree widget. I assume it is some hierarchical data because you chose a tree to display it. I also assume that, based on the combo box selection, you are only displaying part of the data in the tree, not all of the data.

    I am not that good at Python, but a Python list seems like one way to construct a tree-shaped data structure. A list containing lists, which contain lists, and so on. So say your top-level list has five items in it, and the combo box is used to choose one of those five items. You then want to display the list hierarchy that starts with that item.

    You have two ways to find that place in the top-level list. If the top-level list items are the same strings you put in the combo box, then it is easy - you just look at each item in the top-level list until you find the match. If the strings in the combo box are not in the list, then you can use the alternative signal from the combo box, the one that returns the integer index of the item that was selected. Using that index, you get the top-level list item at that location.

    Qt Code:
    1. theList = [[A, [Aa, [a_i, a_ii, a_iii], [a_iv, a_v, a_vi], [a_vii, a_viii, a_ix], ...], [b, [b_i, b_ii], [b_iii, b_iv, b_v]], ...], [B, [Bb, [...]]], ...]
    To copy to clipboard, switch view to plain text mode 

    theList at index 0 gives you [A, [Aa, [a_i ...]]] and you build your tree widget items starting there.

    You might be using a different data structure, but the idea is the same - find a way to locate the place you want based on the combo box selection, and use that to build the tree.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    supratik (13th October 2020)

  3. #2
    Join Date
    Aug 2019
    Posts
    15
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sharing a QTreeWidget item across two tabs in Pyside2

    Thanks again for the elaborate explanation for the list data structure.
    Actually I am reading data from xml file and storing it in form of QTreeWidget with parents and child as the different elements of the tree.
    As evident from your post, I have to create two different widgets for two different tabs and I can't switch them between tabs..

    I also thought of creating two different tree widgets , but the linkages of the tree in different places of the code was forcing me to not create another instance and just try to switch the same instance between tabs.
    But now I guess I have to create two different treewidgets and display them in different tabs , according to the ComboBox selection .

    Thanks for the help

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sharing a QTreeWidget item across two tabs in Pyside2

    If you are reading from an XML file, then I suggest you read the file into a QDomDocument instance. If you keep the data this way, it makes it easy to find an element at any level of the document, using QDomDocument::elementsByTagName(). It is also easy to build the tree from that point - just follow the QDomNodeList down deeper into the document.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    supratik (14th October 2020)

  6. #4
    Join Date
    Aug 2019
    Posts
    15
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sharing a QTreeWidget item across two tabs in Pyside2

    Thanks a lot
    I created two instances of the QTreeWidget and modified my code.
    It was worth learning for newbie like myself.
    I will use this forum again and hope to learn more

Similar Threads

  1. Replies: 0
    Last Post: 21st October 2013, 09:16
  2. Replies: 4
    Last Post: 8th April 2011, 20:59
  3. Sharing the same widget object between several tabs
    By Lipton in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2010, 15:21
  4. popupmenu for QTreeWidget's item when a particular item selected.
    By vinod sharma in forum Best Practices in Qt Programming
    Replies: 1
    Last Post: 22nd January 2010, 10:45
  5. Replies: 1
    Last Post: 20th January 2010, 08:38

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.