Results 1 to 4 of 4

Thread: Can't change the text display of a QTreeview item.

  1. #1
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Can't change the text display of a QTreeview item.

    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:

    Qt Code:
    1. QAbstractItemModel *Model = const_cast<QAbstractItemModel*>(Current_Model_Index->model());
    2. Model->setData(*Current_Model_Index,Qt::blue,Qt::TextColorRole);
    To copy to clipboard, switch view to plain text mode 

    Similarly, I attempted to change the value of the text using this:
    Qt Code:
    1. Model->setData(*Current_Model_Index,tr("XXXXXXX"),Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

    That did not work. So I checked: Model->data(*Current_Model_Index,Qt::ItemIsEditable).toB ool() 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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't change the text display of a QTreeview item.

    Did you reimplement QAbstractItemModel::flags() to return proper flags for your items?

  3. #3
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't change the text display of a QTreeview item.

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't change the text display of a QTreeview item.

    Quote Originally Posted by johnny_sparx
    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 Code:
    1. Qt::ItemFlags MyModel::flags ( const QModelIndex & index ) const {
    2. if(hasChildren(index)){
    3. return Qt::ItemIsEnabled|Qt::ItemIsSelectable;
    4. } else {
    5. return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable;
    6. }
    7. return Qt::ItemIsEnabled;
    8. }
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. Changing the text color of a QTreeView leaf.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 23rd March 2006, 00:58
  2. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 15:24
  3. Replies: 1
    Last Post: 17th March 2006, 09:19
  4. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 02:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.