Reputation: 955
How do I link a tree view to a table view using Qt? For example, I have a treeView
with a list of items in it. I also have a tableView
with a model/view implementation so that data from different files populate the table. So let's say the filename of each file (in this case, all files are CSVs) is listed as an item in the treeView
. What I'd like to do is link each .csv
item in the treeView
to show the parsed contents of the .csv file that is selected in the treeView
. I'd like that data to show in the tableView
. I have implemented both the tree and table separately - and they work - I just don't know how to link them together. How do I make my parsed data show in the table only after I select the corresponding item in the treeview?
Upvotes: 2
Views: 860
Reputation: 73041
The Qt itemviews system won't give you this functionality automatically, but it's easy to get the behavior using a signal/slot connection.
When the user selects a different row in the QTreeView, the QTableView should repopulate itself with the contents of the .CSV file that row in the QTreeView represents. This can be accomplished by connecting the selectionChanged() method of your QTreeView's SelectionModel object to a slot that will perform the table-repopulation operation. i.e. something like:
connect(myTreeWidget->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), someObjectInYourProgram, SLOT(RepopulateTableView()));
... and then have your RepopulateTableView() slot-method look at which row(s) are currently selected in the QTreeView object, and repopulate its contents based on them.
(side note: you can have RepopulateTableView() use the arguments directly from the selectionChanged() signal, but I often find it more useful to have it examine the QTreeView's selectionModel object using a pointer that is supplied separately, since that way RepopulateTableView() can be easily called from other contexts besides this signal)
Upvotes: 1