• 6523阅读
  • 0回复

QListBox使用注意 [复制链接]

上一主题 下一主题
离线浪漫天使
 
只看楼主 倒序阅读 楼主  发表于: 2006-04-29
  1. void classname::slot_DelCondition()
  2. {
  3.   int i=0;
  4. //   QListBoxItem *tmpitem = NULL;
  5.   int num = ConditionListBox->count();
  6. //   printf("num =%d\n",num);
  7.   i = num;
  8.   //when we delete a list item , the num will -1 the num has change, so we must delete from tail
  9.   while (i)
  10.   {
  11.     i--;
  12.     if(ConditionListBox->isSelected(i))
  13.     {
  14. //       printf("%d is selected \n",i);
  15.         ConditionListBox->removeItem(i);
  16.     }
  17.   }
  18. /********************
  19. // 这里的num没有及时的根新,还是原来的count()的返回值
  20.   for(i=0;i<num;i++)
  21.   {
  22.     //QListBoxItem *tmpitem = ConditionListBox->item(i);
  23. /////////////////
  24.     if(ConditionListBox->isSelected(i))
  25.     {
  26.         printf("%d is selected \n",i);
  27.         ConditionListBox->removeItem(i);
  28.     }
  29. ////////////////////////////////
  30. ///////////////////////////
  31.     QListBoxItem *tmpitem = ConditionListBox->item(i);
  32.     printf("the %d address of tmpitem is %x\n",i,tmpitem);
  33.     if(tmpitem && (tmpitem->isSelected()) )
  34.     {
  35.         int x= ConditionListBox->index(tmpitem);
  36.         printf("%d is selected address is %x\n",x,tmpitem);
  37.         ConditionListBox->removeItem(x);
  38. // 这以后 count()的返回值改变了
  39. //       ConditionListBox->takeItem(tmpitem);
  40. //       ConditionListBox->update();
  41.     }
  42. ///////////////////////////
  43.   }
  44. **********************/
  45. }


当我们需要删除QListBox里面得QListViewItem的时候,要注意从后面删除。因为当你删除一项的时候,QListBox 的数目会动态的更新,所以,如果是用for循环的话,count()的返回值已经改变,但是变量num的值没有跟这改变,导致一些QListView没有被删除。改用了while循环,并且是从后面遍历,就可以避免这样的问题,不知道我说明白了没有;)

看不到代码的效果,版主把我扫盲一下吧:)
[ 此贴被浪漫天使在2006-04-29 11:54重新编辑 ]
快速回复
限100 字节
 
上一个 下一个