Make LineEdit set focus for virtual keyboard



  • My goal is to create a simple virtual keyboard for my application without creating a separate window. I was able to create the keyboard but I am unable to press the button and display on whatever line edit (there's four in total) it is currently clicked on. I have been looking into how to create focus for a widget but I am completely lost.

    dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QKeyEvent>
    #include <QSignalMapper>
    #include <QInputMethod>
    
    Dialog::Dialog(QWidget *parent, Qt::WindowFlags flags) :
        QDialog(parent,flags),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        signalMapper = new QSignalMapper(this);
        digitButtons = this->findChildren<QPushButton *>();
        for (int i=0;i<digitButtons.count();i++)
        {
        connect(digitButtons.at(i), SIGNAL(clicked()), signalMapper, SLOT(map()));
        signalMapper->setMapping(digitButtons.at(i), i);
        }
        connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(btn_clicked(int)));
        QLineEdit::setFocusPolicy(Qt::ClickFocus);
    }
    
    void Dialog::btn_clicked(int btn)
    {
    QKeyEvent keyEvent(QEvent::KeyPress, 0, Qt::NoModifier, digitButtons.at(btn)->text(), false, 0);
    QApplication::sendEvent(this->focusWidget(), &keyEvent);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    

  • Lifetime Qt Champion

    Hi,

    You won't be able to do it purely with focus. If you don't want to implement your own QInputMethod, you'd rather have to give your keyboard the information about which QLineEdit it should write in



  • @SGaist how would I implement my own QInputMethod?


  • Lifetime Qt Champion

    You have a pretty good article here about it or here



  • @SGaist I was reading http://www.kdab.com/qt-input-method-virtual-keyboard/ and I thought it gave a great explanation of the code but they are using a server for the QT Input Method Plugin. Is this really necessary? What if I am not connected to the internet when running the application?


  • Lifetime Qt Champion

    They are using DBus which doesn't require any internet connection but no you can use other methods



  • @SGaist These articles semi helped. Both looked like those virtual keyboards are used as a plugin so that they appear once the user clicks on the textfield/lineedit. However, i would like my keyboard to be a widget that i could add to a layout and be inside the application I am creating. Is the QPlatformInputContext() necessary for this??


  • Lifetime Qt Champion

    That makes it easier to do the input properly.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.