Results 1 to 4 of 4

Thread: Base function first or Subclass function first? - What's the recommended way?

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

    Default Base function first or Subclass function first? - What's the recommended way?

    Hi,

    I am developing a customized list widget and I've derived it from QListWidget. I've encountered a problem where in I cannot select the row I wan't to select in my customized list. My list has overriden the QWidget::keyPressEvent function and it simply perfroms it's specific behavior and later on calls QListWidget::keyPressEvent to maintain the other default behavior? However, I've problems with this as I've said above, but when I called the base class's function first before the specific behavior my list selects the item correctly.

    What could be the problem here and what is the recommended way of overriding base class functions especially in the case of just adding a specific behavior(maintaining other default behavior)? Am I on the right track?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Base function first or Subclass function first? - What's the recommended way?

    As I understand it, you can no longer select rows using the keyboard with your custom list widget?

    Can you post the code of your keyPressEvent ?

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

    Default Re: Base function first or Subclass function first? - What's the recommended way?

    Oops...sorry I have some updates...Forget about my solution above - calling the base class function first before the overriden one.

    I realized it's safer to just set something, somewhat like simulating a normal behavior and let the base class perform the default behavior. So to summarize the behavior I need to is to wrap to the first item if user has pressed the down key while on the last item and vice versa. I somewhat implemented correctly this behavior but the other way around, my code failed.

    Here is how I implmented wrapping from last -> first item while down key is pressed on the last item,

    Qt Code:
    1. // if current row is equal to last item in the list
    2. listWidget->setCurrentRow(-1); // is there other non hacky way to do this? This has worked by the way
    3. // simulating a normal behavior, use row -1 so that next row is 0 which is the first item
    4. // call base class function here, this is expected to go to the next row which is 0
    To copy to clipboard, switch view to plain text mode 

    For the other way around, first -> last item wrap,

    Qt Code:
    1. // if current row is equal to the first row in the list
    2. listWidget->setCurrentRow(listWidget->count()); // simulating a normal behavior, setting the row = count, next row should be count -1 which is the last item
    3. // but this didn't work, how should i do this?
    4. // call base class function here, this should go to the next row count-1 but this didn't work
    To copy to clipboard, switch view to plain text mode 

    Please help.

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

    Default Re: Base function first or Subclass function first? - What's the recommended way?

    Sorry I've confused you. Please refer to my previous post. The keyPressEent looks like this,

    Qt Code:
    1. void CustomListWidget::keyPressEvent(QKeyEvent *event)
    2. {
    3. // custom code
    4. if(event->key() == Qt::Key_Down)
    5. {
    6. // if last item wrap to first
    7. if(currentRow() == count()-1)
    8. setCurrentRow(-1); // this worked
    9. }
    10. else if(event->key() == Qt::Key_Up)
    11. {
    12. // if first item wrap to last
    13. if(currentRow() == 0)
    14. setCurrentRow(count()); // this didn't work
    15. }
    16.  
    17. // let base class do the normal behavior
    18. QListWidget::keyPressEvent(event);
    19. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 10th March 2010, 09:13
  2. SigSlot conn from base classs to subclass
    By stevey in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2009, 12:12
  3. Signal in base class Slot in Subclass
    By csvivek in forum Newbie
    Replies: 7
    Last Post: 30th March 2008, 17:59
  4. Zip function for Qt4
    By devilj in forum Newbie
    Replies: 7
    Last Post: 15th July 2007, 15:37
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 22:26

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.