Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidget row height
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget row height

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BDMarler
    wrote on last edited by
    #1

    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);
    }
    @

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BDMarler
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved