- CompleteLineEdit::keyPressEvent(QKeyEvent*e){
- if(!listView->isHidden()){
- intkey=e->key();
- intcount=listView->model()->rowCount();
- QModelIndexcurrentIndex=listView->currentIndex();
- if(Qt::Key_Down==key){
- //按向下方向键时,移动光标选中下一个完成列表中的项
- introw=currentIndex.row()+1;
- if(row>=count){
- row=0;
- }
- QModelIndexindex=listView->model()->index(row,0);
- listView->setCurrentIndex(index);
- }elseif(Qt::Key_Up==key){
- //按向下方向键时,移动光标选中上一个完成列表中的项
- introw=currentIndex.row()-1;
- if(row<0){
- row=count-1;
- }
- QModelIndexindex=listView->model()->index(row,0);
- listView->setCurrentIndex(index);
- }elseif(Qt::Key_Escape==key){
- //按下Esc键时,隐藏完成列表
- listView->hide();
- }elseif(Qt::Key_Enter==key||Qt::Key_Return==key){
- //按下回车键时,使用完成列表中选中的项,并隐藏完成列表
- if(currentIndex.isValid()){
- QStringtext=listView->currentIndex().data().toString();
- setText(text);
- }
- listView->hide();
- }else{
- //其他情况,隐藏完成列表,并使用QLineEdit的键盘按下事件
- listView->hide();
- QLineEdit::keyPressEvent(e);
- }
- }else{
- QLineEdit::keyPressEvent(e);
- }
qt 键盘按键事件 范例 keyPreEvent()
最新推荐文章于 2024-04-04 11:04:22 发布