您的位置:首页 > 编程语言 > Qt开发

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

2010-07-20 17:13 1591 查看
 

 CompleteLineEdit::keyPressEvent(QKeyEvent *e) {  

    if (!listView->isHidden()) {  

        int key = e->key();  

        int count = listView->model()->rowCount();  

        QModelIndex currentIndex = listView->currentIndex();  

        if (Qt::Key_Down == key) {  

            // 按向下方向键时,移动光标选中下一个完成列表中的项  

            int row = currentIndex.row() + 1;  

            if (row >= count) {  

                row = 0;  

            }  

            QModelIndex index = listView->model()->index(row, 0);  

            listView->setCurrentIndex(index);  

        } else if (Qt::Key_Up == key) {  

            // 按向下方向键时,移动光标选中上一个完成列表中的项  

            int row = currentIndex.row() - 1;  

            if (row < 0) {  

                row = count - 1;  

            }  

            QModelIndex index = listView->model()->index(row, 0);  

            listView->setCurrentIndex(index);  

        } else if (Qt::Key_Escape == key) {  

            // 按下Esc键时,隐藏完成列表  

            listView->hide();  

        } else if (Qt::Key_Enter == key || Qt::Key_Return == key) {  

            // 按下回车键时,使用完成列表中选中的项,并隐藏完成列表  

            if (currentIndex.isValid()) {  

                QString text = listView->currentIndex().data().toString();  

                setText(text);  

            }  

            listView->hide();  

        } else {  

            // 其他情况,隐藏完成列表,并使用QLineEdit的键盘按下事件  

            listView->hide();  

            QLineEdit::keyPressEvent(e);  

        }  

    } else {  

        QLineEdit::keyPressEvent(e);  

    }  

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qt up