'How can I intercept the pressing of multiply (*) key in my C++ Qt calculator app?
I'm new in Qt and now working on calculator application that has opportunity of keyboard input (1,2,3,4,5,6,7,8,9,0,-,+,/,*,.,(,),).
Firstly, I tried just to determine "keyPressEvent" method like this:
void MainWindow::keyPressEvent(QKeyEvent* ev)
{
QString CurrentLabel_disp = ui->label->text();
QString KeyPressed;
if (ev->key() == Qt::Key_0)
KeyPressed = "0";
else if (ev->key() == Qt::Key_1)
KeyPressed = "1";
...
else if (ev->key() == Qt::Key_Plus)
KeyPressed = "+";
else if (ev->key() == Qt::Key_Minus)
KeyPressed = "-";
else if (ev->key() == Qt::Key_Slash)
KeyPressed = "/";
else if (ev->key() == Qt::Key_multiply)
KeyPressed = "*";
}
after some reflection I decided to reimplement "bool eventFilter()" and use "installEventFilter(this)" method instead of "keyPressEvent" determination:
bool MainWindow::eventFilter(QObject *obj, QEvent *event){
if(obj==this && event->type()==QEvent::KeyPress){
QKeyEvent* keyEvent=static_cast<QKeyEvent*>(event);
QString KeyPressed;
switch (keyEvent->key()) {
case Qt::Key_0:
KeyPressed="0";VisualItem_key_pressed(KeyPressed);return true;
case Qt::Key_1:
KeyPressed="1";VisualItem_key_pressed(KeyPressed);return true;
...
case Qt::Key_Plus:
KeyPressed="+";VisualItem_key_pressed(KeyPressed);return true;
case Qt::Key_Minus:
KeyPressed="-";VisualItem_key_pressed(KeyPressed);return true;
case Qt::Key_Slash:
KeyPressed="/";VisualItem_key_pressed(KeyPressed);return true;
case Qt::Key_multiply:
KeyPressed="*";VisualItem_key_pressed(KeyPressed);return true;
}
}
return QMainWindow::eventFilter(obj,event);
}
But in the first and second cases multiply key (*) was not working unlike the other keys..
So, the problem is in fact, that programm doesn't associate pressing (*) key on numpad or pressing (shift+8) with "case Qt::Key_multiply"
maybe the problem is in "Qt::Key_multiply", because I really dont know how numpad decimal separator (.) and multiply (*) symbols called in Qt..
Can you direct me to this problem's solution?
Solution 1:[1]
try using this case (Qt::Key_Asterisk):
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | omar yossuf |
