来源: http://blog.sina.com.cn/s/blog_712038ba0101dgfx.html
QcomboBox
The QComboBox widget is a combined button and popup list.
说白了就是下拉列表,当点击QcomboBox时,QcomboBox将获得焦点并将其展开,展开以后,焦点将位于QcomboBox.view(),当选中QcomboBox.view()下的某一项时焦点又回到QcomboBox上,QcomboBox的大体构建为:Model-àviewàQcomboBox,数据存在Model中,通过View显示出来就构成了QcomboBox;
QcomboBox常用的方法有:
Eg:QcomboBox *combox;
1. 将QcomboBox展开:combox->showPopup();
2. 将QcomboBox收起来:combox->hidePopup();
3. 当QcomboBox展开后焦点位于QcomboBox.view()下,这是我们不想有鼠标点击,想通过方向键控制上下移动,安另外一个键控制选择;
当obj== combox ->view()时
if(keyEvent->key()==Qt::Key_Up)
{
Int current_index=combox->currentIndex();
current_index--;
combox->setCurrentIndex(current_index);
QModelIndex itemIndex = combox->view()->model()->index(current_index,0);
combox->view()->selectionModel()->setCurrentIndex(itemIndex,QItemSelectionModel::SelectCurrent);
}
else if(keyEvent->key()==Qt::Key_Y)
{
QModelIndex itemIndex = combox->view()->model()->index(current_index,0);
combox->view()->selectionModel()->setCurrentIndex(itemIndex,QItemSelectionModel::ToggleCurrent);
combox->setCurrentIndex(current_index);
combox->hidePopup();
return true;
}