QStandardItemModel not inserting children
-
Hi All,
The following code does not insert the children (or at least not show then), and im not sure i understand why.
I get the root topnode but no children visible in the QTreeView
any help appreciated..void DialogStructureXML::on_btn_add_shot_released() { QModelIndex currentIndex = ui->tree_shot->selectionModel()->currentIndex(); QModelIndex child = currentIndex; QModelIndex child2 = currentIndex; QModelIndex prevIndex; QStandardItemModel *model = static_cast<QStandardItemModel*>(ui->tree_shot->model()); int rowCount = model->rowCount(currentIndex);//will also be root if no entries in tree int rowCountRoot = model->rowCount(); //will be root as default new QModelIndex() int currentRow = 0; if ( !traceIndex.isValid() ) { if (model->columnCount(currentIndex) == 0) { if (!model->insertColumn(0, currentIndex)) return ; //Column index 0 ItemText if (!model->insertColumn(1, currentIndex)) return ; //Column index 1 Description } if (!model->insertRow(currentRow, currentIndex)) return ; child = model->index(currentRow, 0, currentIndex); //Column index 0 model->setData(child, QVariant( "topNode" ), Qt::EditRole); child2 = model->index(currentRow, 1, prevIndex); //Column index 1 model->setData(child2, QVariant( "[Description]" ), Qt::EditRole); if (!model->insertRow(0, child)) return ; child = model->index(0, 0, child); //Column index 0 model->setData(child, QVariant( "subNode" ), Qt::EditRole); child = model->index(0, 1, child); //Column index 1 model->setData(child, QVariant( "[Description]" ), Qt::EditRole); ui->tree_view->selectionModel()->setCurrentIndex(model->index(0, 0, child),QItemSelectionModel::ClearAndSelect); } }
-
Hi,
From a quick look at your code, it's seems that you don't create any item with a parent.
-
tks, seems like im not understanding the QModelIndex :
is:
QModelIndex currentIndex= ui->tree_shot->selectionModel()->currentIndex();not getting a valid parent to attach a row to?
parent attachment to currentIndex?.if (!model->insertRow(currentRow, currentIndex)) return ;
-
this first insert seems to work as I get the first entry. its the next child insertion that seems to fail.
if (!model->insertRow(currentRow, currentIndex)) return ;
child= model->index(currentRow, 0, currentIndex); // Column index 0
model->setData(child, QVariant( "topNode" ), Qt::EditRole); -
now I use the child to insert, is the the issue?
I thought the following code was also doing the parent attachment.
if (!model->insertRow(0, child)) return ;
child = model->index(0, 0, child); //Column index 0
model->setData(child, QVariant( "subNode" ), Qt::EditRole);would this then be?:
if (!model->insertRow(0, child.parent() )) return;cheers
c -
-
What model are you using ?
-
lol, stamp replies? ....
if you look at the above code post it says:
QStandardItemModel *model = static_cast<QStandardItemModel*>(ui->tree_shot->model());
I have been trying a lot of variations and it seem to come down to:
-
Insure you have to a column
-
find the index
-
set the data.
seems a lot of stuff, to just add data....
the code i am using...index = CreateTopBranch( currentIndex , ui->tree_shot ); if (model->columnCount(index) == 0) { if (!model->insertColumn(0, index)) return; // if (!model->insertColumn(1, index)) return; } if (!model->insertRow(0, index)) return; child = model->index(0, 0, index); model->setData(child, QVariant("[No data]"), Qt::EditRole); child = model->index(0, 1, index); model->setData(child, QVariant("DEscription"), Qt::EditRole); ui->tree_shot->selectionModel()->setCurrentIndex(model->index(0, 0, index),QItemSelectionModel::ClearAndSelect);
-
-
Ok then, are you using a QTreeWidget or QTreeView ?
-
@Trilec said:
QStandardItemModel model = static_cast<QStandardItemModel>(ui->tree_shot->model());
Instance of which class is ui->tree_shot->model()?
The fact that you cast it to QStandardItemModel does not make it one.
If it is not QStandardItemModel or derived class you are in undefined behavior area.
You should not use static_cast for any of QObject derived class anyway. Use qobject_cast and check that value is not nullptr.If you are creating model yourself is ui->tree_shot a QTreeWidget?
If it is custom model you probably did not call proper functions when changing the model.
In this case widget will not be updated properly.