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. how to set height of single QTreeWidgetItem in QTreeWidget

how to set height of single QTreeWidgetItem in QTreeWidget

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.5k 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.
  • Z Offline
    Z Offline
    zhuangbin
    wrote on last edited by
    #1

    hi guys

    Do you know how to set height of single QTreeWidgetItem in QTreeWidget? any idea? thx

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums
      One way would be.

      class HeightItemDelegate : public QStyledItemDelegate
      {
      public:
          HeightItemDelegate(QObject *poParent = nullptr) :
              QStyledItemDelegate(poParent)  {}
      
          QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
          {
              QSize oSize = QStyledItemDelegate::sizeHint(option, index);
              int height = index.model()->data( index, Qt::UserRole ).toInt();
              if ( height ) {
                  oSize.setHeight(height);
              }
              return oSize;
          }
      };
      ......
      //use the delegate
      HeightItemDelegate *delegate= new HeightItemDelegate(this);
      ui->treeWidget->setItemDelegate(delegate);
       // set height for an item via model
      auto model = ui->treeWidget->model();
      // get index
      QModelIndex indx = ui->treeWidget->model()->index(1, 0);
      // assing the height value
      model->setData( indx, 50 , Qt::UserRole);
      

      alt text

      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