PDA

View Full Version : QTreeWidgetItem Subclassing



soxs060389
13th January 2010, 23:50
Hi, I want to subclass QTreeWidgetItem and use it within a QTreeWidget subclass, but I do not want to reimplement all functions from QTreeWidgetItem nore QTreeWidget.

So far so god.

Additionally I need the option to dynamically update the TreeWidget.

I create a method to search for applicable TreeWidgetItems:





/*! search from root (if root is zer0, search all nodes) all children for the Desired Drawable
*/
TreeWidgetItem* TreeWidget::deepSearchByPtr(TreeWidgetItem *root, Drawable *d){
qDebug() << "DFS 0";
if (root == 0){
}
else if (reinterpret_cast<Drawable*>(root->ptr()) == d){
qDebug() << "DFS B";
return root;
}

qDebug() << "DFS C";
TreeWidgetItem *ret;
TreeWidgetItem *child;
qDebug() << "DFS D";
int count(0);
if (root==0){
count = invisibleRootItem()->childCount();
}
else{
count = root->childCount();
}
for(int q(0); q<count; ++q){
qDebug() << "DFS LOOP A";
child = reinterpret_cast<TreeWidgetItem*>(root->child(q));
qDebug() << "DFS LOOP B";
if (child == NULL){
qDebug() << "DFS LOOP C";
continue;
}
qDebug() << "DFS LOOP D";
ret = deepSearchByPtr(child, d);
qDebug() << "DFS LOOP E";
if (ret != NULL){
qDebug() << "DFS LOOP F";
return ret;
}
}

return NULL;
}


The Problem with that is, that it fails right before
qDebug() << "DFS LOOP B";
though all children actually are TreeWidgetItem s.

Note: falling back to QTreeWidgetItems is not an option, as they hold way more data than ptr.

Note:
My Custom Type is called "TreeWidgetItem" and "TreeWIdget"

can anybody plx help me to fix this issue.

Sincerely

soxs060389

P.S. allready wasted hours on that subject...

high_flyer
14th January 2010, 15:18
your code allows
child = reinterpret_cast<TreeWidgetItem*>(root->child(q));

to be called even if 'root == NULL'.

soxs060389
14th January 2010, 20:53
Thank you very much, it was definitly too late to search for bugs yesterday O.o

Solved