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. -
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 ?
-
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. -
@dheerendra
For our Application we don't want to use H/W keyboard.
We are using Ubuntu 16.04 , 64bit system with Touchscreen display.@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_OBJECTpublic:
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_OBJECTpublic:
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);
} -
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
-
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
-
Thank you for your reply.
- At the first time my Push button click event is not getting fired so released event is also not working.
- I have implemented focus in event but at first time its also not getting emitting.
-
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? -
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. -
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.
-
@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.
@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 -
@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 -
as you said I have done same when keyboard widget is shown.
output isDBG default: focus widget QLineEdit(0x227ca90, name = "lineEdit")
lineEdit is Nothing but the QLineEdit on Keyboard widget.