Automatic Row Height in QTreeView



  • 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?



  • 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.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.