Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6865137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:00:12+00:00 2026-05-27T03:00:12+00:00

My QTreeWidget has a single column. Its items have a check box, an icon,

  • 0

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 clicked. How can I find the position and size of the icon in a QTreeWidgetItem?

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

First, I sub-classed QItemDelegate so that I could access the coordinates of each part of a QTreeWidgetItem (check box, 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 MyItemDelegate* member to MyTreeWidget, and set it as the item view’s delegate. In the header:

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

In the 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) ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:00:13+00:00Added an answer on May 27, 2026 at 3:00 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a QTreeWidget with a bunch of QTreeWidgetItems. Each item has a couple
I have a QTreeWidget that I insert items in, and the user can select
I have an inherited QTreeWidget (called PackList) class and its parent is a KXmlGuiWindow.
I have to populate a QTreeWidget with items (or children of items) that may
Is it possible to have individual indentation of items in a QTreeWidget? In specific,
I have two columns in a QTreeWidget , one column represents a list of
I'm using a QTreeWidget to display some simple items. I've set the list sortable
In my qt app I have this object, filled before setting up my QTreeWidget's
I have an application written with pyside where a user can load an image,
I have an QHBoxLayout with a QTreeWidget on the left, a separator on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.