Solved get Tree Item rect on mousePressEvent
-
I use QTreeView with model. How can i get Widget or Rect of QTreeView item in his mousePressEvent.
I try
void CSProjectsTreeView::mousePressEvent(QMouseEvent *in_pEvent) { bool l_bDefault = true; if(in_pEvent->button() == Qt::LeftButton) { QPoint l_Pos = in_pEvent->pos(); QModelIndex l_Index = indexAt(l_Pos); CSTreeModel* l_pModel = (CSTreeModel*)((QTreeView*)parent())->model(); CSTreeItem* l_pItem = l_pModel->getItem(l_Index); if(l_pItem->getType() == PROJECT_TREE_ITEM_PROJECT) { QPixmap pix(":/Images/Resources/Delete.ico"); QSize size = pix.size(); QWidget* l_pWidget = indexWidget(l_Index); if(l_pWidget) { QRect l_Rec = l_pWidget->rect(); int shift = (l_Rec.height() - size.height()) / 2; int startX = l_Rec.x() + l_Rec.width() - size.width() - shift; int startY = l_Rec.y() + shift; int endX = startX + size.width(); int endY = startY + size.height(); QRect l_Image(startX, startY, size.width(), size.height()); if(l_Image.contains(l_Pos)) { l_bDefault = false; qDebug() << "contain!!!"; } } } } if(l_bDefault) QTreeView::mousePressEvent(in_pEvent); }
but it return null anyway.
Any solutions?
-
@Craetor said in get Tree Item rect on mousePressEvent:
indexWidget
Hi, unless you have set a Widget there it is NULL.
You can use
QRect QAbstractItemView::visualRect(const QModelIndex & index) constIf you need to paint a pixmap over a cell, maybe considering using a delegate ?
-
@mrjj, it works great, thanks!
Yes, i draw an image on paint event by the QStyledItemDelegate and detect his rect on mousePressEvent