qt 鍵盤按鍵事件 範例 keyPreEvent()

來源:互聯網
上載者:User

 

  1.  CompleteLineEdit::keyPressEvent(QKeyEvent *e) {  
  2.     if (!listView->isHidden()) {  
  3.         int key = e->key();  
  4.         int count = listView->model()->rowCount();  
  5.         QModelIndex currentIndex = listView->currentIndex();  
  6.         if (Qt::Key_Down == key) {  
  7.             // 按向下方向鍵時,移動游標選中下一個自動完成清單中的項  
  8.             int row = currentIndex.row() + 1;  
  9.             if (row >= count) {  
  10.                 row = 0;  
  11.             }  
  12.             QModelIndex index = listView->model()->index(row, 0);  
  13.             listView->setCurrentIndex(index);  
  14.         } else if (Qt::Key_Up == key) {  
  15.             // 按向下方向鍵時,移動游標選中上一個自動完成清單中的項  
  16.             int row = currentIndex.row() - 1;  
  17.             if (row < 0) {  
  18.                 row = count - 1;  
  19.             }  
  20.             QModelIndex index = listView->model()->index(row, 0);  
  21.             listView->setCurrentIndex(index);  
  22.         } else if (Qt::Key_Escape == key) {  
  23.             // 按下Esc鍵時,隱藏自動完成清單  
  24.             listView->hide();  
  25.         } else if (Qt::Key_Enter == key || Qt::Key_Return == key) {  
  26.             // 按下斷行符號鍵時,使用自動完成清單中選中的項,並隱藏自動完成清單  
  27.             if (currentIndex.isValid()) {  
  28.                 QString text = 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.     }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.