PDA

View Full Version : QTreeView restore Expanded node after reload model



patrik08
25th May 2008, 15:06
I insert on a QMap each first level node if is Expanded or not
CollState.insert(cid,isExpanded(node) ? 1 : 0 );

after user chance the page order criteria from a node i must reload the model and restore
all expanded item state..

this is run only on first level ..
How i can register the state from child ?

node.child show not valid ... but exist!

i can not use expandAll the are to many childs..




void PageList::savestate()
{
/* enum COLLAPSENR { COLLAPSEID = Qt::UserRole + 20 }; */
CollState.clear(); /* QMap<int,int> CollState; clear old state */
for (int r = 0; r < model->rowCount(); ++r) {
for (int c = 0; c < model->columnCount(); ++c) {
QModelIndex node = model->index(r,c);
savestate(node);
}
}
}

void PageList::savestate( const QModelIndex node )
{
if (!node.isValid()) {
return;
}
const int cid = node.data(COLLAPSEID).toInt();
if (CollState[cid]) { /* is register on qmap ? after clear */
return;
}
CollState.insert(cid,isExpanded(node) ? 1 : 0 );
savestate(node.child(node.row(),node.column()));
}


/* restore if expanded or not */

void PageList::staterestore( const QModelIndex node )
{
if (!node.isValid()) {
return;
}
const int cid = node.data(COLLAPSEID).toInt();
if (!CollState[cid]) {
return;
}
if (CollState[cid] == 1) {
setExpanded(node,true);
}
staterestore(node.child(node.row(),node.column())) ;
}

jpn
25th May 2008, 20:59
Is it possible that the hierarchy changes when you reload the model or does it remain the same?

patrik08
26th May 2008, 14:02
Is it possible that the hierarchy changes when you reload the model or does it remain the same?

no remain the same ....

I solved on this way ... colums 0 from Category having a nummer and 1 the name
and level 3 pages not...
Now i must only write QMap to Qsetting .



void PageList::savestate()
{
/* enum COLLAPSENR { COLLAPSEID = Qt::UserRole + 20 }; */
CollState.clear(); /* QMap<int,int> CollState; */
for (int r = 0; r < model->rowCount(); ++r) {
for (int c = 0; c < model->columnCount(); ++c) {
QModelIndex node = model->index(r,c);
savestate(node);
}
}
qDebug() << "### SAVE SIZEeeeeeeeeee " << CollState.size();
}

void PageList::savestate( const QModelIndex node , bool fromchild )
{
if (!node.isValid()) {
return;
}
if (node.data().toString().toInt() < 1 || node.column() !=0 ) {
return;
}
const int cid = node.data().toString().toInt();
if (CollState[cid]) { /* is register on qmap ? after clear */
return;
}
CollState.insert(cid,isExpanded(node) ? 1 : 0 );
QStandardItem *item = model->itemFromIndex(node);
if ( item->hasChildren() ) {
for (int i = 0; i < limit_page; ++i) {
savestate(node.child(i,0),true);
}
}
}


void PageList::RestoreCollapseState()
{
if (CollState.size() < 1) {
return;
}
for (int r = 0; r < model->rowCount(); ++r) {
for (int c = 0; c < model->columnCount(); ++c) {
QModelIndex node = model->index(r,c);
staterestore(node);
}
}
}

void PageList::staterestore( const QModelIndex node )
{
if (!node.isValid()) {
return;
}
if (node.data().toString().toInt() < 1 || node.column() !=0 ) {
return;
}
const int cid = node.data().toString().toInt();
if (!CollState[cid]) {
return;
}
if (CollState[cid] == 1) {
////////qDebug() << "### expand on name " << node.data().toString();
///////////qDebug() << "### expand on " << node.row() << node.column();
setExpanded(node,true);
}
QStandardItem *item = model->itemFromIndex(node);
if ( item->hasChildren() ) {
for (int i = 0; i < limit_page; ++i) {
staterestore(node.child(i,0));
}
}
}

jpn
26th May 2008, 17:42
I did it once like this for a static QTreeWidget:


void saveTreeState()
{
QVariantList states;
QTreeWidgetItemIterator it(treeWidget);
while (*it)
{
states += (*it)->isExpanded();
++it;
}
QSettings settings;
settings.setValue("states", states);
}

void restoreTreeState()
{
QSettings settings;
QVariantList states = settings.value("states").toList();
QTreeWidgetItemIterator it(treeWidget);
while (*it && !states.isEmpty())
{
(*it)->setExpanded(states.takeFirst().toBool());
++it;
}
}

Of course, it doesn't apply in your situation Patrik because you're using a model, not QTreeWidget. But perhaps it turns out to be useful for someone who later searches for a solution on the same subject.