Qt : send Key_Return and Key_Delete events

余生长醉 提交于 2019-12-23 12:34:12

问题


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:


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();


来源:https://stackoverflow.com/questions/8254019/qt-send-key-return-and-key-delete-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!