Search code examples
c++qtqt5qcomboboxqlineedit

Event QComboBox to Custom QLineEdit


The Problem: I have a custom event on QLineEdit inside a custom QComboBox and only specific events are being passed from QComboBox to QLineEdit when I want. I can't get tab to be passed.

I want when an event passed to QComboBox it will be passed to the QComboBox->lineEdit().

QCustomCombo::QCustomCombo():
    m_lineEdit(new QCustomLineEdit)
{
    setEditable(true);
    setLineEdit(m_lineEdit);
}

bool QCustomCombo::event(QEvent * event)
{
    if(event->type() == QEvent::KeyPress)
    {
        QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
        if(keyEvent->key() == Qt::Key_tab)
        {
            //pass to lineEdit();
            //I have tried 'return true/false and QWidget::event(event)'
            //I have also tried commenting out QCustomCombo::event, same problem
        }
    }
    return QWidget::event(event);
}

QCustomLineEdit

bool QCustomLineEdit::event(QEvent * event)
{
    if(event->type() == QEvent::KeyPress)
    {
        QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
        if(keyEvent->key() == Qt::Key_tab)
        {
            //Do custom Stuff
            return true;
        }
        if(keyEvent->key() == Qt::Key_Right)
        {
            //Do custom Stuff
            return true;
        }
    }
    return QWidget::event(event);
}

The QLineEdit has a custom event for left and right arrow and tab. Only the arrows get passed. But I can't get the tab to pass to it.


Solution

  • Use QApplication::notify

    bool QCustomCombo::event(QEvent * event)
    {
        if(event->type() == QEvent::KeyPress)
        {
            QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
            if(keyEvent->key() == Qt::Key_Tab)
            {
                qApp->notify(m_lineEdit, event);
                return true;
            }
        }
        return QWidget::event(event);
    }