Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView doesn't change its behaviour for child items even if flags are correctly set

QTreeView doesn't change its behaviour for child items even if flags are correctly set

Scheduled Pinned Locked Moved Solved General and Desktop
qtwidgetsqtreeviewflagsc++
2 Posts 1 Posters 412 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    famastefano
    wrote on last edited by
    #1

    Hello,
    I have a very simple Model/View with a custom model and a default QTreeView.

    My model has 2 levels only:
    4765be57-5f5e-4e73-b1e4-893b7560914c-image.png

    I have 6 columns:

    • Type, Name, and Life belong to the top level item
    • Twin, Work, and Usage belong to the childrens

    I need to:

    1. Edit 'Name' and 'Life'
    2. Edit 'Work'
    3. Make anything selectable
    4. Make children be draggable as they will be copied on another widget

    I've set the flags like this:

    constexpr int TYPE_COL = 0;
    constexpr int NAME_COL = 1;
    constexpr int LIFE_COL = 2;
    
    constexpr int TWIN_SUBINDEX_COL = 3;
    constexpr int TWIN_WORK_COL     = 4;
    constexpr int TWIN_USAGE_COL    = 5;
    
    Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
    {
        if(!index.isValid())
            return Qt::NoItemFlags;
    
        int col = index.column();
    
        if(getType(index.internalPointer()) == AbstractToolItem::ToolType::TOP_LEVEL)
        {
            switch(col)
            {
            case TYPE_COL: return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
            case NAME_COL:
            case LIFE_COL: return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
            }
        }
        else
        {
            switch(col)
            {
            case TWIN_SUBINDEX_COL:
            case TWIN_USAGE_COL: return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
            case TWIN_WORK_COL: return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemNeverHasChildren | Qt::ItemIsDragEnabled;
            }
        }
    
        return Qt::NoItemFlags;
    }
    

    Just in case, MyModel::data(....) returns a QVariant() for "invalid columns":

    • Column 3, 4, 5 for top level items
    • Column 0, 1, 2 for childrens

    Everything seems to work only for the top level items and I don't know why...
    Any ideas?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      famastefano
      wrote on last edited by
      #2

      A colleague of mine, that used Qt for years, helped me.
      The culprit was (other than me) that I wrongly created the parent of the children.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved