开发者

Qt focus handling for virtual keyboard operated only by UDLR buttons

开发者 https://www.devze.com 2023-03-27 14:47 出处:网络
I have a data entry screen on a Qt/Embedded device that only has up/down/left/right/select buttons. The screen looks like a calculator with a QLineEdit on top and a set of QPushButtons for the numbers

I have a data entry screen on a Qt/Embedded device that only has up/down/left/right/select buttons. The screen looks like a calculator with a QLineEdit on top and a set of QPushButtons for the numbers. The UDLR buttons move focus around the QPushButtons and select appends the button's number to the QLineEdit. This works great. However, since the QLineEdit doesn't have focus, the text entry cursor doesn't blink. The QLineEdit just doesn't look like it's the recipient of the virtual keyboard button presses and that's not good.

The other Qt virtual keyboard implementations that I've seen don't need their keyboard buttons to gain focus since they are touch or mouse operated. Therefore, the QLineEdit can retain focus the entire time. That's not an option here. The best the I'开发者_如何学JAVAve seen so far is manual generation of focus events to trick the QLineEdit into thinking that it has focus when the user is really navigating around the virtual keyboard. I am hoping that there is a more elegant solution or maybe a different way to structure the screen's implementation that avoids this issue. Any ideas?


Since it seems like you don't need the true editing capabilities of QLineEdit, replace it with a QLabel. You can nest it inside a frame to make it look like an edit box. Then you can simulate a blinking cursor using a QTimer that doesn't need focus to work.

Start the timer with a 500ms interval and in the timer slot, insert or remove a '|' character like this:

void MainWindow::slot_timeout()
{
    QString labelText = label->text();    
    if(labelText.right(1) == "|")
    {
        labelText = labelText.left(labelText.size()-1);
    }
    else
    {
        labelText += "|";
    }    
    label->setText(labelText);
}

When your select button is pressed, insert the appropriate character at the front of the string so that the blinking cursor stays on the right end.

void MainWindow::slot_selectClick()
{
    QString labelText = label->text();
    labelText.prepend("x");
    label->setText(labelText);
}

It might be enough to achieve the effect you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消