user1406186
user1406186

Reputation: 1002

How to set QTreeWidget row height

I want to set the height of all QTreeWidgetItems in a QTreeWidget to a specified height. I couldn't see anything in Qt Designer or the QTreeWidget class documentation.

Upvotes: 0

Views: 5179

Answers (2)

Wade Wang
Wade Wang

Reputation: 690

Try the following stylesheet for your QTreeWidget:

QTreeWidget::item{
    height:25px;//your preferred height
}

Upvotes: 3

Simon
Simon

Reputation: 1590

In order to modify the tree item row,

create your own customized QItemDelegate and override the sizeHint() function.

For example:

class ItemDelegate : public QItemDelegate
{
private:
    int m_iHeight;
public:
    ItemDelegate(QObject *poParent = Q_NULLPTR, int iHeight = -1) :
        QItemDelegate(poParent), m_iHeight(iHeight)
    {
    }

    void SetHeight(int iHeight)
    {
        m_iHeight = iHeight;
    }

    // Use this for setting tree item height.
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QSize oSize = QItemDelegate::sizeHint(option, index);

        if (m_iHeight != -1)
        {
            // Set tree item height.
            oSize.setHeight(m_uHeight);
        }

        return oSize;
    }
};

Then in your class, set the custome delegate item to the tree,

and change the row height as you like:

class YourClass
{
private:
    QTreeWidget *m_poTreeWidget;
    ItemDelegate m_oItemDelegate;
public:
    void InitTree()
    {
        // do stuff
        m_oItemDelegate.SetHeight(30); // set row height
        m_poTreeWidget->setItemDelegate(&m_oItemDelegate);
        // ...
    }
};

Upvotes: 3

Related Questions