- void classname::slot_DelCondition()
- {
- int i=0;
- // QListBoxItem *tmpitem = NULL;
- int num = ConditionListBox->count();
- // printf("num =%d\n",num);
- i = num;
- //when we delete a list item , the num will -1 the num has change, so we must delete from tail
- while (i)
- {
- i--;
- if(ConditionListBox->isSelected(i))
- {
- // printf("%d is selected \n",i);
- ConditionListBox->removeItem(i);
- }
- }
- /********************
- // 这里的num没有及时的根新,还是原来的count()的返回值
- for(i=0;i<num;i++)
- {
- //QListBoxItem *tmpitem = ConditionListBox->item(i);
- /////////////////
- if(ConditionListBox->isSelected(i))
- {
- printf("%d is selected \n",i);
- ConditionListBox->removeItem(i);
- }
- ////////////////////////////////
- ///////////////////////////
- QListBoxItem *tmpitem = ConditionListBox->item(i);
- printf("the %d address of tmpitem is %x\n",i,tmpitem);
- if(tmpitem && (tmpitem->isSelected()) )
- {
- int x= ConditionListBox->index(tmpitem);
- printf("%d is selected address is %x\n",x,tmpitem);
- ConditionListBox->removeItem(x);
- // 这以后 count()的返回值改变了
- // ConditionListBox->takeItem(tmpitem);
- // ConditionListBox->update();
- }
- ///////////////////////////
- }
- **********************/
- }
当我们需要删除QListBox里面得QListViewItem的时候,要注意从后面删除。因为当你删除一项的时候,QListBox 的数目会动态的更新,所以,如果是用for循环的话,count()的返回值已经改变,但是变量num的值没有跟这改变,导致一些QListView没有被删除。改用了while循环,并且是从后面遍历,就可以避免这样的问题,不知道我说明白了没有;)
看不到代码的效果,版主把我扫盲一下吧:)
[ 此贴被浪漫天使在2006-04-29 11:54重新编辑 ]