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. Custom QWidget Keyboard first time click issue.
QtWS25 Last Chance

Custom QWidget Keyboard first time click issue.

Scheduled Pinned Locked Moved Unsolved General and Desktop
27 Posts 5 Posters 4.8k Views
  • 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.
  • A Offline
    A Offline
    Arvind K
    wrote on last edited by
    #1

    For a General Application we have login form with on screen custom keyboard. Keyboard is designed with QWidget. When we try to enter a value with on screen keyboard , at first time it's not taking any input(i.e. button click) afterwards all buttons are working fine. We have tried to set a focus programmatically but its not working. Please give a solution for the same.
    We observed that keyboard is working fine if we call on Pushbutton click, but its not working with QLineEdit.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Event not received indicates that event is taken some other object and processed before it reaches keyboard widget. Did try running the program with H/W keyboard ? Which platform did you try this ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      A 1 Reply Last reply
      3
      • dheerendraD dheerendra

        Event not received indicates that event is taken some other object and processed before it reaches keyboard widget. Did try running the program with H/W keyboard ? Which platform did you try this ?

        A Offline
        A Offline
        Arvind K
        wrote on last edited by
        #3

        @dheerendra
        For our Application we don't want to use H/W keyboard.
        We are using Ubuntu 16.04 , 64bit system with Touchscreen display.

        mrjjM 1 Reply Last reply
        0
        • A Arvind K

          @dheerendra
          For our Application we don't want to use H/W keyboard.
          We are using Ubuntu 16.04 , 64bit system with Touchscreen display.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Arvind-K
          Hi
          since you are showing no code, i wont speculate on what error might be.

          Anyway, try using QGuiApplication::focusObject();
          and see what widget has the focus.

          For my widget keyboard, i have all buttons set to NoFocus for focusPolicy as else they
          steal focus on first shown, unless i control it very tight.

          1 Reply Last reply
          2
          • A Offline
            A Offline
            Arvind K
            wrote on last edited by
            #5

            ----------------- Login widget Code -------------------------------

            frmlogin.h

            #include <QWidget>
            #include "keyboard/keyboard.h"
            #include "messagebox.h"

            namespace Ui {
            class FrmLogin;
            }

            class FrmLogin : public QWidget
            {
            Q_OBJECT

            public:
            Ui::FrmLogin *ui;
            explicit FrmLogin(QWidget *parent = 0);
            ~FrmLogin();

            private slots:
            void run_keyboard_lineEdit();
            void on_BtnCancel_clicked();

            protected:
            void showEvent(QShowEvent *event);

            private:
            Keyboard *lineEditkeyboard;
            };


            frmlogin.cpp

            FrmLogin::FrmLogin(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::FrmLogin)
            {

            ui->setupUi(this);
            setGeometry(QStyle::alignedRect(Qt::LeftToRight,Qt::AlignCenter,size(),
                                            qApp->desktop()->availableGeometry()
                                            )
                        );
            setWindowFlags ( Qt::CustomizeWindowHint | Qt::WindowTitleHint);
            lineEditkeyboard = new Keyboard();
            
            connect(ui->txt_Pass ,SIGNAL(selectionChanged()),this,SLOT(run_keyboard_lineEdit()));
            connect(ui->txt_user ,SIGNAL(selectionChanged()),this,SLOT(run_keyboard_lineEdit()));
            

            }

            void FrmLogin::run_keyboard_lineEdit()
            {
            Qt::WindowFlags flags = windowFlags();

            QLineEdit *line = (QLineEdit *)sender();
            lineEditkeyboard->setLineEdit(line);
            lineEditkeyboard->ui->lineEdit->setText(line->text());
            if(line->objectName()=="txt_Pass")
            {
                lineEditkeyboard->ui->lineEdit->setEchoMode(QLineEdit::EchoMode::Password);
            }
            else
            {
                lineEditkeyboard->ui->lineEdit->setEchoMode(QLineEdit::EchoMode::Normal);
            }
            
            lineEditkeyboard->setWindowModality(Qt::ApplicationModal);
            lineEditkeyboard->setWindowState(Qt::WindowActive);
            lineEditkeyboard->raise();
            lineEditkeyboard->show();
            

            }

            -------------- Keyboard Widget Code -----------------------------------

            Keyboard.h

            #include <QWidget>
            #include <QtGui>
            #include <QLineEdit>
            #include <QPushButton>

            namespace Ui {
            class Keyboard;
            }

            class Keyboard : public QWidget
            {
            Q_OBJECT

            public:
            bool IsOnlyCharVar=false;
            explicit Keyboard(QWidget *parent = 0);
            void setLineEdit(QLineEdit * );
            ~Keyboard();
            QPushButton *enterButton;
            Ui::Keyboard *ui;

            private slots:
            void keyboardHandler();
            void on_enterButton_clicked();
            void on_lineEdit_returnPressed();

            private:

            QString outputText;
            QLineEdit *outputLineEdit;   
            

            };

            Keyboard.cpp

            #include "keyboard.h"
            #include "ui_keyboard.h"
            #include <QtGui>
            #include "QToolTip"

            Keyboard::Keyboard(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Keyboard)
            {
            ui->setupUi(this);

            this->setWindowFlags(Qt::WindowStaysOnTopHint);   
            
            connect ( ui->Buttona, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            connect ( ui->Buttonb, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            connect ( ui->Buttonc, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            connect ( ui->Buttond, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            connect ( ui->Buttone, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            connect ( ui->Buttonf, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            connect ( ui->Buttong, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            connect ( ui->Buttonh, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            connect ( ui->Buttoni, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );    
            
            connect ( ui->space, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
            
            outputText = "";
            ui->lineEdit->setFocus();
            

            }

            void Keyboard::keyboardHandler()
            {
            QPushButton *button = (QPushButton *)sender();
            QString inputText = button->text();

            QToolTip::showText(button->mapToGlobal(QPoint()),inputText,button,button->rect(),300);
            
            if (inputText == "Space")
            {
                outputText += " ";
            }
            else if(inputText == "&&")
            {
                outputText += "&";
            }
            else if(inputText == "\\")
            {
                outputText += ui->Buttona->text() ;
            }
            else
            {      
                {
                    outputText += inputText;
                }
            }
            ui->lineEdit->setText(outputText);
            

            }

            void Keyboard::setLineEdit(QLineEdit * line)
            {
            outputLineEdit = line;
            }

            Keyboard::~Keyboard()
            {
            delete ui;
            }

            void Keyboard::on_shift_clicked()
            {
            if(shift == true)
            {
            shift = false;
            ui->shift->setStyleSheet("background-color: rgb(248, 248, 248);");

                ui->Buttona->setText("a");
                ui->Buttons->setText("b");
                ui->Buttond->setText("c");
                ui->Buttonf->setText("d");
                ui->Buttong->setText("e");
                ui->Buttonh->setText("F");
                ui->Buttonj->setText("g");
                ui->Buttonk->setText("h");
                ui->Buttonl->setText("i");
            }
            else
            {
                shift = true;
                ui->shift->setStyleSheet("background-color: rgb(121, 121, 255);");
                ui->Buttona->setText("A");
                ui->Buttons->setText("B");
                ui->Buttond->setText("C");
                ui->Buttonf->setText("D");
                ui->Buttong->setText("E");
                ui->Buttonh->setText("F");
                ui->Buttonj->setText("G");
                ui->Buttonk->setText("H");
                ui->Buttonl->setText("I");
            }
            

            }

            void Keyboard::on_clear_clicked()
            {
            outputText="";
            ui->lineEdit->setText(outputText);
            }

            void Keyboard::on_enterButton_clicked()
            {
            //qDebug() << "enter";
            outputLineEdit->setText(outputText);
            outputText="";
            ui->lineEdit->setText(outputText);
            close();
            }

            void Keyboard::on_backButton_clicked()
            {
            //outputText.remove(outputText.length()-1,outputText.length());
            outputText.remove(outputText.length()-1,1);
            ui->lineEdit->setText(outputText);
            }

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Arvind K
              wrote on last edited by
              #6

              Any Updates??
              Please feel free to ask any questions on above code.

              mrjjM 1 Reply Last reply
              0
              • A Arvind K

                Any Updates??
                Please feel free to ask any questions on above code.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Arvind-K
                Hi
                What did
                qDebug() << "focus widget " << qApp->focusObject();
                report ?
                in the case where it dont type.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Arvind K
                  wrote on last edited by
                  #8

                  @mrjj

                  i used qDebug() << "focus widget " << qApp->focusObject(); at button click event.

                  it doesn't fire at first time . but at second time click on Button "a" i get "focus widget QPushButton(0x26bbc00, name= "Buttona")" log .

                  i think at first time my keyboard widget doesn't get activate. after click on widget anywhere it gets activate and after that it works proper.

                  how to solve this issue.?

                  1 Reply Last reply
                  0
                  • J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    Hi,

                    from my experience, this behaviour comes from the focus shift,

                    you click a button and than it gets the focus a and only than the clicked mechanimn works.

                    IIRC, you can change that a couple of different ways.

                    the following comes to my mind:

                    • Chaning the signal from clicked to released
                    • Subclassing QPushButton and manually emitting the pressed signal during focus in event

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    A 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      Hi,

                      from my experience, this behaviour comes from the focus shift,

                      you click a button and than it gets the focus a and only than the clicked mechanimn works.

                      IIRC, you can change that a couple of different ways.

                      the following comes to my mind:

                      • Chaning the signal from clicked to released
                      • Subclassing QPushButton and manually emitting the pressed signal during focus in event
                      A Offline
                      A Offline
                      Arvind K
                      wrote on last edited by
                      #10

                      @J.Hilk

                      Thank you for your reply.

                      1. At the first time my Push button click event is not getting fired so released event is also not working.
                      2. I have implemented focus in event but at first time its also not getting emitting.
                      mrjjM 1 Reply Last reply
                      0
                      • A Arvind K

                        @J.Hilk

                        Thank you for your reply.

                        1. At the first time my Push button click event is not getting fired so released event is also not working.
                        2. I have implemented focus in event but at first time its also not getting emitting.
                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Arvind-K
                        Hi
                        for my keyboard, I find it needed to have
                        NoFocus for focusPolicy on the buttons or they steal focus from Lineedit.

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          Arvind K
                          wrote on last edited by
                          #12

                          still, the issue is pending. Please provide a solution.

                          1 Reply Last reply
                          -1
                          • D Offline
                            D Offline
                            deleted372
                            wrote on last edited by
                            #13

                            It may be a silly question, does it happen with all the buttons of your keyboard?
                            Are you sure that the first time you push on a button of your keyboard the slot keyboardHandler() gets called?

                            A 1 Reply Last reply
                            0
                            • D deleted372

                              It may be a silly question, does it happen with all the buttons of your keyboard?
                              Are you sure that the first time you push on a button of your keyboard the slot keyboardHandler() gets called?

                              A Offline
                              A Offline
                              Arvind K
                              wrote on last edited by
                              #14

                              @JoZCaVaLLo

                              Yes, it happens with all the buttons of a keyboard. slot keyboardHandler() does not get called for the first time. another thing I observed that when I called a keyboard on QPushbutton clicked event then keyboard works properly. this issue happens with QlineEdit Only.

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                deleted372
                                wrote on last edited by
                                #15

                                I would call lineEditkeyboard->setLineEdit(line) only when the lineEdit gets the focus.
                                In addition, in the setLineEdit(), I would call this->setFocusProxy(line). In order to avoid your keyboard to steal the focus from your linedit.

                                A 1 Reply Last reply
                                0
                                • D deleted372

                                  I would call lineEditkeyboard->setLineEdit(line) only when the lineEdit gets the focus.
                                  In addition, in the setLineEdit(), I would call this->setFocusProxy(line). In order to avoid your keyboard to steal the focus from your linedit.

                                  A Offline
                                  A Offline
                                  Arvind K
                                  wrote on last edited by
                                  #16

                                  @JoZCaVaLLo
                                  @mrjj
                                  @J-Hilk
                                  @dheerendra
                                  one more thing to share when I called keyboard widget with a show() like
                                  lineEditkeyboard->setLineEdit(line);
                                  lineEditkeyboard->show();

                                  the keyboard works properly but when I called Keyboard like

                                  lineEditkeyboard->setLineEdit(line);
                                  lineEditkeyboard->setWindowModality(Qt::ApplicationModal);
                                  lineEditkeyboard->show();

                                  then I face the same issue of the first click.

                                  mrjjM 1 Reply Last reply
                                  0
                                  • A Arvind K

                                    @JoZCaVaLLo
                                    @mrjj
                                    @J-Hilk
                                    @dheerendra
                                    one more thing to share when I called keyboard widget with a show() like
                                    lineEditkeyboard->setLineEdit(line);
                                    lineEditkeyboard->show();

                                    the keyboard works properly but when I called Keyboard like

                                    lineEditkeyboard->setLineEdit(line);
                                    lineEditkeyboard->setWindowModality(Qt::ApplicationModal);
                                    lineEditkeyboard->show();

                                    then I face the same issue of the first click.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @Arvind-K
                                    hi, we need to find out what is happening.
                                    please do
                                    qDebug() << "focus widget " << qApp->focusObject();
                                    which tells us when you open the keyboard, who has focus.
                                    Meaning who will get the keys if u type.
                                    I think you have something that steals keyboard focus when u open the
                                    lineEditkeyboard

                                    A 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @Arvind-K
                                      hi, we need to find out what is happening.
                                      please do
                                      qDebug() << "focus widget " << qApp->focusObject();
                                      which tells us when you open the keyboard, who has focus.
                                      Meaning who will get the keys if u type.
                                      I think you have something that steals keyboard focus when u open the
                                      lineEditkeyboard

                                      A Offline
                                      A Offline
                                      Arvind K
                                      wrote on last edited by
                                      #18

                                      @mrjj

                                      as you said I have done same when keyboard widget is shown.
                                      output is

                                      DBG default: focus widget QLineEdit(0x227ca90, name = "lineEdit")

                                      lineEdit is Nothing but the QLineEdit on Keyboard widget.

                                      mrjjM 1 Reply Last reply
                                      0
                                      • A Arvind K

                                        @mrjj

                                        as you said I have done same when keyboard widget is shown.
                                        output is

                                        DBG default: focus widget QLineEdit(0x227ca90, name = "lineEdit")

                                        lineEdit is Nothing but the QLineEdit on Keyboard widget.

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @Arvind-K
                                        Ok so it does have the focus when first click is not registered?

                                        A 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @Arvind-K
                                          Ok so it does have the focus when first click is not registered?

                                          A Offline
                                          A Offline
                                          Arvind K
                                          wrote on last edited by
                                          #20

                                          @mrjj

                                          Yes.
                                          after that when I click on Anywhere on Widget Nothing happen or no Any event gets called.

                                          mrjjM 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