Home > Back-end >  Qt set different heights for items in Qtreeview for children
Qt set different heights for items in Qtreeview for children

Time:03-18

I'm currently defining the heights of items in a QTreeView via a stylesheet

QTreeView {
    background: palette(window);
    color: palette(text);
    border: none;
}

QTreeView::item {
    height: 40px;
    padding-top: 0.5ex;
    padding-bottom: 0.5ex;
    margin: 2px;
}

With this all items in the tree will have the same height. Is it possible to define a different height for the children?

CodePudding user response:

I'm not sure if this can be done via a stylesheet. The normal way to achieve this would be to override QAbstractItemModel::data and have it return specific values associated with the Qt::SizeHintRole data role.

QVariant new_model::data (const QModelIndex &index, int role) const
{
    if (role == Qt::SizeHintRole) {

        /*
         * Calculate required size hint based on model data etc.
         */
        QSize size = ...;
        return size;
    }

    /*
     * Defer to base class implementation.
     */
    return base_class::data(index, role);
}
  • Related