Results 1 to 7 of 7

Thread: How to disable & enable a row in QTreeView

  1. #1
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question How to disable & enable a row in QTreeView

    Hi, I have a QTreeView with custom model derived from QAbstractItemModel. I want to disable the parent item until all of its child items gets populated. Once done, then I have to enable the parent item. How can I achieve this using flags() function?

    I want this kind of behavior for all the tree nodes to avoid user click till it is fully loaded.

    Please help on this

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to disable & enable a row in QTreeView

    Qt Code:
    1. setFlags( index, flags( index ) ~ Qt::ItemIsEnabled ); // disables the item at index
    2. setFlags( index, flags( index ) | Qt::ItemIsEnabled ); // enables the item at index
    To copy to clipboard, switch view to plain text mode 

    Should do it. Call the first one before you populate, call the second one after population is finished. You should probably call setExpanded( index, false ) on the tree view first to ensure that the parent is colapsed before populating the children.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to disable & enable a row in QTreeView

    I am not seeing any in-built setFlags() function on QModelIndex.

    auto flags = sourceIndex.flags();

    flags.setFlag(Qt::ItemIsEnabled,false);

    Don't know how to set this flag again to the sourceIndex

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to disable & enable a row in QTreeView

    I am not seeing any in-built setFlags() function on QModelIndex.
    QAbstractItemModel::setFlags()
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to disable & enable a row in QTreeView

    Hi, I am not able to find any such setFlags() method under QAbstractItemModel

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to disable & enable a row in QTreeView

    Sorry, sorry, my mistake. You need to implement the QAbstractItemModel::flags() method to return the correct set of flags for each item in your model. I was thinking of QTableWidgetItem, which does have a setFlags() method.

    So your tree model should initially return flags without the Qt:: ItemIsEnabled bit set for parents whose children have not been populated. Once all of the children have been loaded, then your model can enable the parent index. I think emitting the dataChanged() signal will cause the view to request the new data and flags for the parent, or possibly just hovering the mouse over the parent item will cause the flags to be queried. You'll have to test that.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to disable & enable a row in QTreeView

    Thanks for your inputs. Will try

Similar Threads

  1. Smart way to enable/disable Controls
    By sp3x in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2016, 09:48
  2. Enable and Disable Close (X)
    By anh5kor in forum Newbie
    Replies: 2
    Last Post: 25th March 2015, 06:45
  3. disable enable network connections?
    By remy06 in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2009, 17:52
  4. Replies: 1
    Last Post: 18th December 2008, 10:25
  5. disable/enable shortcut
    By harakiri in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2007, 10:58

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.