Qt : send Key_Return and Key_Delete events

2019-06-24 19:23发布

I am developing a virtual keyboard with Qt Embedded, and I'm confronted with a little problem. In fact, I use SignalMappers to map the key to keyboard events in order to display text in a QTextEdit widget.

Everything works fine, except for two events : Key_Return and Key_Delete ; I have no idea what I'm doing wrong, maybe you'll have an idea.

Here is a classical code, to send chars :

void VirtualKeyboard::SendChar( int index )
{
    QChar charToSend( letters_.at( index )->text().at( 0 ) ); // Get char

    server_->sendKeyEvent( charToSend.unicode(), QEvent::KeyPress, Qt::NoModifier, true, false );
}

letters_ is a QVector containing QPushButton* and server_ is the instance of QWSServer ; this code works fine. Now, for example with backspace :

void VirtualKeyboard::SendBackspace()
{
    server_->sendKeyEvent( Qt::Key_Backspace, Qt::Key_Backspace, Qt::NoModifier, true, false );
}

This code works fine too. And the code that doesn't work :

void VirtualKeyboard::SendDelete()
{
    server_->sendKeyEvent( Qt::Key_Delete, Qt::Key_Delete, Qt::NoModifier, true, false );
}

void VirtualKeyboard::SendEnter()
{  
    server_->sendKeyEvent( 0x01000004, Qt::Key_Return, Qt::NoModifier, true, false ); 
}

As you can see, I tryed to put an unicode value but it doesn't help ; can you help me please ?

Thanks !


SOLVED WITH THE FOLLOWING CODE (SEE COMMENT) :

void TextEdit::DeleteEvent()
{
    if( cursor_.hasSelection() )
    {
        // Delete selection
        cursor_.removeSelectedText();
    }
    else
    {
        // Delete right char
        cursor_.deleteChar();
    }

    setTextCursor( cursor_ );
}

void TextEdit::ReturnEvent()
{
    cursor_.insertText( "\n" );
    setTextCursor( cursor_ );
}

cursor_ is a QTextCursor attribute, initialized with this line :

cursor_ = textCursor();

1条回答
我命由我不由天
2楼-- · 2019-06-24 19:53

SOLVED WITH THE FOLLOWING CODE (SEE COMMENT) :

void TextEdit::DeleteEvent()
{
    if( cursor_.hasSelection() )
    {
        // Delete selection
        cursor_.removeSelectedText();
    }
    else
    {
        // Delete right char
        cursor_.deleteChar();
    }

    setTextCursor( cursor_ );
}

void TextEdit::ReturnEvent()
{
    cursor_.insertText( "\n" );
    setTextCursor( cursor_ );
}

cursor_ is a QTextCursor attribute, initialized with this line :

cursor_ = textCursor();
查看更多
登录 后发表回答