`
feipigwang
  • 浏览: 743794 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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. }

分享到:
评论
1 楼 wwq100 2012-08-07  

相关推荐

Global site tag (gtag.js) - Google Analytics