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. get Tree Item rect on mousePressEvent
Forum Updated to NodeBB v4.3 + New Features

get Tree Item rect on mousePressEvent

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

    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?

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

      @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) const

      If you need to paint a pixmap over a cell, maybe considering using a delegate ?

      1 Reply Last reply
      4
      • C Offline
        C Offline
        Craetor
        wrote on last edited by Craetor
        #3

        @mrjj, it works great, thanks!

        Yes, i draw an image on paint event by the QStyledItemDelegate and detect his rect on mousePressEvent

        1 Reply Last reply
        1

        • Login

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