Position of icon in QTreeWidgetItem

后端 未结 1 1548
粉色の甜心
粉色の甜心 2021-01-18 05:38

My QTreeWidget has a single column. Its items have a check box, an icon, and text. If the user clicks inside an item, I want to know whether the icon was clicke

相关标签:
1条回答
  • 2021-01-18 06:17

    Unfortunately there is no simple way to achieve what you want. The problem is that QTreeWidget is responsible for painting its items so the item itself has no information about the position of its elements in the view.

    First of all you have to subclass QTreeWidget and reimplement the mousePressEvent (or mouseReleaseEvent if you prefer). Inside the event you should calculate the position of the icon and handle it correspondingly.

    Sample code (but untested) follows:

    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's 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; 
       }
    }
    

    I repeat that the code is untested but you get the idea about how to achieve what you want.

    0 讨论(0)
提交回复
热议问题