关于Qt中QComboBox的下拉、收起焦点问题

原文链接在此
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;

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值