I'm trying to show a single QTreeWidgetItem
instance on 2 QTreeWidget
s, 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.
QTreeWidgetItem
can belong to only oneQTreeWidget
. What are you trying to do? – Avarisitem.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 acopy
method that uses this underneath. – AvarisQTreeView
. 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