How to read/update widget(s) in QTreeView
Solved
General and Desktop
-
Hello,
I'm trying to implement a program that reads and updates a row in QTreeView using indexWidget, but no luck so far. (indexWidget always returns None... :<
Would you correct me to a right direction?
My current test code is as follows.import sys, os, pprint, time from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class App(QWidget): def __init__(self): super().__init__() self.vbox = QVBoxLayout() self.view = QTreeView() self.view.setSelectionBehavior(QAbstractItemView.SelectRows) self.model = QStandardItemModel() self.model.setHorizontalHeaderLabels(['col1', 'col2', 'col3']) self.view.setModel(self.model) self.view.setUniformRowHeights(True) self.vbox.addWidget(self.view) self.setLayout(self.vbox) for i in range(3): parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i)) for j in range(3): child1 = QStandardItem('Child {}'.format(i*3+j)) child2 = QStandardItem('row: {}, col: {}'.format(i, j+1)) child3 = QStandardItem('row: {}, col: {}'.format(i, j+2)) parent1.appendRow([child1, child2, child3]) self.model.appendRow(parent1) # span container columns self.view.setFirstColumnSpanned(i, self.view.rootIndex(), True) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # expand third container index = self.model.indexFromItem(parent1) self.view.expand(index) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # select last row selmod = self.view.selectionModel() index2 = self.model.indexFromItem(child3) selmod.select(index2, QItemSelectionModel.Select|QItemSelectionModel.Rows) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #view.show() # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def get_data_row(self, row): row_wgt = self.view.indexWidget(self.model.createIndex(1,1)) self.model.appendRow(row_wgt) if __name__ == '__main__': app = QApplication(sys.argv) wgt = App() wgt.show() wgt.get_data_row(0) sys.exit(app.exec_())
Many many thanks in advance for your help.
Regards,
Sat -
Hi
The data is in the model. Not the view.
So you would use its Data function
http://doc.qt.io/qt-5/qabstractitemmodel.html#data
to read and insert new data to the model with
insertRow -
@ShinSat said in How to read/update widget(s) in QTreeView:
dataChanged
hi
Its normal to emit that to tell the view(s) that model/data has changed.
But if you didnt make you own setData() , it might do it automatically.