Custom QWidget Keyboard first time click issue.



  • 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.


  • Qt Champions 2017

    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
    For our Application we don't want to use H/W keyboard.
    We are using Ubuntu 16.04 , 64bit system with Touchscreen display.


  • Qt Champions 2017

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



  • ----------------- 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);
    }



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


  • Qt Champions 2017

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



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



  • 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


  • @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.

  • Qt Champions 2017

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



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



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



  • @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.



  • 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.



  • @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.


  • Qt Champions 2017

    @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



  • @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.


  • Qt Champions 2017

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



  • @mrjj

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


  • Qt Champions 2017

    @Arvind-K
    so the widget appear frozen for a moment ?
    Do you have any kind of loops in constructor or anything like that ?



  • @mrjj

    No, there is no Loop in the constructor or No Any other code only I have connected signal to Slot.

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

    this->setWindowFlags(Qt::WindowStaysOnTopHint);
    connect ( ui->Buttonq, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonw, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttone, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonr, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttont, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttony, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonu, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttoni, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttono, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonp, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    
    connect ( ui->Buttona, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttons, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttond, 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->Buttonj, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonk, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonl, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonz, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonx, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonc, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonv, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonb, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonn, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Buttonm, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button0, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button1, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button2, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button3, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button4, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button5, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button6, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button7, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button8, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->Button9, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    connect ( ui->space, SIGNAL( clicked() ), this, SLOT( keyboardHandler() ) );
    
    outputText = "";
    shift = false;
    

    }


  • Qt Champions 2017

    @Arvind-K
    ok. when this situation is happening.
    Does the buttons show activated effect ?
    I mean can u see u pres them or anything ?



  • @mrjj

    As I mentioned Previously when I set WindowModality of Keyboard widget to QApplicationModal I face this First Click issue otherwise it works fine. but if I didn't set windowModality then Keyboard goes back to the parent widget.

    **lineEditkeyboard->setWindowModality(Qt::ApplicationModal); **


  • Qt Champions 2017

    Hmm.
    The code looks ok fine and i cant see any reason for
    QApplicationModal to cause this effect but
    it could be a bug in Qt.



  • @mrjj

    I can't see press button effect for the first time.


  • Qt Champions 2017

    @Arvind-K
    Ok, then it seems its stuck for a moment in the event loop.
    Im not sure what to do about that.
    setWindowModality(Qt::ApplicationModal) must use a local even loop to make
    it modal so might be a but with this or something i dont see in the code.


Log in to reply
 

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