0

i want intercept signal when i press on a Qcombobox->linEdit(), key Backpace and clear all text. I have seen on web and i write this code, but it dont work. It seem that keypressevent function not is invoked.. Some tips???

 MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->comboBox->addItem("11");
        ui->comboBox->addItem("21");
        ui->comboBox->addItem("32");
        ui->comboBox->addItem("41");
        ui->comboBox->addItem("54");
    }

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
  if (obj == ui->comboBox) {
     if (event->type() == QEvent::KeyPress) {
         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
         qDebug()<<"Ciao";
         if (keyEvent->key() == Qt::Key_Backspace)
         {
             ui->comboBox->clearEditText();
             return true;
         }
  }
     // pass the event on to the parent class
     return QMainWindow::eventFilter(obj, event);
}
}
MainWindow::~MainWindow()
{
    delete ui;
}
user3589887
  • 139
  • 2
  • 13

1 Answers1

0

Add ui->comboBox->installEventFilter(this);

olya
  • 312
  • 1
  • 4
  • 1
    Welcome to Stack Overflow! Although this code may help to solve the problem, it doesn't explain _why_ and/or _how_ it answers the question. Providing this additional context would significantly improve its long-term value. Please [edit] your answer to add explanation, including what limitations and assumptions apply. – Toby Speight Jun 07 '17 at 19:45