PDA

View Full Version : how can move up/down the cursor after QComboBox showPopup?



josentop
1st December 2011, 09:19
when my QComboBox show popup the list, it is default to use UP/DOWN key to select, i need to change UP/DOWN key to LEFT/RIGHT key to select, i try to use the eventFilter in my QComboBox, but seem it is impossible due to i can't access the moveCursor function.
could you give me your suggestion? thanks.

Lykurg
1st December 2011, 10:17
Why do you need to move the cursor? Just eat the key up and down events and send them if key left right is clicked. Or Subclass QComboBox and reimp QComboBox::keyPressEvent().

josentop
1st December 2011, 10:40
Hi,
i need to move up/down the cursor in the below item list popup from ComboBox. Is it still possible to reimplement QComboBox's keyPressEvent? thanks.

7143

Lykurg
1st December 2011, 12:12
With cursor you mean selection? If so:
void MyComboBox::keyPressEvent(QKeyEvent* e)
{
if (Qt::Key_Up == e->key()
|| Qt::Key_Down == e->key())
return;

if (Qt::Key_Left == e->key())
{
QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
QComboBox::keyPressEvent(&myEvent);
}
else if (Qt::Key_Right == e->key())
{
QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
QComboBox::keyPressEvent(&myEvent);
}
else
{
QComboBox::keyPressEvent(e);
}
}

EDIT: If you need to move the selection while the popup is shown, you have to install an event filter on QCombobox::view() in your subclass.

josentop
2nd December 2011, 03:09
With cursor you mean selection? If so:
void MyComboBox::keyPressEvent(QKeyEvent* e)
{
if (Qt::Key_Up == e->key()
|| Qt::Key_Down == e->key())
return;

if (Qt::Key_Left == e->key())
{
QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
QComboBox::keyPressEvent(&myEvent);
}
else if (Qt::Key_Right == e->key())
{
QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
QComboBox::keyPressEvent(&myEvent);
}
else
{
QComboBox::keyPressEvent(e);
}
}

EDIT: If you need to move the selection while the popup is shown, you have to install an event filter on QCombobox::view() in your subclass.

but my really problem is how to implement the event filter for QCpmboBox::view(). Could you please give more detail about it? thanks.

Lykurg
2nd December 2011, 08:08
It's quite the same as above. Subclass QComboBox and install the filter there. An I still don't get what you really want. Move the selection on the popup up and down with the left and right arrows? And what do you have so far?

josentop
2nd December 2011, 10:31
Yes, please see below my detail wants: after user selected the comboBox, he can use Key_Alt to popup the item list view. and then he can use the left/right arrow key to select the item in the list, then use Key_Alt key to confirm the selected item and close the item list view. I have implement the below code, but seem the even filter for QComboBox's view() doesn't work.



#include "combobox.h"

#include <QEvent>
#include <QKeyEvent>
#include <QAbstractItemView>
#include <QDebug>

ComboBox::ComboBox(QWidget *parent) :
QComboBox(parent)
{
view()->installEventFilter(this);
}

void ComboBox::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Space) return;

int key = Qt::Key_unknown;
if (e->key() == Qt::Key_Alt || e->key() == Qt::Key_Meta) key = Qt::Key_Space;

if (key == Qt::Key_unknown) QComboBox::keyPressEvent(e);
else
{
QKeyEvent myEvent(QEvent::KeyPress, key, Qt::NoModifier);
QComboBox::keyPressEvent(&myEvent);
}
}

bool ComboBox::eventFilter(QObject *t, QEvent *e)
{
if(e->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e);
int key = keyEvent->key();

qDebug("Key = %x", key);

int key_map = Qt::Key_unknown;
if (t == (QObject *)view())
{
switch(key)
{
case Qt::Key_Left:
key_map = Qt::Key_Up;
break;
case Qt::Key_Right:
key_map = Qt::Key_Down;
break;
case Qt::Key_Alt:
case Qt::Key_Meta:
break;
case Qt::Key_Space:
break;
}
}

if (key_map != Qt::Key_unknown)
{
QKeyEvent myEvent(QEvent::KeyPress, key_map, Qt::NoModifier);
return QComboBox::eventFilter(t, &myEvent);
}
}
return QComboBox::eventFilter(t, e);
}

Lykurg
2nd December 2011, 11:01
Hi,

you have to use e.g.
return QApplication::sendEvent(t, &myEvent);.

josentop
2nd December 2011, 16:22
Hi Lykurg,
It works well after applied the modification. thanks very much.:)