PDA

View Full Version : the question about define my own selection behavior



calmspeaker
20th June 2009, 11:00
Hi ,all:)

Recently, I use tableview to make an alarm window which ,obviously, show the alarms in in the scada system.

The requirement is: If the operator is permitted by his privilege to ack the alarm in the alarm window,he can select the alarm, if not, he cannot select.

I inherit QTableView and rewrite the function
QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand(const QModelIndex &index, const QEvent *event) const
the code is like this:


QItemSelectionModel::SelectionFlags CAckTableView::selectionCommand ( const QModelIndex & index, const QEvent * event) const
{
if (the operator does not have privilege){
return QItemSelectionModel::NoUpdate | QItemSelectionModel::current;
}

return QTableView::selectionCommand(index,event);

}
Then I found the selcet event QEvent::MouseButtonPress act
properly,but QEvent::MouseMove is very strange.

The selection behavior of QEvent::MouseMove is depend on the last item the mouse pointer on. That is hard to describe clearly. Finally I probably find the problem in the qt source code. In
void QAbstractItemView::mouseMoveEvent(QMouseEvent *event),
call
setSelection(selectionRect, command);,

the parameter is


QPoint topLeft;
if (d->selectionMode != SingleSelection)
topLeft = d->pressedPosition - d->offset();
else
topLeft = bottomRight;
QPoint bottomRight = event->pos();
QRect selectionRect = QRect(topLeft, bottomRight);

QModelIndex index = indexAt(bottomRight);
QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);




In coclusion the problem is: the selection of MouseMove event is depend on whether the operator has the privilge on the last item the mouse pointed. The privilege cannot work!!

I think the problem is in the
void QAbstractItemView::mouseMoveEvent(QMouseEvent *event).
Did anyone meet such problem? Please give me a clue! Thank you!

shentian
20th June 2009, 12:40
You don't need to modify the selection model. You can tell the view which items can be selected by using the Qt::ItemIsSelectable flag in your data model.

Inherit your model and overwrite QAbstractItemModel::flags:


Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
{
if (/* user is allowd to select index.row() */)
return QAbstractTableModel::flags(index) | Qt::ItemIsSelectable;
else
return QAbstractTableModel::flags(index) & ~Qt::ItemIsSelectable;
}