PDA

View Full Version : Base function first or Subclass function first? - What's the recommended way?



slscripters
25th May 2010, 08:50
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?:confused:

tbscope
25th May 2010, 09:26
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 ?

slscripters
25th May 2010, 09:27
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,



// if current row is equal to last item in the list
listWidget->setCurrentRow(-1); // is there other non hacky way to do this? This has worked by the way
// simulating a normal behavior, use row -1 so that next row is 0 which is the first item
// call base class function here, this is expected to go to the next row which is 0


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



// if current row is equal to the first row in the list
listWidget->setCurrentRow(listWidget->count()); // simulating a normal behavior, setting the row = count, next row should be count -1 which is the last item
// but this didn't work, how should i do this?
// call base class function here, this should go to the next row count-1 but this didn't work


Please help. :confused:

slscripters
25th May 2010, 09:46
Sorry I've confused you. Please refer to my previous post. The keyPressEent looks like this,



void CustomListWidget::keyPressEvent(QKeyEvent *event)
{
// custom code
if(event->key() == Qt::Key_Down)
{
// if last item wrap to first
if(currentRow() == count()-1)
setCurrentRow(-1); // this worked
}
else if(event->key() == Qt::Key_Up)
{
// if first item wrap to last
if(currentRow() == 0)
setCurrentRow(count()); // this didn't work
}

// let base class do the normal behavior
QListWidget::keyPressEvent(event);
}