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 - Hide controls for expanding and collapsing specific items
QtWS25 Last Chance

QTreeView - Hide controls for expanding and collapsing specific items

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtreeviewqabstractitemmo
3 Posts 3 Posters 4.3k 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.
  • P Offline
    P Offline
    pablo_worker
    wrote on last edited by pablo_worker
    #1

    Hi,
    I have a custom model (derived from QAbstractItemModel) that it is shown with a QTreeView.

    When an item of the model has children, the QTreeView shows a expanding/collapsing icon (a triangle) that allows you to show/hide the children. I want to hide the icon on an specific item. This item has children so it has to be expanded always. I want to achieve the following style for the tree (I will use the symbols + and - as the expanding/collapsing icon)

    A
     B
     C
     D <-I don't want to show the expanding/collapsing icon in this item.
      -E
        E1
        E2
      +F
      -G
        -G1
          G1.1
          G1.2
        G2
    

    I have been able to hide the expanding/collapsing icon of the first item (the A) with the following command:

    myQTreeView.setRootIsDecorated(false);
    

    However, I don't know how to hide the icon for the item D in my example.

    Any ideas?

    Thank you.

    1 Reply Last reply
    0
    • michalosM Offline
      michalosM Offline
      michalos
      wrote on last edited by
      #2

      I hope that someone will suggest a simpler solution, because mine would require some work, but:

      You could use a custom item delegate (derived from QStyledItemDelegate) and override the paint() and sizeHint() method.

      In paint() You draw what You need (icons, text, QTextDocument, etc.) for a specific item. You could use Qt::UserRole to specify the items, that need special drawing like this:

      if(index.data(Qt::UserRole).toInt() == 1) { 
      //draw differently 
      } else { 
      //draw standard
      QStyledItemDelegate::paint(painter, option, index); 
      }
      

      In sizeHint() You declare the size of a single item.

      http://doc.qt.io/qt-5/qstyleditemdelegate.html

      http://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html

      I'm using this method for something slightly different, but it should work for You as well.

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        rather than the delegate I'd reimplement QTreeView:
        protected:

        virtual void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const
        virtual void drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        

        and slots

        void collapse(const QModelIndex &index)
        void expand(const QModelIndex &index)
        

        But agree is not straightforward and will require some work

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2

        • Login

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