Excellent, thanks for that tip.
I went with the eventFilter method, see below... I had to tell the event filter about the model, and the model has a toggle(QModelIndex) method which does the tick/unticking.
The alternative was to call ListView::edit(idx), but that didn't work, or the protected edit(idx,trigger,event), but I thought that was too much to fudge, at least this way I know what code is going to be executed.
{
public:
protected:
{
if (event
->type
() == QEvent::KeyPress) {
QKeyEvent *keyEvent
= dynamic_cast<QKeyEvent
*>
(event
);
switch (keyEvent->key())
{
case Qt::Key_Space:
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Select: // I think this a Phone/PDA key, but never mind hopefully can't hurt
{
// get the current selection
QModelIndexList sels = view->selectionModel()->selectedIndexes();
// get the current index
// are we a member?
if (sels.contains(curr))
{
// for each selection, toggle the checkbox through
// the usual listview::edit method.
for (QModelIndexList::iterator s = sels.begin(); s != sels.end(); ++s)
model->toggle(*s);
return true;
}
}
default: ;
}
}
// standard event processing
return QObject::eventFilter(obj, event
);
}
private:
ListModel_Checkable * model;
};
class MultiTicker : public QObject
{
public:
MultiTicker(QAbstractItemView * view, ListModel_Checkable * model) : QObject(view), view(view), model(model) {}
protected:
bool eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(event);
switch (keyEvent->key())
{
case Qt::Key_Space:
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Select: // I think this a Phone/PDA key, but never mind hopefully can't hurt
{
// get the current selection
QModelIndexList sels = view->selectionModel()->selectedIndexes();
// get the current index
QModelIndex curr = view->currentIndex();
// are we a member?
if (sels.contains(curr))
{
// for each selection, toggle the checkbox through
// the usual listview::edit method.
for (QModelIndexList::iterator s = sels.begin(); s != sels.end(); ++s)
model->toggle(*s);
return true;
}
}
default: ;
}
}
// standard event processing
return QObject::eventFilter(obj, event);
}
private:
QAbstractItemView * view;
ListModel_Checkable * model;
};
To copy to clipboard, switch view to plain text mode
Bookmarks