How to increase the y gap between two tree items in Abstract tree view.
Solved
General and Desktop
-
In my tree view i want to increase the gap between two tree items in Y axis currently they overlap each other.
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); }
-
@summit
If you want to increase the row height, have a look at this topic:
how-set-height-of-all-rows-qtableviewEDIT 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
-