Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Make LineEdit set focus for virtual keyboard
Qt 6.11 is out! See what's new in the release blog

Make LineEdit set focus for virtual keyboard

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marlenet15
    wrote on last edited by marlenet15
    #1

    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;
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        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

        M Offline
        M Offline
        marlenet15
        wrote on last edited by
        #3

        @SGaist how would I implement my own QInputMethod?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You have a pretty good article here about it or here

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            You have a pretty good article here about it or here

            M Offline
            M Offline
            marlenet15
            wrote on last edited by marlenet15
            #5

            @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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              0
              • SGaistS SGaist

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

                M Offline
                M Offline
                marlenet15
                wrote on last edited by
                #7

                @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??

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  That makes it easier to do the input properly.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved