C++ 如何在我的C+;中截获乘法(*)键的按下+;Qt计算器应用程序?
我是Qt新手,现在正在开发计算器应用程序,它有机会进行键盘输入(1,2,3,4,5,6,7,8,9,0,-,+,/,*,(,),) 首先,我尝试确定“keyPressEvent”方法,如下所示:C++ 如何在我的C+;中截获乘法(*)键的按下+;Qt计算器应用程序?,c++,qt5,calculator,C++,Qt5,Calculator,我是Qt新手,现在正在开发计算器应用程序,它有机会进行键盘输入(1,2,3,4,5,6,7,8,9,0,-,+,/,*,(,),) 首先,我尝试确定“keyPressEvent”方法,如下所示: void MainWindow::keyPressEvent(QKeyEvent* ev) { QString CurrentLabel_disp = ui->label->text(); QString KeyPressed; if (ev->key() =
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 = "*";
}
经过一些思考,我决定重新实现“bool eventFilter()”并使用“installEventFilter(this)”方法,而不是“keyPressEvent”确定:
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);
}
bool主窗口::事件过滤器(QObject*obj,QEvent*event){
if(obj==this&&event->type()==QEvent::KeyPress){
QKeyEvent*keyEvent=静态广播(事件);
按QString键;
开关(keyEvent->key()){
案例Qt::键0:
keyppressed=“0”VisualItem\u键按下(keyppressed);返回true;
案例Qt::关键点1:
KeyPressed=“1”VisualItem\u键按下(KeyPressed);返回true;
...
案例Qt::关键点加上:
KeyPressed=“+”VisualItem_键按下(KeyPressed);返回true;
案例Qt::键_减号:
KeyPressed=“-”VisualItem_键按下(KeyPressed);返回true;
案例Qt::键斜杠:
KeyPressed=“/”VisualItem_key_pressed(按下键);返回true;
案例Qt::键_乘法:
KeyPressed=“*”VisualItem_键按下(KeyPressed);返回true;
}
}
返回QMainWindow::eventFilter(对象,事件);
}
但在第一和第二种情况下,乘法键(*)与其他键不一样
所以,问题是实际上,程序并没有将按下numpad上的(*)键或按下(shift+8)键与“case Qt::key\u multiply”相关联
也许问题出在“Qt::Key_multiply”中,因为我真的不知道如何在Qt中调用numpad十进制分隔符(.)和multiply(*)符号
你能告诉我这个问题的解决方法吗?我可以同时使用
Shift+8
和numpad映射上的乘法键,将其设置为Qt::key\u Asterisk
。这也适用于只需按键事件
,无需使用事件过滤器
。我爱你,戴夫,谢谢!