Logo Search packages:      
Sourcecode: qt4-x11 version File versions

void QLineEdit::keyPressEvent ( QKeyEvent event  )  [protected, virtual]

Converts the given key press event into a line edit action.

If Return or Enter is pressed and the current text is valid (or can be made valid by the validator), the signal returnPressed() is emitted.

The default key bindings are listed in the class's detailed description.

Reimplemented from QWidget.

Definition at line 1746 of file qlineedit.cpp.

References QString::at(), backspace(), clear(), copy(), cursorBackward(), cursorForward(), cursorWordBackward(), cursorWordForward(), cut(), del(), echoMode, editingFinished(), end(), home(), insert(), QKeyEvent::isAutoRepeat(), QString::isEmpty(), QChar::isPrint(), QKeyEvent::key(), QWidget::layoutDirection, QString::length(), QKeyEvent::modifiers(), paste(), redo(), returnPressed(), selectAll(), setSelection(), QKeyEvent::text(), undo(), and QWidget::update().

{
    Q_D(QLineEdit);

#ifndef QT_NO_COMPLETER
    if (d->completer
        && d->completer->popup()
        && d->completer->popup()->isVisible()) {
        // The following keys are forwarded by the completer to the widget
        // Ignoring the events lets the completer provide suitable default behavior
       switch (event->key()) {
       case Qt::Key_Escape:
            event->ignore();
            return;
       case Qt::Key_Enter:
       case Qt::Key_Return:
       case Qt::Key_F4:
#ifdef QT_KEYPAD_NAVIGATION
       case Qt::Key_Select:
           if (!QApplication::keypadNavigationEnabled())
               break;
#endif
           d->completer->popup()->hide(); // just hide. will end up propagating to parent
       default:
           break; // normal key processing
       }
    }
#endif // QT_NO_COMPLETER

#ifdef QT_KEYPAD_NAVIGATION
    bool select = false;
    switch (event->key()) {
        case Qt::Key_Select:
            if (QApplication::keypadNavigationEnabled()) {
                if (hasEditFocus()) {
                    setEditFocus(false);
                    if (d->completer && d->completer->popup()->isVisible())
                        d->completer->popup()->hide();
                    select = true;
                }
            }
            break;
        case Qt::Key_Back:
        case Qt::Key_No:
            if (!QApplication::keypadNavigationEnabled() || !hasEditFocus()) {
                event->ignore();
                return;
            }
            break;
        default:
            if (QApplication::keypadNavigationEnabled()) {
                if (!hasEditFocus() && !(event->modifiers() & Qt::ControlModifier)) {
                    if (!event->text().isEmpty() && event->text().at(0).isPrint()
                        && !isReadOnly())
                    {
                        setEditFocus(true);
                        clear();
                    } else {
                        event->ignore();
                        return;
                    }
                }
            }
    }



    if (QApplication::keypadNavigationEnabled() && !select && !hasEditFocus()) {
        setEditFocus(true);
        if (event->key() == Qt::Key_Select)
            return; // Just start. No action.
    }
#endif


    if(echoMode() == PasswordEchoOnEdit &&
       !isReadOnly() &&
       !event->text().isEmpty() &&
#ifdef QT_KEYPAD_NAVIGATION
       event->key() != Qt::Key_Select &&
       event->key() != Qt::Key_Up &&
       event->key() != Qt::Key_Down &&
       event->key() != Qt::Key_Back &&
#endif
       !(event->modifiers() & Qt::ControlModifier))
    {
        setEchoMode(Normal);
        clear();
        d->resumePassword = true;
    }

    d->setCursorVisible(true);
    if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
        if (hasAcceptableInput() || d->fixup()) {
            emit returnPressed();
            d->emitingEditingFinished = true;
            emit editingFinished();
            d->emitingEditingFinished = false;
        }
        event->ignore();
        return;
    }
    bool unknown = false;

    if (false) {
    }
#ifndef QT_NO_SHORTCUT
    else if (event == QKeySequence::Undo) {
        if (!d->readOnly)
            undo();
    }
    else if (event == QKeySequence::Redo) {
        if (!d->readOnly)
            redo();
    }
    else if (event == QKeySequence::SelectAll) {
        selectAll();
    }
#ifndef QT_NO_CLIPBOARD
    else if (event == QKeySequence::Copy) {
        copy();
    }
    else if (event == QKeySequence::Paste) {
        if (!d->readOnly)
            paste();
    }
    else if (event == QKeySequence::Cut) {
        if (!d->readOnly) {
            cut();
        }
    }
    else if (event == QKeySequence::DeleteEndOfLine) {
        if (!d->readOnly) {
            setSelection(d->cursor, d->text.size());
            copy();
            del();
        }
    }
#endif //QT_NO_CLIPBOARD
    else if (event == QKeySequence::MoveToStartOfLine) {
        home(0);
    }
    else if (event == QKeySequence::MoveToEndOfLine) {
        end(0);
    }
    else if (event == QKeySequence::SelectStartOfLine) {
        home(1);
    }
    else if (event == QKeySequence::SelectEndOfLine) {
        end(1);
    }
    else if (event == QKeySequence::MoveToNextChar) {
#if !defined(Q_WS_WIN) || defined(QT_NO_COMPLETER)
        if (d->hasSelectedText()) {
#else
        if (d->hasSelectedText() && d->completer
            && d->completer->completionMode() == QCompleter::InlineCompletion) {
#endif
            d->moveCursor(d->selend, false);
        } else {
            cursorForward(0, layoutDirection() == Qt::LeftToRight ? 1 : -1);
        }
    }
    else if (event == QKeySequence::SelectNextChar) {
        cursorForward(1, layoutDirection() == Qt::LeftToRight ? 1 : -1);
    }
    else if (event == QKeySequence::MoveToPreviousChar) {
#if !defined(Q_WS_WIN) || defined(QT_NO_COMPLETER)
        if (d->hasSelectedText()) {
#else
        if (d->hasSelectedText() && d->completer
            && d->completer->completionMode() == QCompleter::InlineCompletion) {
#endif
            d->moveCursor(d->selstart, false);
        } else {
            cursorBackward(0, layoutDirection() == Qt::LeftToRight ? 1 : -1);
        }
    }
    else if (event == QKeySequence::SelectPreviousChar) {
        cursorBackward(1, layoutDirection() == Qt::LeftToRight ? 1 : -1);
    }
    else if (event == QKeySequence::MoveToNextWord) {
        if (echoMode() == Normal)
            layoutDirection() == Qt::LeftToRight ? cursorWordForward(0) : cursorWordBackward(0);
        else
            layoutDirection() == Qt::LeftToRight ? end(0) : home(0);
    }
    else if (event == QKeySequence::MoveToPreviousWord) {
        if (echoMode() == Normal)
            layoutDirection() == Qt::LeftToRight ? cursorWordBackward(0) : cursorWordForward(0);
        else if (!d->readOnly) {
            layoutDirection() == Qt::LeftToRight ? home(0) : end(0);
        }
    }
    else if (event == QKeySequence::SelectNextWord) {
        if (echoMode() == Normal)
            layoutDirection() == Qt::LeftToRight ? cursorWordForward(1) : cursorWordBackward(1);
        else
            layoutDirection() == Qt::LeftToRight ? end(1) : home(1);
    }
    else if (event == QKeySequence::SelectPreviousWord) {
        if (echoMode() == Normal)
            layoutDirection() == Qt::LeftToRight ? cursorWordBackward(1) : cursorWordForward(1);
        else
            layoutDirection() == Qt::LeftToRight ? home(1) : end(1);
    }
    else if (event == QKeySequence::Delete) {
        if (!d->readOnly)
            del();
    }
    else if (event == QKeySequence::DeleteEndOfWord) {
        if (!d->readOnly) {
            cursorWordForward(true);
            del();
        }
    }
    else if (event == QKeySequence::DeleteStartOfWord) {
        if (!d->readOnly) {
            cursorWordBackward(true);
            del();
        }
    }
#endif // QT_NO_SHORTCUT
    else {
#ifdef Q_WS_MAC
        if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down) {
            Qt::KeyboardModifiers myModifiers = (event->modifiers() & ~Qt::KeypadModifier);
            if (myModifiers & Qt::ShiftModifier) {
                if (myModifiers == (Qt::ControlModifier|Qt::ShiftModifier)
                        || myModifiers == (Qt::AltModifier|Qt::ShiftModifier)
                        || myModifiers == Qt::ShiftModifier) {

                    event->key() == Qt::Key_Up ? home(1) : end(1);
                }
            } else {
                if ((myModifiers == Qt::ControlModifier
                     || myModifiers == Qt::AltModifier
                     || myModifiers == Qt::NoModifier)) {
                    event->key() == Qt::Key_Up ? home(0) : end(0);
                }
            }
        }
#endif
        if (event->modifiers() & Qt::ControlModifier) {
            switch (event->key()) {
            case Qt::Key_Backspace:
                if (!d->readOnly) {
                    cursorWordBackward(true);
                    del();
                }
                break;
#ifndef QT_NO_COMPLETER
            case Qt::Key_Up:
            case Qt::Key_Down:
                d->complete(event->key());
                break;
#endif
#if defined(Q_WS_X11)
            case Qt::Key_E:
                end(0);
                break;

            case Qt::Key_U:
                if (!d->readOnly) {
                    setSelection(0, d->text.size());
#ifndef QT_NO_CLIPBOARD
                    copy();
#endif
                    del();
                }
            break;
#endif
            default:
                unknown = true;
            }
        } else { // ### check for *no* modifier
            switch (event->key()) {
            case Qt::Key_Backspace:
                if (!d->readOnly) {
                    backspace();
#ifndef QT_NO_COMPLETER
                    d->complete(Qt::Key_Backspace);
#endif
                }
                break;
#ifdef QT_KEYPAD_NAVIGATION
            case Qt::Key_Back:
                if (QApplication::keypadNavigationEnabled() && !event->isAutoRepeat()
                    && !isReadOnly()) {
                    if (text().length() == 0) {
                        setText(d->origText);

                        if (d->resumePassword)
                        {
                            setEchoMode(PasswordEchoOnEdit);
                            d->resumePassword = false;
                        }

                        setEditFocus(false);
                    } else if (!d->deleteAllTimer.isActive()) {
                        d->deleteAllTimer.start(750, this);
                    }
                } else {
                    unknown = true;
                }
                break;
#endif

            default:
                unknown = true;
            }
        }
    }

    if (event->key() == Qt::Key_Direction_L || event->key() == Qt::Key_Direction_R) {
        setLayoutDirection((event->key() == Qt::Key_Direction_L) ? Qt::LeftToRight : Qt::RightToLeft);
        d->updateTextLayout();
        update();
        unknown = false;
    }

    if (unknown && !d->readOnly) {
        QString t = event->text();
        if (!t.isEmpty() && t.at(0).isPrint()) {
            insert(t);
#ifndef QT_NO_COMPLETER
            d->complete(event->key());
#endif
            event->accept();
            return;
        }
    }

    if (unknown)
        event->ignore();
    else
        event->accept();
}


Generated by  Doxygen 1.6.0   Back to index