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 increase the y gap between two tree items in Abstract tree view.
Forum Updated to NodeBB v4.3 + New Features

How to increase the y gap between two tree items in Abstract tree view.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 314 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.
  • S Offline
    S Offline
    summit
    wrote on last edited by summit
    #1

    In my tree view i want to increase the gap between two tree items in Y axis currently they overlap each other.
    TreeItems.png

    This is my paint function.

    void PixelDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        int offset = 0;
        int offsetIcon = 0;
        int offsetYAxis = 0;
        const QAbstractItemModel* model = index.model();
        const TreeModel* myModel = (TreeModel*)(model);
        TreeItem* item = myModel->getItem(index);
        Container* cont = item->GetContainer();
        QStyleOptionViewItem newOption = option;
        painter->setBrush(newOption.palette.text());
        newOption.rect.setHeight(option.rect.height() + 5);
    
        if (option.state & QStyle::State_Selected)
            painter->fillRect(newOption.rect, newOption.palette.highlight());
    
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing, true);
        painter->setPen(Qt::NoPen);
        painter->translate(newOption.rect.x(), newOption.rect.y());
        iconPos = newOption.rect.x() / pixelIconValue;
        painter->scale(.4, .4);
        if (option.state & QStyle::State_Selected)
            painter->setBrush(newOption.palette.highlightedText());
        else
            painter->setBrush(newOption.palette.text());
    
        // Set the icon for EYE
        {
            QString qstrIconName;
            if (cont->GetActive())
                qstrIconName = "EYE_OPEN";
            else
                qstrIconName = "EYE_CLOSE";
    
            QString qstrIconPath = QCoreApplication::applicationDirPath();
            QPixmap pixmap;
            qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
            pixmap.load(qstrIconPath);
            QImage image = pixmap.toImage();
            painter->drawImage(0, offsetYAxis, image);
            offset += 20;
            offsetIcon += 50;
            offsetYAxis += 20;
            pixelIconValue++;
        }
    
        if (cont->GetGeometry()->GetType() != "NO_TYPE")
        {
            QString qstrIconName = cont->GetGeometry()->GetType().c_str();
            QString qstrIconPath = QCoreApplication::applicationDirPath();
            QPixmap pixmap;
            qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
            pixmap.load(qstrIconPath);
            QImage image = pixmap.toImage();
            painter->drawImage(offsetIcon, offsetYAxis, image);
            offset += 20;
            offsetIcon += 50;
            offsetYAxis += 10;
        }
        
        if (cont->GetNumberOfAnimationChannels() > 0)
        {
            QString qstrIconName = "ANIMATION";
            QString qstrIconPath = QCoreApplication::applicationDirPath();
            QPixmap pixmap;
            qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
            pixmap.load(qstrIconPath);
            QImage image = pixmap.toImage();
            painter->drawImage(offsetIcon, offsetYAxis, image);
            offset += 20;
        }
        painter->restore();
        // Draw text
        QString text = index.data(Qt::DisplayRole).toString();
        QFontMetrics fm(option.font);
        int textWidth = fm.width(text);
        int textHeight = fm.height(); // Get the height of the text
    
        iconPos = option.rect.x();
        newOption.rect.setX(option.rect.x() + offset + 5);
        newOption.rect.setWidth(textWidth + 8);
        // Calculate the y-coordinate to center the text vertically
        int centerY = option.rect.y() + (option.rect.height() - textHeight) / 2;
        newOption.rect.setY(centerY);
        QStyledItemDelegate::paint(painter, newOption, index);
    }
    
    M 1 Reply Last reply
    0
    • S summit

      In my tree view i want to increase the gap between two tree items in Y axis currently they overlap each other.
      TreeItems.png

      This is my paint function.

      void PixelDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
      {
          int offset = 0;
          int offsetIcon = 0;
          int offsetYAxis = 0;
          const QAbstractItemModel* model = index.model();
          const TreeModel* myModel = (TreeModel*)(model);
          TreeItem* item = myModel->getItem(index);
          Container* cont = item->GetContainer();
          QStyleOptionViewItem newOption = option;
          painter->setBrush(newOption.palette.text());
          newOption.rect.setHeight(option.rect.height() + 5);
      
          if (option.state & QStyle::State_Selected)
              painter->fillRect(newOption.rect, newOption.palette.highlight());
      
          painter->save();
          painter->setRenderHint(QPainter::Antialiasing, true);
          painter->setPen(Qt::NoPen);
          painter->translate(newOption.rect.x(), newOption.rect.y());
          iconPos = newOption.rect.x() / pixelIconValue;
          painter->scale(.4, .4);
          if (option.state & QStyle::State_Selected)
              painter->setBrush(newOption.palette.highlightedText());
          else
              painter->setBrush(newOption.palette.text());
      
          // Set the icon for EYE
          {
              QString qstrIconName;
              if (cont->GetActive())
                  qstrIconName = "EYE_OPEN";
              else
                  qstrIconName = "EYE_CLOSE";
      
              QString qstrIconPath = QCoreApplication::applicationDirPath();
              QPixmap pixmap;
              qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
              pixmap.load(qstrIconPath);
              QImage image = pixmap.toImage();
              painter->drawImage(0, offsetYAxis, image);
              offset += 20;
              offsetIcon += 50;
              offsetYAxis += 20;
              pixelIconValue++;
          }
      
          if (cont->GetGeometry()->GetType() != "NO_TYPE")
          {
              QString qstrIconName = cont->GetGeometry()->GetType().c_str();
              QString qstrIconPath = QCoreApplication::applicationDirPath();
              QPixmap pixmap;
              qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
              pixmap.load(qstrIconPath);
              QImage image = pixmap.toImage();
              painter->drawImage(offsetIcon, offsetYAxis, image);
              offset += 20;
              offsetIcon += 50;
              offsetYAxis += 10;
          }
          
          if (cont->GetNumberOfAnimationChannels() > 0)
          {
              QString qstrIconName = "ANIMATION";
              QString qstrIconPath = QCoreApplication::applicationDirPath();
              QPixmap pixmap;
              qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
              pixmap.load(qstrIconPath);
              QImage image = pixmap.toImage();
              painter->drawImage(offsetIcon, offsetYAxis, image);
              offset += 20;
          }
          painter->restore();
          // Draw text
          QString text = index.data(Qt::DisplayRole).toString();
          QFontMetrics fm(option.font);
          int textWidth = fm.width(text);
          int textHeight = fm.height(); // Get the height of the text
      
          iconPos = option.rect.x();
          newOption.rect.setX(option.rect.x() + offset + 5);
          newOption.rect.setWidth(textWidth + 8);
          // Calculate the y-coordinate to center the text vertically
          int centerY = option.rect.y() + (option.rect.height() - textHeight) / 2;
          newOption.rect.setY(centerY);
          QStyledItemDelegate::paint(painter, newOption, index);
      }
      
      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @summit
      If you want to increase the row height, have a look at this topic:
      how-set-height-of-all-rows-qtableview

      EDIT doesn't work for treeview
      I think you have to return the height you want in:

      QSize QItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
      

      See HERE

      S 1 Reply Last reply
      2
      • M mpergand

        @summit
        If you want to increase the row height, have a look at this topic:
        how-set-height-of-all-rows-qtableview

        EDIT doesn't work for treeview
        I think you have to return the height you want in:

        QSize QItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
        

        See HERE

        S Offline
        S Offline
        summit
        wrote on last edited by
        #3

        @mpergand Thank you very much for the answer .

        1 Reply Last reply
        0
        • S summit has marked this topic as solved on

        • Login

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