PDA

View Full Version : how to detect key events on a list box?



mahe2310
6th March 2006, 04:57
hi,

I am trying to shift the focus from listbox to my textbox on right arrow is pressed. How
to detect those events?

Mahe2310

jpn
6th March 2006, 06:04
Override keyPressEvent (or install an event filter).

mahe2310
6th March 2006, 09:17
Override keyPressEvent (or install an event filter).

hi,

That worked.
But when ever it detects a key Event, it performs the operation defined and then it exits with segmentation fault.

Why it is not looping?
How can i recover the Seg Fault?

Can anybody suggest what happened?

Mahe2310

wysota
6th March 2006, 09:22
Show us your code.

zlatko
6th March 2006, 09:22
show a piece of code

p.s. too slowly :)

mahe2310
6th March 2006, 09:32
show a piece of code

p.s. too slowly :)


Hi,

I am including the code below.
Plz state why this happens?




FullCode::FullCode()
: QWidget( 0, 0 )
{

QGridLayout * g = new QGridLayout( this, 2, 2, 0);
............................

l = new QListBox( this );
g->addWidget( l, 1, 1 );
l->setMaximumSize(125,115);
l->insertItem("M",-1);
l->insertItem("Ma",-1);

............................
}


FullCode::~FullCode()
{
delete bg;
}


void FullCode::keyPressEvent(QKeyEvent *event)
{
printf("Key Press.... %d\n", event->key());
if(event->key() == Qt::Key_Up)
{
key_press = -1;
}
else if(event->key() == Qt::Key_Down)
{
key_press = 1;
}
else if(event->key() == Qt::Key_Right)
{
key_press = 2;
}
else if(event->key() == Qt::Key_Left)
{
key_press = -2;
}
else if(event->key() == Qt::Key_Return)
{
key_press = 3;
}
else {
key_press = 0;
}
}

void QListBox::keyPressEvent(QKeyEvent *event)
{
FullCode ff;
ff.keyPressEvent(event);
}




Regards,

Mahe2310

mahe2310
6th March 2006, 09:42
Hi all,

I got it.... The delete command in the FullCode Destructor is creating the error.
I just removed it
... Now it is working...


Mahe2310

zlatko
6th March 2006, 09:43
strange code for me ..it will be bettre if you use eventFilter, but if you want go by upper way you dont must use QListBox::keyPressEvent(QKeyEvent *event). Just in FullCode::keyPressEvent(QKeyEvent *event) insert checking what elements is now selected.

mahe2310
6th March 2006, 09:57
strange code for me ..it will be bettre if you use eventFilter, but if you want go by upper way you dont must use QListBox::keyPressEvent(QKeyEvent *event). Just in FullCode::keyPressEvent(QKeyEvent *event) insert checking what elements is now selected.


I didnt get that Zlatko. The reason is that, in FullCode::keyPressEvent(QKeyEvent *event), no keyEvent is being
detected that happens in ListBox. So i just called the events in Listbox and redirected it to FullCode keyPressEvent.

My entire Event handling is done at the FullCode keyPressEvent().

Mahe2310

wysota
6th March 2006, 10:44
if(event->key() == Qt::Key_Up)
{
key_press = -1;
}
else if(event->key() == Qt::Key_Down)
{
key_press = 1;
}
else if(event->key() == Qt::Key_Right)
{
key_press = 2;
}
else if(event->key() == Qt::Key_Left)
{
key_press = -2;
}
else if(event->key() == Qt::Key_Return)
{
key_press = 3;
}
else {
key_press = 0;
}


Hmm.... nice piece of code, but how about:

switch(event->key()){
case Qt::Key_Up: key_press=-1; break;
case Qt::Key_Right: key_press = 2; break;
case Qt::Key_Down: key_press = 1; break;
//...
default: key_press = 0;
}



void QListBox::keyPressEvent(QKeyEvent *event)
{
FullCode ff;
ff.keyPressEvent(event);
}

Now this is strange and I think that's what Zlatko didn't like. QListBox is an already existing Qt class, so how come you override it's member? I guess it's just a spelling mistake and some other class name should be there, but even so, why did you create that FullCode object there? You could have just inserted your code in the keyPressEvent of the listbox directly.
And if you don't want to subclass QListBox, you could use an eventFilter as suggested.

mahe2310
6th March 2006, 12:35
Switch case... Thanks for the advice...

Regards Latter...

Let me explain my procedure...
I just included a ListBox in my Screen. I need to have the default ListBox Events.
In addition to that, it should also perform some actions, i prefer. e.g. when Right Arrow or
Left Arrow is pressed i need to trap the events and perform certain set of operations.

For this i proceeded that way.
I didnt understand how event filter works...

Regards,

Mahe2310

wysota
6th March 2006, 12:39
http://doc.trolltech.com/3.3/eventsandfilters.html

mahe2310
8th March 2006, 09:14
hi,

Wat has to be done to make the action on listbox to takeplace?


QListBox *l;

l->setSelected(listSelect,true);



l->currentItem() is showing the proper one but display is on first element.

How can i make this change display on the screen.
I tried to repaint the window but didnt work.

Mahe2310

jpn
8th March 2006, 09:33
void QListBox::setSelected ( QListBoxItem * item, bool select ) [virtual]
Selects item if select is TRUE or unselects it if select is FALSE, and repaints the item appropriately.

It should repaint it automatically.. Maybe there's something wrong with your listSelect variable? How do you assign it?

mahe2310
8th March 2006, 09:43
It should repaint it automatically.. Maybe there's something wrong with your listSelect variable? How do you assign it?

i used the following function...

void QListBox::setSelected ( int index, bool select )

where listSelect is an integer variable.



static int select = 1;
static int listSelect = 1;
switch(event->key())
{
case Qt::Key_Up: printf("Up\n"); key_press = -1; break;
case Qt::Key_Down: printf("Down\n"); key_press = 1; break;
case Qt::Key_Right: printf("Right\n"); key_press = 2; break;
case Qt::Key_Left: printf("Left\n"); key_press = -2; break;
case Qt::Key_Return: printf("Enter\n"); key_press = 3; break;
default: printf("Fail\n"); key_press = 0;
}
if(select == 1)
{
if(key_press == 1)
{
if((int)l->count() > listSelect)
{
listSelect+=1;
}
l->setSelected(listSelect,true);
}
else if(key_press == -1)
{
if(listSelect > 1)
{
listSelect-=1;
}
l->setSelected(listSelect,true);
}
}
else if( select == 2)
{
//other operation
}

jpn
8th March 2006, 10:11
Try using setCurrentItem(int).

mahe2310
8th March 2006, 10:33
Try using setCurrentItem(int).

hi i tried that too but not working...
:( :confused:





static int select = 1;
static int listSelect = 1;
switch(event->key())
{
case Qt::Key_Up: printf("Up\n"); key_press = -1; break;
case Qt::Key_Down: printf("Down\n"); key_press = 1; break;
case Qt::Key_Right: printf("Right\n"); key_press = 2; break;
case Qt::Key_Left: printf("Left\n"); key_press = -2; break;
case Qt::Key_Return: printf("Enter\n"); key_press = 3; break;
default: printf("Fail\n"); key_press = 0;
}
if(select == 1)
{
if(key_press == 1)
{
if((int)l->count() > listSelect)
listSelect+=1;
l->setSelected(listSelect,true);
}
else if(key_press == -1)
{
if(listSelect > 1)
listSelect-=1;
l->setSelected(listSelect,true);
l->setCurrentItem(l->currentItem());
}
}
else if( select == 2)
{
//other operation
}

jpn
8th March 2006, 11:43
Err.. does something like this work?



int selection = l->currentItem();

switch (event->key())
{
case Qt::Key_Up: printf("Up\n"); selection--; break;
case Qt::Key_Down: printf("Down\n"); selection++; break;
}

if (selection >= 0 && selection < l->count())
{
l->setCurrentItem(selection);
}

mahe2310
9th March 2006, 04:25
static int select = 1;
static int listSelect = 1;
printf("Key Press.... %d\n", event->key());
l->setCurrentItem(5);


Even the above command is not working...
Do we have to enable anything for auto refresh of the list box?

Mahe2310

mahe2310
9th March 2006, 05:23
hi all... i got the answer...


It is just because i called the setCurrentItem() in keyPressEvent()of QWidget Class...
Now i shifted it to QListBox Class... Now it is working...


Thanks for support....

Mahe2310