Results 1 to 20 of 20

Thread: how to detect key events on a list box?

  1. #1
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default how to detect key events on a list box?

    hi,

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

    Mahe2310

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to detect key events on a list box?

    Override keyPressEvent (or install an event filter).

  3. #3
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    Quote Originally Posted by jpn
    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to detect key events on a list box?

    Show us your code.

  5. #5
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: how to detect key events on a list box?

    show a piece of code

    p.s. too slowly
    a life without programming is like an empty bottle

  6. #6
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    Quote Originally Posted by zlatko
    show a piece of code

    p.s. too slowly

    Hi,

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

    Qt Code:
    1. FullCode::FullCode()
    2. : QWidget( 0, 0 )
    3. {
    4.  
    5. QGridLayout * g = new QGridLayout( this, 2, 2, 0);
    6. ............................
    7.  
    8. l = new QListBox( this );
    9. g->addWidget( l, 1, 1 );
    10. l->setMaximumSize(125,115);
    11. l->insertItem("M",-1);
    12. l->insertItem("Ma",-1);
    13.  
    14. ............................
    15. }
    16.  
    17.  
    18. FullCode::~FullCode()
    19. {
    20. delete bg;
    21. }
    22.  
    23.  
    24. void FullCode::keyPressEvent(QKeyEvent *event)
    25. {
    26. printf("Key Press.... %d\n", event->key());
    27. if(event->key() == Qt::Key_Up)
    28. {
    29. key_press = -1;
    30. }
    31. else if(event->key() == Qt::Key_Down)
    32. {
    33. key_press = 1;
    34. }
    35. else if(event->key() == Qt::Key_Right)
    36. {
    37. key_press = 2;
    38. }
    39. else if(event->key() == Qt::Key_Left)
    40. {
    41. key_press = -2;
    42. }
    43. else if(event->key() == Qt::Key_Return)
    44. {
    45. key_press = 3;
    46. }
    47. else {
    48. key_press = 0;
    49. }
    50. }
    51.  
    52. void QListBox::keyPressEvent(QKeyEvent *event)
    53. {
    54. FullCode ff;
    55. ff.keyPressEvent(event);
    56. }
    To copy to clipboard, switch view to plain text mode 

    Regards,

    Mahe2310

  7. #7
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: how to detect key events on a list box?

    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

  9. #9
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    Quote Originally Posted by zlatko
    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to detect key events on a list box?

    Quote Originally Posted by mahe2310
    Qt Code:
    1. if(event->key() == Qt::Key_Up)
    2. {
    3. key_press = -1;
    4. }
    5. else if(event->key() == Qt::Key_Down)
    6. {
    7. key_press = 1;
    8. }
    9. else if(event->key() == Qt::Key_Right)
    10. {
    11. key_press = 2;
    12. }
    13. else if(event->key() == Qt::Key_Left)
    14. {
    15. key_press = -2;
    16. }
    17. else if(event->key() == Qt::Key_Return)
    18. {
    19. key_press = 3;
    20. }
    21. else {
    22. key_press = 0;
    23. }
    To copy to clipboard, switch view to plain text mode 
    Hmm.... nice piece of code, but how about:
    Qt Code:
    1. switch(event->key()){
    2. case Qt::Key_Up: key_press=-1; break;
    3. case Qt::Key_Right: key_press = 2; break;
    4. case Qt::Key_Down: key_press = 1; break;
    5. //...
    6. default: key_press = 0;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void QListBox::keyPressEvent(QKeyEvent *event)
    2. {
    3. FullCode ff;
    4. ff.keyPressEvent(event);
    5. }
    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.
    And if you don't want to subclass QListBox, you could use an eventFilter as suggested.

  11. #11
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    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

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to detect key events on a list box?


  13. #13
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    hi,

    Wat has to be done to make the action on listbox to takeplace?
    Qt Code:
    1. QListBox *l;
    2.  
    3. 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

  14. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to detect key events on a list box?

    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?

  15. #15
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    Quote Originally Posted by jpn
    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.

    Qt Code:
    1. static int select = 1;
    2. static int listSelect = 1;
    3. switch(event->key())
    4. {
    5. case Qt::Key_Up: printf("Up\n"); key_press = -1; break;
    6. case Qt::Key_Down: printf("Down\n"); key_press = 1; break;
    7. case Qt::Key_Right: printf("Right\n"); key_press = 2; break;
    8. case Qt::Key_Left: printf("Left\n"); key_press = -2; break;
    9. case Qt::Key_Return: printf("Enter\n"); key_press = 3; break;
    10. default: printf("Fail\n"); key_press = 0;
    11. }
    12. if(select == 1)
    13. {
    14. if(key_press == 1)
    15. {
    16. if((int)l->count() > listSelect)
    17. {
    18. listSelect+=1;
    19. }
    20. l->setSelected(listSelect,true);
    21. }
    22. else if(key_press == -1)
    23. {
    24. if(listSelect > 1)
    25. {
    26. listSelect-=1;
    27. }
    28. l->setSelected(listSelect,true);
    29. }
    30. }
    31. else if( select == 2)
    32. {
    33. //other operation
    34. }
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to detect key events on a list box?

    Try using setCurrentItem(int).

  17. #17
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    Quote Originally Posted by jpn
    Try using setCurrentItem(int).
    hi i tried that too but not working...


    Qt Code:
    1. static int select = 1;
    2. static int listSelect = 1;
    3. switch(event->key())
    4. {
    5. case Qt::Key_Up: printf("Up\n"); key_press = -1; break;
    6. case Qt::Key_Down: printf("Down\n"); key_press = 1; break;
    7. case Qt::Key_Right: printf("Right\n"); key_press = 2; break;
    8. case Qt::Key_Left: printf("Left\n"); key_press = -2; break;
    9. case Qt::Key_Return: printf("Enter\n"); key_press = 3; break;
    10. default: printf("Fail\n"); key_press = 0;
    11. }
    12. if(select == 1)
    13. {
    14. if(key_press == 1)
    15. {
    16. if((int)l->count() > listSelect)
    17. listSelect+=1;
    18. l->setSelected(listSelect,true);
    19. }
    20. else if(key_press == -1)
    21. {
    22. if(listSelect > 1)
    23. listSelect-=1;
    24. l->setSelected(listSelect,true);
    25. l->setCurrentItem(l->currentItem());
    26. }
    27. }
    28. else if( select == 2)
    29. {
    30. //other operation
    31. }
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to detect key events on a list box?

    Err.. does something like this work?

    Qt Code:
    1. int selection = l->currentItem();
    2.  
    3. switch (event->key())
    4. {
    5. case Qt::Key_Up: printf("Up\n"); selection--; break;
    6. case Qt::Key_Down: printf("Down\n"); selection++; break;
    7. }
    8.  
    9. if (selection >= 0 && selection < l->count())
    10. {
    11. l->setCurrentItem(selection);
    12. }
    To copy to clipboard, switch view to plain text mode 

  19. #19
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    Qt Code:
    1. static int select = 1;
    2. static int listSelect = 1;
    3. printf("Key Press.... %d\n", event->key());
    4. 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

  20. #20
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to detect key events on a list box?

    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

Similar Threads

  1. Replies: 10
    Last Post: 6th May 2011, 16:00
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  3. Replies: 1
    Last Post: 22nd October 2007, 02:04
  4. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  5. how to detect key pad events???
    By mahe2310 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 13th April 2006, 04:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.