if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); if ((keyEvent->key()==Qt::Key_0) || (keyEvent->key()==Qt::Key_1) || (keyEvent->key()==Qt::Key_2) || (keyEvent->key()==Qt::Key_3) || (keyEvent->key()==Qt::Key_4) || (keyEvent->key()==Qt::Key_5) || (keyEvent->key()==Qt::Key_6) || (keyEvent->key()==Qt::Key_7) || (keyEvent->key()==Qt::Key_8) || (keyEvent->key()==Qt::Key_9)) { ui->table->editItem(ui->table->item(ui->table->currentRow(),1)); ui->table->item(ui->table->currentRow(),1)->setText(keyEvent->text()); if (ui->table->item(ui->table->currentRow(),1)->text().toInt() > 0) { ui->table->item(ui->table->currentRow(),0)->setFont(QFont("",9,75)); } else { ui->table->item(ui->table->currentRow(),0)->setFont(QFont("",9,-1)); } } } return QMainWindow::eventFilter(obj, event);}
C++ (Qt)if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); if ((keyEvent->key()==Qt::Key_0) || (keyEvent->key()==Qt::Key_1) || (keyEvent->key()==Qt::Key_2) || (keyEvent->key()==Qt::Key_3) || (keyEvent->key()==Qt::Key_4) || (keyEvent->key()==Qt::Key_5) || (keyEvent->key()==Qt::Key_6) || (keyEvent->key()==Qt::Key_7) || (keyEvent->key()==Qt::Key_8) || (keyEvent->key()==Qt::Key_9)) { ui->table->editItem(ui->table->item(ui->table->currentRow(),1)); ui->table->item(ui->table->currentRow(),1)->setText(keyEvent->text()); if (ui->table->item(ui->table->currentRow(),1)->text().toInt() > 0) { ui->table->item(ui->table->currentRow(),0)->setFont(QFont("",9,75)); } else { ui->table->item(ui->table->currentRow(),0)->setFont(QFont("",9,-1)); } return true; //<= } } return QMainWindow::eventFilter(obj, event);}
if (event->type() == QEvent::KeyPress) //смотрим что за кнопка была нажата{ QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); if ((keyEvent->key()==Qt::Key_0) || (keyEvent->key()==Qt::Key_1) || (keyEvent->key()==Qt::Key_2) || (keyEvent->key()==Qt::Key_3) || (keyEvent->key()==Qt::Key_4) || (keyEvent->key()==Qt::Key_5) || (keyEvent->key()==Qt::Key_6) || (keyEvent->key()==Qt::Key_7) || (keyEvent->key()==Qt::Key_8) || (keyEvent->key()==Qt::Key_9))//проверяем, если эта одна из цифр { ui->table->editItem(ui->table->item(ui->table->currentRow(),2));//начинаем редактирование ячейки - количество, текущей строки if (keyEvent->text().toInt() >= 0 && keyEvent->text().toInt() <= 9 /*|| keyEvent->text().toInt() == 2 || keyEvent->text().toInt() == 3 || keyEvent->text().toInt() == 4 || keyEvent->text().toInt() == 5 || keyEvent->text ().toInt() == 6 || keyEvent->text().toInt() == 7 || keyEvent->text().toInt() == 8 || keyEvent->text().toInt() == 9 */) { ui->table->item(ui->table->currentRow(),2)->setText(keyEvent->text());//то в ячейку количество, текущей строки, втавляем символ, который был нажат, то есть число } else { return true; } keyEvent->text().clear();