QTreeView not displaying model
-
I am using Qt4 and I am trying to display a model with a QTreeView. The model that I have is composed of several batch process that each batch has some elements and each element has some childs (the elements of the same batch have the same number of childs, but I do not think that is relevant for the problem). Similar to this:
+ | +-+ Batch_0 | + | | | +-+ Element_0 | | + | | | | | +-+ Child_0 | | | | | +-+ Child_1 | | | | | +-+ Child_2 | | | +-+ Element_1 | | + | | | | | +-+ Child_0 | | | | | +-+ Child_1 | | | | | +-+ Child_2 | | | +-+ Element_2 | + | | | +-+ Child_0 | | | +-+ Child_1 | | | +-+ Child_2 | +-+ Batch_1 + | +-+ Element_0 | + | | | +-+ Child_0 | +-+ Element_0 + | +-+ Child_0
My widget is:
class QMainWidget : public QMainWindow { Q_OBJECT public: explicit QMainWidget(QWidget *parent = 0); ~QMainWidget(); public slots: void load(); void load2(); private: Ui::QMainWidget *ui; QDataItemTree* tree_view_model_; int n_loaded_; };
with implementation:
QMainWidget::QMainWidget(QWidget *parent) : QMainWindow(parent),n_loaded_(0), ui(new Ui::QMainWidget) { // Set up Ui ui->setupUi(this); // Data tree tree_view_model_ = new QDataItemTree("Root"); ui->treeView->setModel(tree_view_model_); // Connect load connect(ui->addElementsLoad_pushButton,SIGNAL(clicked()),this,SLOT(load())); connect(ui->addElementsLoad2_pushButton,SIGNAL(clicked()),this,SLOT(load2())); } QMainWidget::~QMainWidget() { delete ui; delete tree_view_model_; } void QMainWidget::load() { // How many elements to add int n_elements = QInputDialog::getInt(this,"Elements","How many elemtns?",10); // How many childs per element int n_childs_per_element = QInputDialog::getInt(this,"Elements","How many elemtns?",10); // Add batch to tree QDataItem* batch = tree_view_model_->addBatch(n_loaded_); for (int i = 0; i < n_elements; i++) { QDataItem* element = tree_view_model_->addBatchElement(i,batch); for (int j = 0; j <= n_childs_per_element; j++) { tree_view_model_->addBatchElementChild(j,element); } } n_loaded_++; ui->treeView->setModel(tree_view_model_); } void QMainWidget::load2() { // How many elements to add int n_elements = QInputDialog::getInt(this,"Elements","How many elemtns?",10); // How many childs per element int n_childs_per_element = QInputDialog::getInt(this,"Elements","How many elemtns?",10); // Add batch to tree tree_view_model_ = new QDataItemTree("Root"); QDataItem* batch = tree_view_model_->addBatch(n_loaded_); for (int i = 0; i < n_elements; i++) { QDataItem* element = tree_view_model_->addBatchElement(i,batch); for (int j = 0; j <= n_childs_per_element; j++) { tree_view_model_->addBatchElementChild(j,element); } } n_loaded_++; ui->treeView->setModel(tree_view_model_); }
My problem is that when I try to add things to the model, it is only displayed if I use the function
load2()
but not withload()
. The only difference between the two functions is that inload2()
I create a new model each time it is called instead of adding elements to the model that has already set as the tree view model (see the code, avoid commenting on the memory leak, I already know it). I cannot create a new model every time because I want to add things to the existing model, not create a new one.The simplified version of what I am trying to do can be found here.
I have read this tutorial to be able to implement what I want.
Thank you in advance.
-
Hi,
You just update your internal data structure. You don't use any of the begin/endInsertRows and related methods. This means that the widget where you set this model are never notified that something happened.
-
Could you please explain how do I need to use begin/endInsertRows and related methods? I have tried adding it at the end of the functions that I use to add data to my model
QDatatItermTree::addBatch
,QDataItemTree::addBatchElemen
andQDataItemTree::addBatchElementChild
but I get a segmentation fault. The updated functions look like:QDataItem* QDataItemTree::addBatch(int number) { QString name = "Batch_"+QString::number(number); QList<QVariant> d; d<<name; QDataItem* batch = new QDataItem(d,rootItem); rootItem->appendChild(batch); endInsertRows(); return batch; } QDataItem* QDataItemTree::addBatchElement(int number, QDataItem* parent) { QString name = "Element_"+QString::number(number); QList<QVariant> d; d<<name; QDataItem* element = new QDataItem(d,parent); parent->appendChild(element); endInsertRows(); return element; } QDataItem* QDataItemTree::addBatchElementChild(int number, QDataItem* parent) { QString name = "Child_"+QString::number(number); QList<QVariant> d; d<<name; QDataItem* element_child = new QDataItem(d,parent); parent->appendChild(element_child); endInsertRows(); return element_child; }
-
It's explained in e.g. the methods documentation.