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.
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);
}
I want when an event passed to QComboBox it will be passed to the QComboBox->lineEdit().
installEventFilter() is your friend here. It allows object A to install an event filter on another object B, so that before object B's event(QEvent *)
method gets called, object A's eventFilter(QObject *, QEvent *)
method will be called first, so that object A can decide how to handle the event (and whether or not the event should be passed on to object B afterwards).
You can use that so that your CustomCombo
can see and react to events that would otherwise go directly to the `QComboBox.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.