Getting QTreeWidgetItem's height

  • Hi. Sorry for this stupid question, I just couldn't find any solution for my problem. I need to get the height of QTreeWidgetItem. QTreeWidgetItem::sizeHint always returns -1. I just can't figure out what I'm doing wrong.

    @ QTreeWidgetItem *row = new QTreeWidgetItem();

            row->setText(1, fi.fileName());
            row->setText(3, history->createDate().toString(trs("DateTimeFormat")));
        qDebug() << row->sizeHint(1).height(); // Always -1


  • Yes, sizeHint should be set using setSizeHint() by yourself, otherwise a default QSize() will be returned.


  • Ok, thanks but that doesn't answer my question. How should I get the height of my QTreeWidgetItem?

  • You might try the protected function
    int rowHeight ( const QModelIndex & index ) const

    of your QTreeWidget (defined in QTreeView). I am not sure why it is protected, because I don't know that interface very well. If you can not access protected functions in that context you could define a new function that exposes this information publicly. But there must be a reason why Qt doesn't do that in the first place, so probably there is a more elegant solution.

  • I forgot to mention: you can get the QModelIndex by calling the function
    QModelIndex QTreeWidget::indexFromItem ( QTreeWidgetItem * item, int column = 0 ) const

  • The height of the result of this QTreeWidget method, called with a given QTreeWidgetItem, does the trick (for QTreeWidget -- this isn't usable for QTreeView, of course):

    QRect QTreeWidget::visualItemRect (const QTreeWidgetItem* item) const;

Log in to reply

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