Results 1 to 9 of 9

Thread: Sharing a QTreeWidget item across two tabs in Pyside2

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

    Default Sharing a QTreeWidget item across two tabs in Pyside2

    I am trying to design a QTabwidget having two tabs in it. The tab should change based on selection from a QComboBox. I have to populate a QTreewidget instance in the two tabs based on the selection from the combo box i.e same tree should be displayed whenever i switch tabs. I tried creating layouts inside tab and added the same Tree to both the layouts. It works for the first time i.e when i switch tabs I can see the same tree in both the tabs.
    Qt Code:
    1. if(self.ui.comboReportsNew.currentText()=="Synthesis"):
    2. self.leftLayout = QtWidgets.QVBoxLayout()
    3. self.ui.tab_2.setLayout(self.leftLayout)
    4. self.leftLayout.addWidget(self.ReqTree) //self.ReqTree is the QTreeWidget
    5.  
    6. elif(self.ui.comboReportsNew.currentText()=="Code Coverage"):
    7. self.rightLayout = QtWidgets.QVBoxLayout()
    8. self.ui.tab.setLayout(self.rightLayout)
    9. self.rightLayout.addWidget(self.ReqTree)
    To copy to clipboard, switch view to plain text mode 
    Next time i try to do that it gives me warning " Attempting to add Layout to widget which already has a layout". I tried creating logic to check if my tree widget already has layout and if so , delete the old layout and initialize it with new layout but no luck.

    Any help will be very appreciated.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    512
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Hi, when do you execute your code snippet? Each time you change the tab?

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    supratik (13th October 2020)

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

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

    Hi, actually each time i change the text in the comboBox...the code snippet is run. Based on the text in the ComboBox I have to change my tabs and show the QTreeWidget

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

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

    Based on the text in the ComboBox I have to change my tabs and show the QTreeWidget
    First, you cannot "share" QTreeWidgetItem instances between two QTreeWidget instances. An item belongs to one or the other.

    You are making the common mistake of "confusing the map for the terrain", that is, thinking that the QTreeWidgetItem -is- the data and not just the way that the data is wrapped to show on screen. The map is not the dirt and rocks under your feet, it is only a picture of it. You can have many maps showing the same rock (the data), but there is only one rock (the terrain).

    You want to make two QTreeWidget instances, one for each tab, when you first construct the tabs and not each time you switch. It is not possible to "share" the same widget between two parents. When you call addWidget(), you are moving the widget from one tab to the other, you are not sharing it.

    The only thing that changing tabs should do is make one widget or the other visible, and QTabWidget takes care of that for you. The errors you are seeing is because you are trying to create a new GUI every time the combo box selection changes.

    So what you should be doing is this:

    1 - Create a data structure that holds the information you want to display in the trees. Make a method that returns you the correct part of the data structure based on the combo box selection.

    2 - When you construct the GUI for the first time (and only the first time), create your tree widgets, layouts, etc. and add them to the tab widget. Save the tree widget pointers as member variables so you can access them later.

    3 - When the combo box selection changes, clear each tree's contents, find the new contents from the data structure, and add new QTreeWidgetItems to each tree using that content. Remember that you cannot add the -same- QTreeWidgetItem to both trees - you have make two QTreeWidgetItems, one for each tree. If you add the same item to two trees, then all that does is move it from the first tree to the second. They cannot be shared.
    <=== 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.

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

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

    First of all thanks for this elaborate explanation. It helped me a lot in clearing my misconception.

    I am very new to Pyside framework. It will be really helpful if you could help me with the first point i.e Make a method that returns you the correct part of the data structure based on the combo box selection. . I am not very sure how to do it.

    How do I add tree widget pointers in Python ?

    Is there any other way I can use the same tree in two tabs. The same tree is used in number of places and if I make two trees just for the sake of switching tabs it creates too much confusion in the other parts of code.
    Sorry for my less knowledge about Pyside and this forum also.

    Thanks again

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,236
    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.

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

    supratik (13th October 2020)

  9. #7
    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

  10. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,236
    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.

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

    supratik (14th October 2020)

  12. #9
    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.