0

I have derived the class QTreeWidget and creating my own QtPropertyTree. In order to populate the tree with widgets (check boxes, buttons, etc) I am using the following code:

// in QtPropertyTree.cpp
QTreeWidgetItem topItem1 = new QTreeWidgetItem(this);    
QTreeWidgetItem subItem = new QTreeWidgetItem(this);

int column1 = 0
int Column2 = 1;

QPushButton myButton = new QPushButton();
this->setIndexWidget(this->indexFromItem(this->subItem,column1), myButton);   

QCheckBox myBox = new QCheckBox();
this->setIndexWidget(this->indexFromItem(this->subItem,column2), myBox);

This works fine, but the problem is that i want to avoid using the "indexFromItem" function since it is protected, and there are other classes that are populating the tree and need access to that funcion. Do you know any alternative to using that function?

Cocomico
  • 878
  • 7
  • 18

2 Answers2

4

You can try to use your QTreeWidget's model (QAbstractItemModel) to get the right index by the column and row numbers:

// Row value is 1 because I want to take the index of
// the second top level item in the tree.
const int row = 1;

[..]

QPushButton myButton = new QPushButton();
QModelIndex idx1 = this->model()->index(row, column1);
this->setIndexWidget(idx1, myButton);   

QCheckBox myBox = new QCheckBox();
QModelIndex idx2 = this->model()->index(row, column2);
this->setIndexWidget(this->indexFromItem(idx2, myBox);

UPDATE

For sub items, the same approach can be used.

QModelIndex parentIdx = this->model()->index(row, column1);
// Get the index of the first child item of the second top level item.
QModelIndex childIdx = this->model()->index(0, column1, parentIdx);
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Unfortunately It didnt work. the model()->index(r,c) does only return indexes from top level items but I need the indexes of subItems in the model. – Cocomico Nov 14 '14 at 14:34
  • @Cocomico, what prevents you from using the `index()` function for sub items too? Just use the parent's model index as a third argument in the function as shown in the updated answer. – vahancho Nov 14 '14 at 14:46
  • Does it mean that if I have sub items nested several levels, I have to always call model()->index() and give the parent index as the third parameter? Supose I have 3 levels so It would look like: `QModelIndex i1 = model()->index(a,b)` `QModelIndex i2 = model()->index(c,d, i1)` `QModelIndex i3 = model()->index(e,f, i2)` ? – Cocomico Nov 14 '14 at 15:01
  • Or is there an easier way you can think of? – Cocomico Nov 14 '14 at 15:04
  • Very good answer. One last thing. I would still be calling the function setIndexWidget() from another external class, It means that class needs a reference to the QTreeWidget class , Isnt it bad design practice? – Cocomico Nov 14 '14 at 15:14
  • @Cocomico, I don't think it is bad. Alternatively you can avoid storing the reference to your tree, but look up for it dynamically among other widgets. – vahancho Nov 14 '14 at 15:52
2

The obvious solution is to de-protect indexFromItem like this:

class QtPropertyTree {
  ...
public:
  QModelIndex publicIndexFromItem(QTreeWidgetItem * item, int column = 0) const
    return indexFromItem (item, column) ;
  }
} ;
TonyK
  • 16,761
  • 4
  • 37
  • 72
  • That's ok. But then I would have to keep a reference of QtPorpertyTree in my other subclass and access the publicIndexFromItem. At The same time QtPropertyTree is accesing methods of the subclass. There is a cross-reference problem, Which can be solved. But isnt it a bad design practice? – Cocomico Nov 14 '14 at 15:51