qt 键盘按键事件 范例 keyPreEvent()

  1. CompleteLineEdit::keyPressEvent(QKeyEvent*e){
  2. if(!listView->isHidden()){
  3. intkey=e->key();
  4. intcount=listView->model()->rowCount();
  5. QModelIndexcurrentIndex=listView->currentIndex();
  6. if(Qt::Key_Down==key){
  7. //按向下方向键时,移动光标选中下一个完成列表中的项
  8. introw=currentIndex.row()+1;
  9. if(row>=count){
  10. row=0;
  11. }
  12. QModelIndexindex=listView->model()->index(row,0);
  13. listView->setCurrentIndex(index);
  14. }elseif(Qt::Key_Up==key){
  15. //按向下方向键时,移动光标选中上一个完成列表中的项
  16. introw=currentIndex.row()-1;
  17. if(row<0){
  18. row=count-1;
  19. }
  20. QModelIndexindex=listView->model()->index(row,0);
  21. listView->setCurrentIndex(index);
  22. }elseif(Qt::Key_Escape==key){
  23. //按下Esc键时,隐藏完成列表
  24. listView->hide();
  25. }elseif(Qt::Key_Enter==key||Qt::Key_Return==key){
  26. //按下回车键时,使用完成列表中选中的项,并隐藏完成列表
  27. if(currentIndex.isValid()){
  28. QStringtext=listView->currentIndex().data().toString();
  29. setText(text);
  30. }
  31. listView->hide();
  32. }else{
  33. //其他情况,隐藏完成列表,并使用QLineEdit的键盘按下事件
  34. listView->hide();
  35. QLineEdit::keyPressEvent(e);
  36. }
  37. }else{
  38. QLineEdit::keyPressEvent(e);
  39. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值