0
votes

I'm trying to show a single QTreeWidgetItem instance on 2 QTreeWidgets, which ends up the item shown only on the 1st tree without being notified. I haven't seen its API doc talks about a limitation if any. Is there a way to workaround?

#!/usr/bin/python
import os
import sys  

from PySide.QtCore import QFile
from PySide.QtUiTools import QUiLoader
from PySide.QtGui import QApplication, QTreeWidget, QTreeWidgetItem, QWidget

class MyTreeWidgetItem(QTreeWidgetItem):
    def __init__(self, *args):  
       super(MyTreeWidgetItem, self).__init__()

class MyWidget(QWidget):
    def __init__(self, *args):  
       super(MyWidget, self).__init__()

       loader = QUiLoader()
       file = QFile('./src/prove_qtreewidget/qtree_mainwidget.ui') 
       file.open(QFile.ReadOnly)
       self.widget_ui = loader.load(file, self)
       file.close()

       item1 = MyTreeWidgetItem(self)
       item1.setText(0, 'Item 1')

       _tw1 = self.widget_ui.findChild(QTreeWidget, '_tree_widget_1')
       _tw2 = self.widget_ui.findChild(QTreeWidget, '_tree_widget_2')
       _tw1.addTopLevelItem(item1)
       _tw2.addTopLevelItem(item1)

if __name__ == '__main__':  
   print("Running in " + os.getcwd() + " .\n")    
   app = QApplication(sys.argv)      
   win = MyWidget()  
   win.show()    
   app.exec_()

.ui file above is available here.

Using Qt 4.8, Ubuntu 12.04

I haven't tried PyQt binding but I just assume shouldn't be any different w/o proof.

1
No. A QTreeWidgetItem can belong to only one QTreeWidget. What are you trying to do?Avaris
I appreciate if you could share a documentation if any about the limit. I'm trying to show a data that I already implemented with QTreeWidgetItem in multiple different trees for different purpose. I know QTreeView fits more for this purpose but I've already implemented non-trivial amount of code..IsaacS
It doesn't say that explicitly (or I couldn't find it) but if you were to add it to multiple tree widgets, item.treeWidget() would be undefined. You're right, model/view is more appropriate if you want to share same model between views. If not, all you can do is add a copy of your item. item_copy = QTreeWidgetItem(item) will do that for you. If you have custom items, you can implement a copy method that uses this underneath.Avaris
That being said, I'd really suggest that you convert to QTreeView. It might be time consuming to convert your current code base, but if you are going to do anything non-trivial it would possibly be better in the long-run.Avaris

1 Answers

0
votes

What you need is a model and a QTreeView, that's for what they are for:

Model/View Programming: Widgets do not maintain internal data containers. They access external data through a standardized interface and therefore avoid data duplication.