PDA

View Full Version : QDataWidgetMapper with QTreeView



Nightfox
30th August 2009, 09:01
Hi All

On a QWidget I've displayed a QTreeView and QTabWidget. They both have the same QAbstractItemModel as source model and the idea is to view the treestructure in the treeview but let the user modify the individual items in the QTabWidget where the items are also shown. Basically exactly the same as in C++ GUI Programming with Qt 4, the staff manager example (relevant section starts p.324).

Everything works according to plan but QDataWidgetMapper will only display items from the QTreeView column 0 so only top level treeitems are displayed in the QDataWidgetMapper.

I initiate the QDataWidgetMapper with;


QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
mapper->setModel(mytreemodel);
mapper->setRootIndex(myTreeView->rootIndex() );

//connect model to mapper
connect(myTreeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
mapper, SLOT(setCurrentModelIndex(QModelIndex)));


With this setup only top nodes will be displayed! I've tried with



connect(myTreeView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
mapper, SLOT(setCurrentModelIndex(QModelIndex)));

and


connect(myTreeView->selectionModel(), SIGNAL(currentColumnChanged(QModelIndex,QModelInde x)),
mapper, SLOT(setCurrentModelIndex(QModelIndex)));


and both of them at the same time but no luck.

Any idea out there how I can get the QDataWidgetMapper to change with the QTreeView selection change?

Thanks

wysota
31st August 2009, 23:22
You have to adjust the root index of the data widget mapper.

Nightfox
1st September 2009, 20:58
Hi wysota

Thanks for your reply. I changed the slot from the connect above with a custom slot which is called each time selected item changes in the treeview. The new code is



connect(myTreeView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
this, SLOT(rowcolchange(QModelIndex) ));

void myQWidget::rowcolchange(const QModelIndex & index )
{
mapper->setRootIndex(index.parent());
mapper->setCurrentModelIndex(index);
}


Now it works - thanks again!