I am beginner to qt. I was working on calculator gui application, I have already defined slots like numPressed() when any of the number pushbutton is pressed on the calculator that will be displayed on the lineEdit.
void Calculator::numPressed(){
QPushButton *button = (QPushButton *)sender();
QString buttonValue = button->text();
Qstring display = ui->lineEdit->text();
Qdouble ProcessedValue = previousValue + buttonValue.toDouble();
.
.
.
ui->lineEdit->setText(QString::number(ProcessedValue));
}
connect(ui->add_button,SIGNAL(released),this,SLOT(numPressed));
Here add_button is a QPushbutton name on the UI.
What i want to achieve is?
When a number key is pressed on the key board i want to connect the number pressed and number pushbutton on the calculator which fills the lineEdit with the number that i have pressed on the keyboard?
PS: I want to connect number pressed on the keyboard to the number on the pushbutton.
If
QPushButton
is used for the calculator buttons then the solution might be very easy:Qt provides a concept for short cuts. Actions as well as certain widgets (
QPushButton
included) are already prepared for this.QPushButton::shortcut (inherited from base class
QAbstractButton
):Example:
To try this out, I used my (old) pocket calculator example and added the support of shortcut keys.
The one-and-only source code
qCalc.cc
:A Qt project file to build
qCalc.pro
:Built and tested in VS2017 with Qt 5.13: