Yeah your'e right! It's a hacky solution but it makes sense. But here it is, 
in the overriden keyPressEvent function,
void CustomListWidget
::keyPressEvent(QKeyEvent *event
) {
// feed this to base class as a default, we may also use this for simulating a key
switch(finalEvent->key())
{
case Qt::Key_Down:
{
// if last item reached wrap to first
if(currentRow() == (count()-1))
{
// set row to -1 so that later on doing the normal behavior it will go to row 1 which is the first item
setCurrentRow(-1);
}
break;
}
case Qt::Key_Up:
{
// if first item is reached wrap to last
if(currentRow() == 0)
{
if(count() > 1)
{
// set the current row at the top of the last row
setCurrentRow(count() - 2);
// and simulate a Key_Down event, event->key() = Key_Down
finalEvent
= new QKeyEvent(copy event
's values, except for key()) }//else no need to wrap first item = last
}
break;
}
default:
break;
}
if(finalEvent)
{
// perform the normal behavior
QListWidget::keyPressEvent(finalEvent);
}
}
void CustomListWidget::keyPressEvent(QKeyEvent *event)
{
// feed this to base class as a default, we may also use this for simulating a key
QKeyEvent *finalEvent = event;
switch(finalEvent->key())
{
case Qt::Key_Down:
{
// if last item reached wrap to first
if(currentRow() == (count()-1))
{
// set row to -1 so that later on doing the normal behavior it will go to row 1 which is the first item
setCurrentRow(-1);
}
break;
}
case Qt::Key_Up:
{
// if first item is reached wrap to last
if(currentRow() == 0)
{
if(count() > 1)
{
// set the current row at the top of the last row
setCurrentRow(count() - 2);
// and simulate a Key_Down event, event->key() = Key_Down
finalEvent = new QKeyEvent(copy event's values, except for key())
}//else no need to wrap first item = last
}
break;
}
default:
break;
}
if(finalEvent)
{
// perform the normal behavior
QListWidget::keyPressEvent(finalEvent);
}
}
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.
Bookmarks