PDA

View Full Version : 2 Views 1 Model



PrimeCP
2nd October 2007, 00:25
I have two QTreeViews using the same model (this works just fine).

The TreeViews have a tree structure where nodes can be checked (clicked with a checkbox).

I want one view to allow the user to change which nodes are clicked, and the other to not allow them to change them. I can't figure out how to do this. Setting one TreeView to be disabled isn't what I want because then they can't traverse the tree (which I still want). I just want it so that the user can't deselect a node in one view. Is there some way I can intercept all user interaction on the TreeView and only allow node traversal, ignoring the check box clicks?

Thanks

jacek
2nd October 2007, 01:25
You can write a proxy model, that will alter the value returned by flags().

PrimeCP
2nd October 2007, 01:32
Yeah I have a model implementation, but I'm not sure how that helps since there will be instances where both views will be displayed at the same time, one editable and the other not allowing the check box clicks. How would I differentiate in the model between the 2 views?

I have something like this in the flags method of the model:



if (index.column() == 0)
{
//allow checkbox
return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable |
Qt::ItemIsTristate;
}
else
{
//don't allow check box
return 0;
}

jacek
2nd October 2007, 01:40
How would I differentiate in the model between the 2 views?
You will have two models --- original one and a proxy model.


class ProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
...
virtual Qt::ItemFlags flags( const QModelIndex & index ) const
{
Qt::ItemFlags f = QSortFilterProxyModel::flags( index );
if( _checkingDisabled ) {
f &= ~ Qt::ItemIsUserCheckable;
}
return f;
}
...
};

...

ProxyModel * proxyModel = new ProxyModel();
proxyModel->setSourceModel( originalModel );

firstView->setModel( originalModel );
secondView->setModel( proxyModel );