PDA

View Full Version : problem withh QListViewItemIterator



:db:sStrong
6th March 2007, 13:58
Hello ppl,

I have got a probleem with QlistViewItemIterator, it doent not werk well by me.
i want to search in my listview voor something. I just get the first item on my list en it seems dat the search mechanism doen not work well.

this is my code:



void test::selectItem( QString name, int type )
{
QString _cmp_name = name.section(":", 0, 0);
QListViewItem* item;
QString _x_name = name.section(":", 1, 1);
QListViewItem * _cmp_item = (((CView *)(pWorkView->pStack->visibleWidget()))->ui_view->firstChild()->firstChild());
QListViewItemIterator it2= _cmp_item;
QListViewItem * _child_item = _cmp_item->firstChild();
item = _child_item;

while (_child_item)
if ( _child_item->text(0) == _x_name )
{
item = _child_item;
break;
}
else
{
item = _child_item->nextSibling();
}

while(*it2)
{
if(it2.current()->text( 0 ).contains(_x_name))
{
if ((*it2) && !(*it2)->pixmap(0))
{
((CView *)(pWorkView->pStack->visibleWidget()))->selectItem(*it2);
pWorkView->m_TabView->setCurrentPage(0);
((CView *)(pWorkView->pStack->visibleWidget()))->ui_view->ensureItemVisible(*it2);
break;
}
}
it2++;
}

}

thanx in advance

wysota
6th March 2007, 14:17
The code seems quite complicated... can't you use QListView::findItem() instead? What is it exactly that you want to find?

:db:sStrong
6th March 2007, 14:30
The code seems quite complicated... can't you use QListView::findItem() instead? What is it exactly that you want to find?


I want to search the child items of my list. take a look at this picture:-

http://img228.imageshack.us/img228/2360/testio0.jpg

in this list i want to search for example for the child item of C5, but i always get the first item en the first child in the list. i have tried it with findItem but when there is match(if the seach finds that there are two children with the same name, it will stop and i will get no results)it does not work.

wysota
6th March 2007, 22:07
Do you know up front that you'll be looking for a child of C5?

:db:sStrong
7th March 2007, 09:36
Do you know up front that you'll be looking for a child of C5?

sorry i did not get u :) ..

wysota
7th March 2007, 09:44
Do you know apriori (before starting the search) that the result you're looking for is a child of C5? Or is it just that you search all items and want to get an item which only happens to be a child of C5.

:db:sStrong
7th March 2007, 10:09
Do you know apriori (before starting the search) that the result you're looking for is a child of C5? Or is it just that you search all items and want to get an item which only happens to be a child of C5.

I will try to explain this problem as follow:


QString _cmp_name = name.section(":", 0, 0);
QString _x_name = name.section(":", 1, 1);

with the above code i get the name of C5 and the child of C5 which is 1..


QListViewItem * _cmp_item = (((CView

*)(pWorkView->pStack->visibleWidget()))->ui_view->firstChild()->firstChild());


this is the pointer to the ListView in which all those item are available(see the picture)

this is my listviewItem iterator, which would iterate in the listview..

QListViewItemIterator it2= _cmp_item;

in this place i search if the child item which i select is equal to the _x_name
if it is so i will get highlighted.(see the picture)
if it is not so it should iterate on my list till it will find the childItem


while (_child_item)

if ( _child_item->text(0) == _x_name )

{

item = _child_item;

break;

}

else

{

item = _child_item->nextSibling();

}



in this code i will search for the childItem, but u see i have a probleem that the most of the childItems have the same name for example "1" is the name of childItem of C5 en that of C1 and almost all of the items have the same childItem name and this code finds the childItem of first item in the list and do not see further. I select for example C5's child item which is "1", but this code hightlight the wrong childItem (the first occurance)

while(*it2)

{

if(it2.current()->text( 0 ).contains(_x_name))

{

if ((*it2) && !(*it2)->pixmap(0))

{

((CView *)(pWorkView->pStack->visibleWidget()))->selectItem(*it2);

pWorkView->m_TabView->setCurrentPage(0);

((CView *)(pWorkView->pStack->visibleWidget()))->ui_view->ensureItemVisible(*it2);

break;

}

}

it2++;

wysota
7th March 2007, 10:29
Ok, let's assume I know what you mean ;)


QListViewItem *MyClass::findItem(QListView *lv, QString parentName, QString childName){
QListViewItem *pitem = lv->findItem(parentName, 0): // find parent item
if(!pitem)
return 0; // parent item not found
// iterate child items:
for(QListViewItem *iter = pitem->firstChild(); iter!=0; iter = iter->nextSibling()){
if(iter->text(0) == childName) // texts match
return iter; // return item
}
return 0; // child item not found
}

:db:sStrong
7th March 2007, 12:41
Ok, let's assume I know what you mean ;)


QListViewItem *MyClass::findItem(QListView *lv, QString parentName, QString childName){
QListViewItem *pitem = lv->findItem(parentName, 0): // find parent item
if(!pitem)
return 0; // parent item not found
// iterate child items:
for(QListViewItem *iter = pitem->firstChild(); iter!=0; iter = iter->nextSibling()){
if(iter->text(0) == childName) // texts match
return iter; // return item
}
return 0; // child item not found
}


nope this methode i have tried earlier it doesnot work because if it finds a match the search stops and returns 0. FindItem() is not appropraite to use somehow. My last code works but it does not iterate well. I get the childItem but the first childItem in the list. In your methode i only find the childItems which occoured one time in de list.

wysota
7th March 2007, 13:53
How about this:

QListViewItem *MyClass::findItem(QListView *lv, QString parentName, QString childName){
QListViewItem *pitem;
for(pitem = lv->firstChild(); pitem; pitem = pitem->nextSibling()){
if(pitem->text(0) == parentName)
break;
}
if(!pitem)
return 0;
for(QListViewItem *iter = pitem->firstChild(); iter!=0; iter = iter->nextSibling()){
if(iter->text(0) == childName)
return iter;
}
return 0;
}

:db:sStrong
7th March 2007, 14:05
How about this:

QListViewItem *MyClass::findItem(QListView *lv, QString parentName, QString childName){
QListViewItem *pitem;
for(pitem = lv->firstChild(); pitem; pitem = pitem->nextSibling()){
if(pitem->text(0) == parentName)
break;
}
if(!pitem)
return 0;
for(QListViewItem *iter = pitem->firstChild(); iter!=0; iter = iter->nextSibling()){
if(iter->text(0) == childName)
return iter;
}
return 0;
}


thnx alot it works just fine :)