Results 1 to 9 of 9

Thread: Circular wrapping of QListWidget items - how to?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Location
    Bahamas
    Posts
    29
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Circular wrapping of QListWidget items - how to?

    Yeah your'e right! It's a hacky solution but it makes sense. But here it is,

    in the overriden keyPressEvent function,

    Qt Code:
    1. void CustomListWidget::keyPressEvent(QKeyEvent *event)
    2. {
    3. // feed this to base class as a default, we may also use this for simulating a key
    4. QKeyEvent *finalEvent = event;
    5.  
    6. switch(finalEvent->key())
    7. {
    8. case Qt::Key_Down:
    9. {
    10. // if last item reached wrap to first
    11. if(currentRow() == (count()-1))
    12. {
    13. // set row to -1 so that later on doing the normal behavior it will go to row 1 which is the first item
    14. setCurrentRow(-1);
    15. }
    16. break;
    17. }
    18. case Qt::Key_Up:
    19. {
    20. // if first item is reached wrap to last
    21. if(currentRow() == 0)
    22. {
    23. if(count() > 1)
    24. {
    25. // set the current row at the top of the last row
    26. setCurrentRow(count() - 2);
    27. // and simulate a Key_Down event, event->key() = Key_Down
    28. finalEvent = new QKeyEvent(copy event's values, except for key())
    29. }//else no need to wrap first item = last
    30. }
    31. break;
    32. }
    33. default:
    34. break;
    35. }
    36.  
    37. if(finalEvent)
    38. {
    39. // perform the normal behavior
    40. QListWidget::keyPressEvent(finalEvent);
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 


    aamer4yu,

    Does Qt automatically delete the event object ? In this case it is possible that the event object is not passed to the base class, will it still be deleted somewhere? Or do I need to explicitly delete this?

    At first look, the event object may not be deleted if not passed to the base class. I hope what I'm doing is safe, I need to read more on Qt about this. Deleting the event object explicitly may even be worse. Please help.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Circular wrapping of QListWidget items - how to?

    Why have u done extra work for key_up ??
    you could have simply done as u did for key_down -
    case Key_up :
    if(currentRow==0)
    setCurrentRow(count() -1 );

    and if your conditions are not met, you can call QListWidget::keyPressEvent.
    something like -
    Qt Code:
    1. void CustomListWidget::keyPressEvent(QKeyEvent *event)
    2. {
    3. bool sendEventToBaseClass = true;
    4. switch(event->key())
    5. {
    6. case Qt::Key_up :
    7. if(condition)
    8. {
    9. sendEventToBaseClass = false;
    10. .... // other jobs
    11. }
    12.  
    13. break;
    14. case Qt::Key_Down :
    15. if(condition)
    16. {
    17. sendEventToBaseClass = false;
    18. .... // other jobs
    19. }
    20. break;
    21.  
    22. }
    23.  
    24. if(sendEventToBaseClass)
    25. QListWidget::keyPressEvent(event);
    26. }
    To copy to clipboard, switch view to plain text mode 

    thats it.. no need to keep duplicate pointer...

  3. #3
    Join Date
    Apr 2010
    Location
    Bahamas
    Posts
    29
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Circular wrapping of QListWidget items - how to?

    Hi,

    Why have u done extra work for key_up ??
    The base class's function may be setting variables or calling functions inside it to accomplish the default behavior. So I thought it would be safer to always call the base class function, anyway what I really need is just to change the current row. I've thought of creating a flag and checking if event is necessary to be passed to base class or not, like what you've done above.

    I will try this and see if it works with my code. I'll go for this if this works since this is very simple solution.

    With this solution, what shall we do then with the event object? Do we need to delete it explicitly after using it or not? In general, does Qt leave the deletion of the event objects to us or is it automatically deleted somewhere?

Similar Threads

  1. Move items up and down in QListWidget
    By araglin in forum Newbie
    Replies: 7
    Last Post: 31st August 2016, 11:05
  2. QListWidget cannot handle too many items
    By mr.hichem in forum Qt Programming
    Replies: 1
    Last Post: 2nd November 2009, 23:00
  3. Get Visible Items from the QListWidget
    By srj in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 20:13
  4. Qt 4.2: QListWidget changes size of its items
    By KingFish in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 11:06
  5. Getting all items of a QListWidget
    By Codepoet in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2006, 22:52

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
  •  
Qt is a trademark of The Qt Company.