PDA

View Full Version : moving QListViewItems



ct
17th March 2006, 17:51
QListViewItem *item =this->firstChild();
while( item )
{
if(((myItem *)item)->isBoolFunction())
{
item->moveItem(this->firstChild());
this->firstChild()->moveItem(item);

}
item =item->nextSibling();
}


I want to move one or more items to the top in a QListView.
myItem is my class which inherits QListViewItem. Everything compiles fine ..however when the GUI is shown the application completely hangs and I have to literally kill the process...
is this some kind of bug. I am using qt 3.1.1 on redhat. Any other way to do this nicely ?

wysota
17th March 2006, 17:55
But what exactly? You have only shown a snippet (not complete one) of a code that does something.

Solarity
17th March 2006, 19:24
It sounds like you are never exiting your loop. I would put a break in there to make sure your exit condition is met.

ct
18th March 2006, 05:04
actually the full code is too long and the two classes are in separate files so I was trying to just explain the scenario..
here is the myItem class with the bool function ..


class myItem : public QListViewItem
{
myItem(QListViewItem *parent,QString label);
bool myBoolFunction();

};
bool myItem::myBoolFunction()
{

if(Flag)
return true;
else
return false;
}


now all I want to do is for each listviewitem in the parentcheck whether the flag is set and is so move the item to the top to make the first child of the parent..
in doing so I would have to iterate through all the childs of the parent and check the flag of each child through myBoolFunction()...

also


class myGroup : public QListViewItem
{
myGroup(QListView *parent,QString Label);
void adjustGroups();

}


void adjustGroups()
{
QListViewItem *item =this->firstChild();
while( item )
{
if(((myItem *)item)->isBoolFunction())
{
item->moveItem(this->firstChild());//works fine if i remove this
this->firstChild()->moveItem(item);//works fine if i remove this
count++;
}
item =item->nextSibling();
}


}


the class that adjustGroup() is in is also a QListViewItem however it has several childs..and works fine if I remove the two lines...
my guess is I am moving the item and then agian doing a item->nextSibling().... can we do this in some other way to move one or more items to the top of the list...

where would I use this ??
well in a messenger client when someone comes online then he/she is shifted to the top of its group... like in msn messenger... I am trying to do a similar thing :)

wysota
21st March 2006, 02:14
You might do it this way (it's just a concept):


QPtrList<QListViewItem> _items;
myItem *item = dynamic_cast<myItem>(this->firstChild());
while(item){
if(item->myBoolFunction()) _items.push_back(item);
item = dynamic_cast<myItem>(item->nextSibling());
}
for(QPtrList<QListViewItem>::iterator it = _items.begin(); it!=_items.end();++it){
takeItem(*it);
insertItem(*it); // or some other method like moveItem()
}

The first iteration looks for all items to move upwards, the second does the move.