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. Automatic Row Height in QTreeView
QtWS25 Last Chance

Automatic Row Height in QTreeView

Scheduled Pinned Locked Moved Solved General and Desktop
qtreeviewqstyleditemdeleheightrow
2 Posts 1 Posters 4.1k 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.
  • M Offline
    M Offline
    michalos
    wrote on 22 Dec 2016, 14:12 last edited by
    #1

    Hi,
    Does anyone know how to automatically adjust a row height according to the width of the column, and the length of the text in it?
    I have a QStandardItemModel with a tree structure.
    I only have one column in each row.
    At the start of the application, the height of each row is set correctly, but when I resize the widget(and the tree view) the heights of the rows are not recalculated.
    I have a custom QStyledItemDelegate that draws the content, and calculates the sizeHint(), but it looks like the tree view only checks it when the model is set.
    I've looked for a solution in this and other forums, but there doesn't seem to be an answer.
    Everything works fine in a QTableView, but I need my model to be viewed as a tree.
    Could anyone help me?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      michalos
      wrote on 9 Jan 2017, 13:45 last edited by
      #2

      Ok. I've found the answer.

      I subclass the QTreeView and override the void resizeEvent(QResizeEvent* e) as shown:

      void MyTreeView::resizeEvent(QResizeEvent *e)
      {
          if(model() != Q_NULLPTR){
              model()->layoutChanged();
          }
          QTreeView::resizeEvent(e);
      }
      

      And it did the trick.

      Had help from the Voidrealms Facebook group.

      1 Reply Last reply
      0

      • Login

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