hi,
I am trying to shift the focus from listbox to my textbox on right arrow is pressed. How
to detect those events?
Mahe2310
hi,
I am trying to shift the focus from listbox to my textbox on right arrow is pressed. How
to detect those events?
Mahe2310
Override keyPressEvent (or install an event filter).
hi,Originally Posted by jpn
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
Show us your code.
show a piece of code
p.s. too slowly
a life without programming is like an empty bottle
Originally Posted by zlatko
Hi,
I am including the code below.
Plz state why this happens?
Qt Code:
FullCode::FullCode() { ............................ 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; } { 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; } } { FullCode ff; ff.keyPressEvent(event); }To copy to clipboard, switch view to plain text mode
Regards,
Mahe2310
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
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.
a life without programming is like an empty bottle
Originally Posted by zlatko
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
Hmm.... nice piece of code, but how about:Originally Posted by mahe2310
Qt Code:
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; }To copy to clipboard, switch view to plain text mode
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.Qt Code:
{ FullCode ff; ff.keyPressEvent(event); }To copy to clipboard, switch view to plain text mode
And if you don't want to subclass QListBox, you could use an eventFilter as suggested.
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
hi,
Wat has to be done to make the action on listbox to takeplace?
Qt Code:
QListBox *l; l->setSelected(listSelect,true);To copy to clipboard, switch view to plain text mode
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
It should repaint it automatically.. Maybe there's something wrong with your listSelect variable? How do you assign it?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.
i used the following function...Originally Posted by jpn
void QListBox::setSelected ( int index, bool select )
where listSelect is an integer variable.
Qt Code:
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 }To copy to clipboard, switch view to plain text mode
Try using setCurrentItem(int).
hi i tried that too but not working...Originally Posted by jpn
Qt Code:
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 }To copy to clipboard, switch view to plain text mode
Err.. does something like this work?
Qt Code:
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); }To copy to clipboard, switch view to plain text mode
Qt Code:
static int select = 1; static int listSelect = 1; printf("Key Press.... %d\n", event->key()); l->setCurrentItem(5);To copy to clipboard, switch view to plain text mode
Even the above command is not working...
Do we have to enable anything for auto refresh of the list box?
Mahe2310
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
Bookmarks