Icon Position in QTreeWidgetItem

My QTreeWidgethas one column. Its elements have a checkmark, icon, and text. If the user clicks inside an element, I want to know if the icon has been clicked. How to find the position and size of the icon in QTreeWidgetItem?

Updated to add: Here is the code for my final solution, as requested by webclectic.

First, I subclassed QItemDelegateto get access to the coordinates of each part QTreeWidgetItem(checkbox, icon and text). Here is the header file:

#include <QItemDelegate>

class MyItemDelegate : public QItemDelegate
  {
  Q_OBJECT

public:
  explicit MyItemDelegate (MyTreeWidget *parent)
    : QItemDelegate (parent), ParentView (parent) { }
  ~MyItemDelegate() { }

  void GetRects (const QModelIndex &index, QRect& CheckBox, QRect& Icon, QRect& Text) const ;

private:
  MyTreeWidget* ParentView ;
  } ;

And here is the source file:

void MyItemDelegate::GetRects (const QModelIndex &index, QRect& CheckBox, QRect& Icon, QRect& Text) const
  {
  QStyleOptionViewItem option = ParentView -> viewOptions() ;
  CheckBox = rect (option, index, Qt::CheckStateRole) ;
  Icon = rect (option, index, Qt::DecorationRole) ;
  Text = rect (option, index, Qt::DisplayRole) ;

  doLayout (option, &CheckBox, &Icon, &Text, true) ;

  QRect VisualRect = ParentView -> visualRect (index) ;
  CheckBox.translate (VisualRect.topLeft()) ;
  Icon.translate (VisualRect.topLeft()) ;
  Text.translate (VisualRect.topLeft()) ;
  }

Then I added a member MyItemDelegate*to MyTreeWidgetand set it as the delegate of the element view. In the title:

class MyTreeWidget : public QTreeWidget
  {
  ...
  MyItemDelegate* Delegate ;
  ...
  } ;

In source:

MyTreeWidget::MyTreeWidget (QObject* parent)
  {
  ...
  Delegate = new MyItemDelegate (this) ;
  setItemDelegate (ItemDelegate) ;
  }

Now, to get the coordinates of each part of a QTreeWidgetItem:

  QTreeWidgetItem* item ;
  ...
  QModelIndex ModelIndex = indexFromItem (item) ;
  QRect CheckBoxRect, IconRect, TextRect ;
  ItemDelegate -> GetRects (ModelIndex, &CheckBoxRect, &IconRect, &TextRect) ;
+5
1

, , . , QTreeWidget , .

, QTreeWidget mousePressEvent ( mouseReleaseEvent, ). .

( ):

void mousePressEvent(QMouseEvent *event)
{
   QModelIndex clickedIndex = indexAt(event->pos());
   // make sure the event was on a valid item
   if (clickedIndex.isValid() == false)
      return;

   // Get the tree widget x position
   int treeX = header()->sectionViewportPosition(0);

   // Get the x coordinate of the root item. It is required in order to calculate
   // the identation of the item
   int rootX = visualRect(rootIndex()).x();

   // Get the rectangle of the viewport occupied by the pressed item
   QRect vrect = visualRect(clickedIndex);

   // Now we can easily calculate the x coordinate of the item
   int itemX = treeX + vrect.x() - rootX; 

   // The item is a checkbox, then an icon and finally the text. 

   // 1. Get the rect surrounding the checkbox
   QRect checkboxRect = QRect(itemX, 
                              vrect.y(), 
                              style()->pixelMetric(QStyle::PM_IndicatorWidth),
                              vrect.height()); 

   // 2. Get the rect surrounding the icon
   QRect iconRect = QRect(itemX + checkboxRect.width(),
                          vrect.y(),
                          iconSize().width(),
                          vrect.height());

   // 3. Finally get the rect surrounding the text
   QRect textRect = QRect(itemX + checkboxRect.width() + iconRect.width(),
                          vrect.y(),
                          vrect.width() - checkboxRect.width() - iconRect.width(),
                          vrect.height());       

   // Now check where the press event took place and handle it correspondingly

   if(checkboxRect.contains(event->pos())) 
   {
       qDebug() << "Checkbox pressed";
       QTreeWidget::mousePressEvent(event);
       return;
   } 
   else if (iconRect.contains(event->pos()))
   {
       qDebug() << "Icon pressed";
       QTreeWidget::mousePressEvent(event);
       return;
   }
   else
   {
       qDebug() << "Text pressed";
       QTreeWidget::mousePressEvent(event);
       return; 
   }
}

, , , , .

+2

All Articles