QTreeWidget row height



  • Hi,

    I'm having trouble getting the row heights to redraw immediately. They will redraw correctly when another row is added or the window is resized. I've tried creating a custom delegate and using setItemDelegateForRow, as well as setting the individual widgets sizeHint. Any suggestions? Seen several other similar questions googling, but just can't seem to resolve this.

    Thanks in advance!

    @
    class RowDelegate: public QStyledItemDelegate
    {
    public:
    RowDelegate(){}

    private:
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    };

    class TreeWidget: public QWidget
    {
    public:
    TreeWidget(QWidget* pParent);

    private Q_SLOT:
    void onAddRow();

    private:
    QVBoxLayout m_groupLayout;
    QTreeWidget m_treeWidget;
    QPushButton m_addRow;
    };

    TreeWidget::TreeWidget(QWidget* pParent)
    {
    setLayout(&m_groupLayout);
    m_groupLayout.addWidget(&m_treeWidget);

    QTreeWidgetItem *pRow=new QTreeWidgetItem();
    m_treeWidget.insertTopLevelItem(0, pRow);

    m_treeWidget.setItemWidget(pRow, 0, &m_addRow);
    m_addRow.setText("Add Row");

    connect(&m_addRow,&QPushButton::clicked,this,&TreeWidget::onAddRow);
    }

    void TreeWidget::onAddRow()
    {
    QTreeWidgetItem *pRow=new QTreeWidgetItem();
    m_treeWidget.insertTopLevelItem(m_treeWidget.topLevelItemCount()-1,pRow);//append before buttons
    m_treeWidget.setItemWidget(pRow,0,new QLabel("blank row"));
    m_treeWidget.setItemDelegateForRow(m_treeWidget.topLevelItemCount()-2,new RowDelegate());
    pRow->setSizeHint(1,QSize(50,55));//was setting item widget sizehint for calculating TreeWidget sizeHint
    updateGeometry();
    }

    QSize RowDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    return QSize(50,55);
    }
    @



  • simplified the above code a bit more, but still having trouble finding a solution/what I'm doing wrong? Still relatively new to Qt - can anyone point me in the right direction?


Log in to reply
 

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