Results 1 to 9 of 9

Thread: Circular wrapping of QListWidget items - how to?

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

    Default Circular wrapping of QListWidget items - how to?

    Hi,

    Is a QListWidget capable of a somewhat circular wrapping of items? When selection is at the end of the list and down key is pressed selection will immediately be set to the first item, and vice versa?

    I'm a Qt newbie and I can't find a function with this behavior (if there's any) in any Qt documentation . If there's none, can you please suggest some ideas? Thank you in advance!

  2. #2
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    I think that the closest QT has to offer out of the box is QLinkedList

    You can use the hasNext() and hasPrevious() to test if you have reached the end of the list
    and first() or last() to traverse it accordingly.
    Got to keep the loonies on the path ...

  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,

    I think that the closest QT has to offer out of the box is QLinkedList
    I think QLinkedList is just a container and doesn't draw something on the screen? Or can this be embedded in the QListWidget? Please guide me on your idea.

    One way I've thought about is to catch a signal that can be emitted when down/up key is pressed to change the current row of the QListWidget. However, when the current row is at the first/last item of the list, current row doesn't change anymore even if down/up key is pressed.

    Please help.

  4. #4
    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?

    You can do 2 things - either install an event filter on the QListWidget or subclass it and override the keyPressEvent.
    When you get a key press event for down arrow key, check the index of the current item, and if its last, set current item to first index.

    You can also override QListView::moveCursor and call the base class moveCursor first, then check if its first or last, and then modify the index to return as you wish .

    Hope you get the idea

  5. The following user says thank you to aamer4yu for this useful post:

    slscripters (25th May 2010)

  6. #5
    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,

    subclass it and override the keyPressEvent
    I've chose this method. My implementation iis somewhat like a hack but it makes sense though.

    If somebody wants to know the idea of my implementation just infrom me and I'll give you the idea. The implementation is not as straightforward as you think.

    Thanks again aamer4yu!

  7. #6
    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?

    You can anyway post the solution... you never know who can benefit from it

  8. #7
    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.

  9. #8
    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...

  10. #9
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.