Solved How to update QTreeView?
-
@JonB if I regenerate then it doubles the information.
-
@ahsan737 ,
That's why I said you need to clear all list first. :)Regards,
Mucip:) -
@ahsan737 , @Mucip
Sorry, I don't understand these two posts. If yourQTreeView
is bound to a model, it shows what is in that model. It does not "retain previous stuff". Where do you get "doubles the information" or "need to clear it first" from? What are you talking about clearing, there is noQTreeView::clear()
? Obviously "regenerate the model" does not mean "append to whatever is presently there". -
I think the OP did not use a proper model but rather the (in)famous
setItemWidget
function... -
-
@JonB ,
I mean;
ui->someTreeWidget->clear();Regards,
Mucip:) -
@Mucip
OK, but that isQTreeWidget::clear()
, and the OP's question is "How to updateQTreeView
",QTreeWidget
has never been mentioned. I'm sure he would ask aboutQTreeWidget
not justQTreeView
if that's what he wants to know about. Ho-hum. -
@J-Hilk I am creating the QTreeView model using this method:
//--------------------Tree View Model-------------------------- QStandardItemModel *model=new QStandardItemModel (this); rootItem=model->invisibleRootItem(); QStandardItem *TitleItem=new QStandardItem ("\n"+Title_line.at(i)+"\n"+second_line.at(i)+"\n"+third_line.at(i)); TitleItem->setIcon(QIcon(":/img/img/saved_data.jpg.png")); //defining tree structure rootItem->appendRow(TitleItem); ui->treeView->setModel(model);
-
@ahsan737
I did say earlier to answer both your questions:@ahsan737
If you repopulate (or regenerate) the model by reloading the file, the QTreeView will see that and refresh for you, without you needing to do anything else.
.
You can also recognise that an external file has been modified via https://doc.qt.io/qt-5/qfilesystemwatcher.html.You said:
@JonB if I regenerate then it doubles the information.
But it should not do that. So what did you do?
-
@JonB thank you for the considerate response, can you please help with a sample code then it will be easy for me to understand, I am not good with Qt.
-
@ahsan737
You should show what code you have used to "regenerate then it doubles the information". -
This post is deleted! -
@JonB I have done that successfully, thank you for guidance. I was only clearing model and rootItem data but forgot to reset variable values.
-
@ahsan737
This is why it's always good to show code :)