Results 1 to 9 of 9

Thread: how can move up/down the cursor after QComboBox showPopup?

  1. #1
    Join Date
    Sep 2011
    Posts
    30
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default how can move up/down the cursor after QComboBox showPopup?

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how can move up/down the cursor after QComboBox showPopup?

    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().

  3. #3
    Join Date
    Sep 2011
    Posts
    30
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how can move up/down the cursor after QComboBox showPopup?

    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.

    lpp.png

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how can move up/down the cursor after QComboBox showPopup?

    With cursor you mean selection? If so:
    Qt Code:
    1. void MyComboBox::keyPressEvent(QKeyEvent* e)
    2. {
    3. if (Qt::Key_Up == e->key()
    4. || Qt::Key_Down == e->key())
    5. return;
    6.  
    7. if (Qt::Key_Left == e->key())
    8. {
    9. QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
    10. QComboBox::keyPressEvent(&myEvent);
    11. }
    12. else if (Qt::Key_Right == e->key())
    13. {
    14. QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
    15. QComboBox::keyPressEvent(&myEvent);
    16. }
    17. else
    18. {
    19. QComboBox::keyPressEvent(e);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #5
    Join Date
    Sep 2011
    Posts
    30
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how can move up/down the cursor after QComboBox showPopup?

    Quote Originally Posted by Lykurg View Post
    With cursor you mean selection? If so:
    Qt Code:
    1. void MyComboBox::keyPressEvent(QKeyEvent* e)
    2. {
    3. if (Qt::Key_Up == e->key()
    4. || Qt::Key_Down == e->key())
    5. return;
    6.  
    7. if (Qt::Key_Left == e->key())
    8. {
    9. QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
    10. QComboBox::keyPressEvent(&myEvent);
    11. }
    12. else if (Qt::Key_Right == e->key())
    13. {
    14. QKeyEvent myEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
    15. QComboBox::keyPressEvent(&myEvent);
    16. }
    17. else
    18. {
    19. QComboBox::keyPressEvent(e);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how can move up/down the cursor after QComboBox showPopup?

    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?

  7. #7
    Join Date
    Sep 2011
    Posts
    30
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how can move up/down the cursor after QComboBox showPopup?

    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.


    Qt Code:
    1. #include "combobox.h"
    2.  
    3. #include <QEvent>
    4. #include <QKeyEvent>
    5. #include <QAbstractItemView>
    6. #include <QDebug>
    7.  
    8. ComboBox::ComboBox(QWidget *parent) :
    9. QComboBox(parent)
    10. {
    11. view()->installEventFilter(this);
    12. }
    13.  
    14. void ComboBox::keyPressEvent(QKeyEvent *e)
    15. {
    16. if (e->key() == Qt::Key_Space) return;
    17.  
    18. int key = Qt::Key_unknown;
    19. if (e->key() == Qt::Key_Alt || e->key() == Qt::Key_Meta) key = Qt::Key_Space;
    20.  
    21. if (key == Qt::Key_unknown) QComboBox::keyPressEvent(e);
    22. else
    23. {
    24. QKeyEvent myEvent(QEvent::KeyPress, key, Qt::NoModifier);
    25. QComboBox::keyPressEvent(&myEvent);
    26. }
    27. }
    28.  
    29. bool ComboBox::eventFilter(QObject *t, QEvent *e)
    30. {
    31. if(e->type() == QEvent::KeyPress)
    32. {
    33. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e);
    34. int key = keyEvent->key();
    35.  
    36. qDebug("Key = %x", key);
    37.  
    38. int key_map = Qt::Key_unknown;
    39. if (t == (QObject *)view())
    40. {
    41. switch(key)
    42. {
    43. case Qt::Key_Left:
    44. key_map = Qt::Key_Up;
    45. break;
    46. case Qt::Key_Right:
    47. key_map = Qt::Key_Down;
    48. break;
    49. case Qt::Key_Alt:
    50. case Qt::Key_Meta:
    51. break;
    52. case Qt::Key_Space:
    53. break;
    54. }
    55. }
    56.  
    57. if (key_map != Qt::Key_unknown)
    58. {
    59. QKeyEvent myEvent(QEvent::KeyPress, key_map, Qt::NoModifier);
    60. return QComboBox::eventFilter(t, &myEvent);
    61. }
    62. }
    63. return QComboBox::eventFilter(t, e);
    64. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 2nd December 2011 at 09:46. Reason: missing [code] tags

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how can move up/down the cursor after QComboBox showPopup?

    Hi,

    you have to use e.g.
    Qt Code:
    1. return QApplication::sendEvent(t, &myEvent);
    To copy to clipboard, switch view to plain text mode 
    .

  9. The following user says thank you to Lykurg for this useful post:

    josentop (2nd December 2011)

  10. #9
    Join Date
    Sep 2011
    Posts
    30
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how can move up/down the cursor after QComboBox showPopup?

    Hi Lykurg,
    It works well after applied the modification. thanks very much.

Similar Threads

  1. ReImplement showPopup for my QComboBOx
    By ber0y in forum Newbie
    Replies: 5
    Last Post: 8th April 2012, 12:42
  2. can't move cursor
    By felix_tang in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:36
  3. QListView: How to move the cursor to a specific row
    By muellerp in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2008, 07:29
  4. QCombobox showPopup problem
    By SailinShoes in forum Qt Programming
    Replies: 2
    Last Post: 21st August 2008, 11:16
  5. QCombobox showPopup
    By SailinShoes in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2008, 12:21

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.