• 8423阅读
  • 4回复

QcomboBox:index 0  out of range [复制链接]

上一主题 下一主题
离线rockyluo
 

只看楼主 倒序阅读 楼主  发表于: 2009-02-06
有个QComboBox
  1.     QComboBox *cb = new QComboBox(false,this);
  2.     cb->insertItem("Item1",0);
  3.     cb->insertItem("Item2",1);
  4.     cb->setCurrentItem(0);

后来调用QComboBox::changeItem ( const QString & text, int index )
如下:
cb->changeItem ("ON",0);
cb->changeItem ("Yes",1);
运行时确出现:
  1. QComboBox::changeItem: (unnamed) Index 0 out of range
  2. QComboBox::changeItem: (unnamed) Index 1 out of range
  3. QComboBox::changeItem: (unnamed) Index 0 out of range
  4. QComboBox::changeItem: (unnamed) Index 1 out of range
  5. QComboBox::changeItem: (unnamed) Index 0 out of range
  6. QComboBox::changeItem: (unnamed) Index 1 out of range
  7. QComboBox::changeItem: (unnamed) Index 0 out of range
  8. QComboBox::changeItem: (unnamed) Index 1 out of range
  9. QComboBox::changeItem: (unnamed) Index 0 out of range
  10. QComboBox::changeItem: (unnamed) Index 1 out of range
  11. QComboBox::changeItem: (unnamed) Index 0 out of range
  12. QComboBox::changeItem: (unnamed) Index 1 out of range
  13. QComboBox::changeItem: (unnamed) Index 0 out of range
  14. QComboBox::changeItem: (unnamed) Index 1 out of range
  15. ......


这是什么原因?
是QComboBox的bug还是?
各位仁兄该怎么解决???
                                                                                                              我们要团结一心,秉承Linux的开源精神,共享Qt问题的解决方法!
离线nmiirq

只看该作者 1楼 发表于: 2009-02-06
我试了一下,是OK的,你的中间其它操作有没有?
离线rockyluo

只看该作者 2楼 发表于: 2009-02-06
有操作
我的这个QComboBox是由键盘的一个按键来控制,
通过按键不停的按来循环切换QComboBox中的Item
主要动作是  按键一下,调用setCurrentItem()来设置一个Item.
同时,这个QComboBox连接了一个槽,如下:
connect(cb,SIGNAL(highlighted(int)),this,SLOT(cb_Slot(int)));
这样,只要setCurrentItem调用后,就触发cb_Slot(int)的槽函数。
我现在是想在cb_Slot(int)的槽函数中来调用changItem(QString,int)
这样做了后出现了“out of range”错误
不知道这是怎么引起的???

我试过把connect一行屏蔽掉,直接调用changItem(QString,int),如你所说,的确没有错误。
                                                                                                              我们要团结一心,秉承Linux的开源精神,共享Qt问题的解决方法!
离线蛮蛮
只看该作者 3楼 发表于: 2009-02-06
void Q3ComboBox::highlighted ( int index )  [signal]
This signal is emitted when a new item has been set to be the current item. The index is the position of the item in the combobox.
This signal is not emitted if the item is changed programmatically, e.g. using setCurrentItem().

按说明,这个东西在setCurrentItem后是不会调用cb_slot的.

按你说的,我觉得也不应该会out of range的,只是你过程是有没有类似于clear的操作?

在cb_slot里检查一下,是不是真的没有item0和item1?
蛮蛮工作室
离线rockyluo

只看该作者 4楼 发表于: 2009-02-06
解决了  的确是highlighted ( int index )惹的祸
我仔细看了下
void QComboBox::highlighted ( int index ) [signal]
This signal is emitted when a new item has been set to current. The index is the position of the item in the combobox

void QComboBox::activated ( int index ) [signal]
This signal is emitted when a new item has been activated (selected). The index is the position of the item in the combobox.

其中highlighted对应的“has been set to current”,而activated 对应的“has been activated (selected)” 这是关键

而:
void QComboBox::setCurrentItem ( int index ) [virtual]
Sets the index of the current item in the combobox to index. See the "currentItem" property for details.

void QComboBox::changeItem ( const QString & t, int index )
Replaces the item at position index with the text t.

调用setCurrentItem()后会触发highlighted ( int index )信号,不会触发activated ( int index )信号;
而调用changeItem ()后也是只会触发highlighted ( int index )信号,不会触发activated ( int index )信号;(已验证)

这样,如我上的想法,如果
connect(cb,SIGNAL(highlighted(int)),this,SLOT(cb_Slot(int)));
则在cb_Slot(int)中调用changeItem (),必会不停的触发highlighted(int),最终将导致死循环,同时报“out of range”错误

最终解决方法:
1.setCurrentItem()后直接调用cb_Slot(int);
2.将highlighted(int)该为activated ( int );
                                                                                                              我们要团结一心,秉承Linux的开源精神,共享Qt问题的解决方法!
快速回复
限100 字节
 
上一个 下一个