本文介绍了Qt:发送Key_Return和Key_Delete事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Qt Embedded开发一个虚拟键盘,我遇到一个小问题。事实上,我使用SignalMappers将键映射到键盘事件,以便在QTextEdit小部件中显示文本。



除了两个事件,一切都可以正常工作:Key_Return和Key_Delete;我不知道我在做错什么,也许你会有一个想法。



这是一个古典的代码,发送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);
}

字母_ 是一个QVector,包含QPushButton *和 server _ QWSServer 的实例;这段代码工作正常。
现在,例如使用backspace:

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

此代码也可以正常工作。而代码不起作用:

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

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

如你所见,我尝试放一个unicode值,帮助你可以帮我吗?



谢谢!






使用以下代码(见评论):

  void TextEdit :: DeleteEvent()
{
if (cursor_.hasSelection())
{
//删除选择
cursor_.removeSelectedText();
}
else
{
//删除右侧char
cursor_.deleteChar();
}

setTextCursor(cursor_);
}

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

cursor _ 是一个 QTextCursor 属性,用这一行初始化:

  cursor_ = textCursor(); 


解决方案

根据以下代码解决(见评论):

  void TextEdit :: DeleteEvent()
{
if(cursor_.hasSelection())
{
//删除选择
cursor_.removeSelectedText();
}
else
{
//删除右侧char
cursor_.deleteChar();
}

setTextCursor(cursor_);
}

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

cursor _ 是一个 QTextCursor 属性,用这一行初始化:

  cursor_ = textCursor(); 


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();
解决方案

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

这篇关于Qt:发送Key_Return和Key_Delete事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:32