PDA

View Full Version : Can't change the text display of a QTreeview item.



johnny_sparx
1st June 2006, 21:34
I have a model-view set up and so far, they appear synchronized. Now my problem appears when I am trying to change the text in one of the QTreeView leaves.

Given a QModelIndex, I can change the leaf's color using something like:


QAbstractItemModel *Model = const_cast<QAbstractItemModel*>(Current_Model_Index->model());
Model->setData(*Current_Model_Index,Qt::blue,Qt::TextColo rRole);

Similarly, I attempted to change the value of the text using this:

Model->setData(*Current_Model_Index,tr("XXXXXXX"),Qt::DisplayRole);

That did not work. So I checked: Model->data(*Current_Model_Index,Qt::ItemIsEditable).toBo ol() and this returned false.

I tried to change this property to true but it would not hold (the values was not retained when checked). How can I achieve my goal of assigning a text value to a tree leaf given its QModelIndex?

wysota
1st June 2006, 21:41
Did you reimplement QAbstractItemModel::flags() to return proper flags for your items?

johnny_sparx
2nd June 2006, 00:28
No, I did not actually.... I did not think I had to!

What is wrong with the default flags()? For that matter, shouldn't I be using something like setFlags?

wysota
2nd June 2006, 01:03
What is wrong with the default flags()?
It depends which model you use. If you use a direct subclass of QAbstractItemModel, then it doesn't allow items to be modified. You should make sure it returns ItemIsEditable as part of the flags.


For that matter, shouldn't I be using something like setFlags?
No. The model itself should know whether a particular item is to be editable (or otherwise interactable) and should return proper flags for the item. If you want something like "setFlags", you should somehow set/change the internal state for an item and tell the model to act accordingly upon this. For example you might implement the model in such a way that it won't allow items to be editable if they contain children. It would look more or less like so:

Qt::ItemFlags MyModel::flags ( const QModelIndex & index ) const {
if(hasChildren(index)){
return Qt::ItemIsEnabled|Qt::ItemIsSelectable;
} else {
return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsE ditable;
}
return Qt::ItemIsEnabled;
}

Allowing a user to directly modify the state of any item could ruin the model.